形态学中的膨胀操作即让照片变得更大,与腐蚀操作互为逆运算
cv2.dilate(erosion,kernel,iterations = 1)
第一个参数:图像对象名称
第二个参数:卷积核的大小
第三个参数:迭代次数
此时就可与腐蚀操作进行相结合,腐蚀去毛毛但是会损坏图像粗细,然后再膨胀尽可能还原图像
import cv2
import numpy as npdef show_photo(name,picture):cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()img = cv2.imread('E:\Jupyter_workspace\study\data/jiaju.png')
show_photo('jiaju',img)kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
show_photo('erosion',erosion)kernel = np.ones((3,3),np.uint8)
dige_dilate = cv2.dilate(erosion,kernel,iterations = 1)
show_photo('dilate',dige_dilate)res = np.hstack((img,erosion,dige_dilate))
show_photo('YT_FS-PZ',res)
原图:
腐蚀:
膨胀:
合并对比:
接着看下迭代次数iterations对膨胀操作的效果
import cv2
import numpy as npdef show_photo(name,picture):cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()pie = cv2.imread('E:\Jupyter_workspace\study\data/pie.png')kernel = np.ones((30,30),np.uint8)
dilate_1 = cv2.dilate(pie,kernel,iterations = 1)
dilate_2 = cv2.dilate(pie,kernel,iterations = 2)
dilate_3 = cv2.dilate(pie,kernel,iterations = 3)
res = np.hstack((pie,dilate_1,dilate_2,dilate_3))show_photo('YT_1-2-3',res)