使用腐蚀操作的大前提是黑底白字,具有二值性
其中iteration是迭代次数,迭代值越大腐蚀性越高
腐蚀的一个例子是在黑底白字中去除与白字相连的毛刺
import cv2 as cv
import numpy as np
img = cv.imread("D:/Coding/rat2.jpg")
kernal=np.ones((5,5),np.uint8)
#创建各个元素都是1的卷积矩阵
eronsion=cv.erode(img,kernal,iterations=1)
cv.namedWindow("eronsion output",cv.WINDOW_FREERATIO)
cv.imshow("eronsion output",eronsion)
cv.waitKey(0)
cv.destroyAllWindows()
使用膨胀操作的大前提是黑底白字,具有二值性
其中iteration是迭代次数,迭代值越大腐蚀性越高
import cv2 as cv
import numpy as np
img = cv.imread("D:/Coding/rat2.jpg")
kernal=np.ones((3,3),np.uint8)
#创建各个元素都是1的卷积矩阵
dig_dilate=cv.dilate(img,kernal,iterations=1)
cv.namedWindow("eronsion output",cv.WINDOW_FREERATIO)
cv.imshow("eronsion output",dig_dilate)
cv.waitKey(0)
cv.destroyAllWindows()
先腐蚀再膨胀
import cv2 as cv
import numpy as np
img = cv.imread("D:/Coding/rat2.jpg")
kernal=np.ones((3,3),np.uint8)
#创建各个元素都是1的卷积矩阵
openning=cv.morphologyEx(img,cv.MORPH_OPEN,kernal)
cv.namedWindow("eronsion output",cv.WINDOW_FREERATIO)
cv.imshow("eronsion output",openning)
cv.waitKey(0)
cv.destroyAllWindows()
先膨胀再腐蚀
import cv2 as cv
import numpy as np
img = cv.imread("D:/Coding/rat2.jpg")
kernal=np.ones((3,3),np.uint8)
#创建各个元素都是1的卷积矩阵
openning=cv.morphologyEx(img,cv.MORPH_CLOSE,kernal)
cv.namedWindow("eronsion output",cv.WINDOW_FREERATIO)
cv.imshow("eronsion output",openning)
cv.waitKey(0)
cv.destroyAllWindows()
梯度=膨胀减腐蚀得到边界信息
import cv2 as cv
import numpy as np
img = cv.imread("D:/Coding/rat2.jpg")
kernal=np.ones((3,3),np.uint8)
#创建各个元素都是1的卷积矩阵
gradient=cv.morphologyEx(img,cv.MORPH_GRADIENT,kernal)
cv.namedWindow("gradient output",cv.WINDOW_FREERATIO)
cv.imshow("gradient output",gradient)
cv.waitKey(0)
cv.destroyAllWindows()
礼帽=原始数据-开运算=原始数据-(先腐蚀再膨胀)
带刺-不带刺=刺
import cv2 as cv
import numpy as np
img = cv.imread("D:/Coding/rat2.jpg")
kernal=np.ones((3,3),np.uint8)
#创建各个元素都是1的卷积矩阵
tophat=cv.morphologyEx(img,cv.MORPH_TOPHAT,kernal)
cv.namedWindow("tophat output",cv.WINDOW_FREERATIO)
cv.imshow("tophat output",tophat)
cv.waitKey(0)
cv.destroyAllWindows()
笔帽=闭运算-原始数据=(先膨胀再腐蚀)-原始数据
剩下原始数据轮廓
import cv2 as cv
import numpy as np
img = cv.imread("D:/Coding/rat2.jpg")
kernal=np.ones((3,3),np.uint8)
#创建各个元素都是1的卷积矩阵
blackhat=cv.morphologyEx(img,cv.MORPH_BLACKHAT,kernal)
cv.namedWindow("blackhat",cv.WINDOW_FREERATIO)
cv.imshow("blackhat",blackhat)
cv.waitKey(0)
cv.destroyAllWindows()