闭运算
先膨胀再腐蚀
有助于关闭前景物体内部的小孔或物体上的黑点
函数morphologyEx
opening = cv2.morphologyEx(src , cv2.MORPH_CLOSE , kernel)
import cv2
import numpy as np
img = cv2.imread('111.jpg',cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
k = np.ones((5,5),np.uint8)
r,tw = cv2.threshold(gray, 120 , 255 , cv2.THRESH_BINARY)
dst = cv2.morphologyEx(tw, cv2.MORPH_CLOSE, k)
cv2.namedWindow('gray',cv2.WINDOW_AUTOSIZE)
cv2.namedWindow('tw',cv2.WINDOW_AUTOSIZE)
cv2.namedWindow('dst',cv2.WINDOW_AUTOSIZE)
cv2.imshow('gray',gray)
cv2.imshow('tw',tw)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()