1.cv2.dilate(src, kernel, iteration)
參數說明: src表示輸入的圖片, kernel表示方框的大小, iteration表示迭代的次數
膨脹操作原理:存在一個kernel,在圖像上進行從左到右,從上到下的平移,如果方框中存在白色,那么這個方框內所有的顏色都是白色
代碼:
1.讀取帶有毛躁的圖片
2.使用cv2.erode進行腐蝕操作
3.使用cv2.dilate進行膨脹操作
import cv2 import numpy as np # 1.讀入圖片 img = cv2.imread('dige.png') cv2.imshow('original', img) cv2.waitKey(0) cv2.destroyAllWindows() kernel = np.ones((5, 5), np.uint8)

# 2.進行腐蝕操作,去除邊緣毛躁 erosion = cv2.erode(img, kernel, iterations=1) cv2.imshow('erosion', erosion) cv2.waitKey(0) cv2.destroyAllWindows()
、
# 3. 進行膨脹操作 dilate = cv2.dilate(erosion, kernel, iterations=1) cv2.imshow('dilate', dilate) cv2.waitKey(0) cv2.destroyAllWindows()

