1 腐蚀
import cv2
import numpy as np
img=cv2.imread('C:/Users/Administrator/Desktop/kai.jpg')
kerneal =cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
#腐蚀
dst=cv2.erode(img,kerneal,iterations=2)
cv2.imshow("img",np.hstack((img,dst)))
cv2.waitKey(0)
cv2.destroyAllWindows()
2 膨胀
import cv2
import numpy as np
img=cv2.imread('C:/Users/Administrator/Desktop/bi.jpg')
kerneal =cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
# #膨胀
dst=cv2.dilate(img,kerneal,2)
cv2.imshow("img",np.hstack((img,dst)))
cv2.waitKey(0)
cv2.destroyAllWindows()
3 开操作 :先腐蚀再膨胀
import cv2
import numpy as np
img=cv2.imread('C:/Users/Administrator/Desktop/kai.jpg')
kerneal =cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
#开操作
dst=cv2.morphologyEx(img,cv2.MORPH_OPEN,kerneal,iterations=2)
cv2.imshow("img",np.hstack((img,dst)))
cv2.waitKey(0)
cv2.destroyAllWindows()
4 闭操作 :先膨胀再腐蚀
import cv2
import numpy as np
img=cv2.imread('C:/Users/Administrator/Desktop/bi.jpg')
kerneal =cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
#闭操作
dst=cv2.morphologyEx(img,cv2.MORPH_CLOSE,kerneal,iterations=2)
cv2.imshow("img",np.hstack((img,dst)))
cv2.waitKey(0)
cv2.destroyAllWindows()
5 形态学梯度 =原图-腐蚀图
import cv2
import numpy as np
img=cv2.imread('C:/Users/Administrator/Desktop/bi.jpg')
kerneal =cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
#形态学梯度
dst=cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kerneal,iterations=2)
cv2.imshow("img",np.hstack((img,dst)))
cv2.waitKey(0)
cv2.destroyAllWindows()
FR:徐海涛(hunkxu)