开运算和闭运算实则就是将腐蚀操作和膨胀操作结合
而已,也就是个先后循序罢了
开运算:先腐蚀再膨胀
闭运算:先膨胀再腐蚀
cv2.morphologyEx(img_open,cv2.MORPH_OPEN,kernel)
cv2.morphologyEx(img_close,cv2.MORPH_CLOSE,kernel)
第一个参数:图像对象名称
第二个参数:模式选择是开运算还是闭运算
第三个参数:卷积核的大小
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)#开运算 先腐蚀再膨胀
img_open = cv2.imread('E:\Jupyter_workspace\study\data/jiaju.png')
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(img_open,cv2.MORPH_OPEN,kernel)
show_photo('opening',opening)#闭运算 先膨胀再腐蚀
img_close = cv2.imread('E:\Jupyter_workspace\study\data/jiaju.png')
kernel = np.ones((3,3),np.uint8)
closing = cv2.morphologyEx(img_close,cv2.MORPH_CLOSE,kernel)
show_photo('closing',closing)
原图:
开运算:
闭运算: