• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

图像形态学

开发技术 开发技术 5天前 9次浏览

腐蚀操作

图像形态学图像形态学

 1 import cv2
 2 import numpy as np
 3 
 4 img = cv2.imread('default.png')
 5 cv2.imshow('test', img)
 6 kernel = np.ones((3, 5), np.uint8)
 7 # 腐蚀能将边缘的小刺剔除掉
 8 erosion = cv2.erode(img, kernel, iterations=1)
 9 cv2.imshow('corrosion', erosion)
10 cv2.waitKey(0)
11 cv2.destroyAllWindows()

View Code

 

膨胀操作

图像形态学图像形态学

 1 import cv2
 2 import numpy as np
 3 
 4 img = cv2.imread('default.png')
 5 kernel = np.ones((3, 3), np.uint8)
 6 dilate = cv2.dilate(img, kernel, iterations=1)
 7 cv2.imshow('Original', img)
 8 cv2.imshow('dilate', dilate)
 9 cv2.waitKey(0)
10 cv2.destroyAllWindows()

View Code

 

开运算

图像形态学图像形态学

 1 import cv2
 2 import numpy as np
 3 
 4 img = cv2.imread('default.png')
 5 cv2.imshow('Original', img)
 6 kernel = np.ones((5, 5), np.uint8)
 7 # 先腐蚀再膨胀,能将主体被腐蚀的部分补偿回去,但是边缘的毛刺很好的被清除掉
 8 opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
 9 cv2.imshow('opening', opening)
10 cv2.waitKey(0)
11 cv2.destroyAllWindows()

View Code

 

闭运算

图像形态学图像形态学

 1 import cv2
 2 import numpy as np
 3 
 4 img = cv2.imread('default.png')
 5 cv2.imshow('Original', img)
 6 kernel = np.ones((5, 5), np.uint8)
 7 # 先膨胀,再腐蚀,对边缘的毛刺没有什么清除效果,有点鸡肋
 8 closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
 9 cv2.imshow('closing', closing)
10 cv2.waitKey(0)
11 cv2.destroyAllWindows()

View Code

 

梯度运算

图像形态学图像形态学

 1 import cv2
 2 import numpy as np
 3 
 4 img = cv2.imread('default.png')
 5 cv2.imshow('Original', img)
 6 kernel = np.ones((5, 5), np.uint8)
 7 # 梯度=膨胀-腐蚀,可用来计算图像边缘
 8 gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
 9 cv2.imshow('gradient', gradient)
10 cv2.waitKey(0)
11 cv2.destroyAllWindows()

View Code

 

礼帽

图像形态学图像形态学

 1 import cv2
 2 import numpy as np
 3 
 4 img = cv2.imread('default.png')
 5 cv2.imshow('Original', img)
 6 kernel = np.ones((5, 5), np.uint8)
 7 # 礼帽=原始输入-开运算结果
 8 tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
 9 cv2.imshow('tophat', tophat)
10 cv2.waitKey(0)
11 cv2.destroyAllWindows()

View Code

 

黑帽

图像形态学图像形态学

 1 import cv2
 2 import numpy as np
 3 
 4 img = cv2.imread('default.png')
 5 cv2.imshow('Original', img)
 6 kernel = np.ones((5, 5), np.uint8)
 7 # 黑帽=闭运算-原始输入
 8 blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
 9 cv2.imshow('blackhat', blackhat)
10 cv2.waitKey(0)
11 cv2.destroyAllWindows()

View Code

 


程序员灯塔
转载请注明原文链接:图像形态学
喜欢 (0)