一、腐蚀操作
去除毛刺
cv.erode(image,kernel,iterations=1)
·iteration为迭代次数(腐蚀次数)
· kernel:卷积核的大小,越小越不容易腐蚀
二、膨胀操作
cv.erode(image,kernel,iterations=1)
三、开运算闭运算
开运算:
先腐蚀再膨胀
闭运算
先膨胀再腐蚀
四、梯度运算
梯度 = 膨胀 - 腐蚀
# 形态学——腐蚀操作
import cv2 as cv
import numpy as np
image = cv.imread("D:\opencv_project\image\lena3.png",cv.IMREAD_GRAYSCALE)
kernel = np.ones((30,30),np.uint8)
# erosion = cv.erode(image,kernel,iterations=1)
# # 膨胀操作
# kernel = np.ones((3,3),np.uint8)
# dige_dilate = cv.erode(image,kernel,iterations=1)
# cv.imshow("input",dige_dilate)
# cv.waitKey(0)
# cv.destroyAllWindows()
# image = cv.imread("D:\opencv_project\image\lena4.png")
# image = cv.resize(image,(500,500))
# kernel = np.ones((3,3),np.uint8)
# erosion1 = cv.erode(image,kernel,iterations=1)
# erosion2 = cv.erode(image,kernel,iterations=2)
# erosion3 = cv.erode(image,kernel,iterations=3)
# res = np.hstack((erosion1,erosion2,erosion3))
# cv.imshow("input",res)
# cv.waitKey(0)
# cv.destroyAllWindows()
# 开运算与闭运算
# 开:先腐蚀,再膨胀
# kernel = np.ones((5,5),np.uint8)
# opening = cv.morphologyEx(image,cv.MORPH_OPEN,kernel)
# cv.imshow("input",opening)
# cv.waitKey(0)
# cv.destroyAllWindows()
# 闭:先膨胀后腐蚀,可能去不掉毛刺
# kernel = np.ones((5,5),np.uint8)
# opening = cv.morphologyEx(image,cv.MORPH_CLOSE,kernel)
# cv.imshow("input",opening)
# cv.waitKey(0)
# cv.destroyAllWindows()
# 梯度运算 = 膨胀后 - 腐蚀后
# gradient = cv.morphologyEx(image,cv.MORPH_GRADIENT,kernel)
# cv.imshow("input",gradient)
# cv.waitKey(0)
# cv.destroyAllWindows()
# 礼帽和黑帽
"""
礼帽:原始输入——开运算结果
黑帽:闭运算-原始输入
"""
# 礼帽
# tophat = cv.morphologyEx(image,cv.MORPH_TOPHAT,kernel)
# cv.imshow("input",tophat)
# cv.waitKey(0)
# cv.destroyAllWindows()
# 黑帽
blahat = cv.morphologyEx(image,cv.MORPH_BLACKHAT,kernel)
cv.imshow("input",blahat)
cv.waitKey(0)
cv.destroyAllWindows()