形态学中的腐蚀操作一般处理的图像数据为二值
的
cv2.erode(img,kernel,iterations = 1)
kernel表示拿多大的卷积核去腐蚀
iterations表示迭代次数
可以将一些带有毛毛的图像去毛毛化
原图
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)
去毛毛
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')
kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
show_photo('erosion',erosion)
当然,这只是一个样例罢了
腐蚀操作其实就是对一些边缘进行剪切,处理之后的照片会相对变细
下面进行迭代次数的演示
import cv2
import numpy as npdef show_photo(name,picture):cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()pie = cv2.imread('E:\Jupyter_workspace\study\data/pie.png')
show_photo('pie',pie)
原图:
不同的迭代次数也会对图片有着不同的影响
import cv2
import numpy as npdef show_photo(name,picture):cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()pie = cv2.imread('E:\Jupyter_workspace\study\data/pie.png')kernel = np.ones((30,30),np.uint8)
erosion_1 = cv2.erode(pie,kernel,iterations = 1)
erosion_2 = cv2.erode(pie,kernel,iterations = 2)
erosion_3 = cv2.erode(pie,kernel,iterations = 3)
res = np.hstack((pie,erosion_1,erosion_2,erosion_3))show_photo('YT-1-2-3',res)
不同的迭代次数的影响: