前言
参考视频:opencv教学 参考教材:《数字图像处理基础》 我的代码基本是跟着B站的视频里面敲了一遍,然后结合教材对指定区域做了一些加强学习
一、腐蚀和膨胀
首先需要知道的是,形态学运算主要针对二值化图像。 所以在进行形态学操作前 ,我们首先应该将图像转为二值化图像
import numpy as np
X1=cv2.imread('xinpian.jpeg',0)
thresh,img = cv2.threshold(X1, thresh=128, maxval=255, type=cv2.THRESH_BINARY)
如图我是生成了一张芯片的二值化图像
1.1腐蚀
紧接着我们调用腐蚀命令进行操作,需要定义一个内核kernel
kernel=np.ones((5,5),np.uint8)
crosion=cv2.erode(img,kernel,iterations=1)
实现的效果如下: 可以看到很多较细的部分 被腐蚀掉了 1)接下来我们探讨一下不同大小的kernel对于图像的腐蚀效果差异是什么样子的?: 分别设置内核大小如下:
crosion2=cv2.erode(img,kernel2,iterations=1)
crosion3=cv2.erode(img,kernel3,iterations=1)
crosion4=cv2.erode(img,kernel4,iterations=1)
crosion5=cv2.erode(img,kernel5,iterations=1)
应该是内核越大,腐蚀的越狠。 整体代码:
import cv2
import numpy as np
X1=cv2.imread('xinpian.jpeg',0)
thresh,img = cv2.threshold(X1, thresh=128, maxval=255, type=cv2.THRESH_BINARY)
cv2.imshow('xinpian',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel2=np.ones((2,2),np.uint8)
kernel3=np.ones((3,3),np.uint8)
kernel4=np.ones((4,4),np.uint8)
kernel5=np.ones((5,5),np.uint8)
crosion2=cv2.erode(img,kernel2,iterations=1)
crosion3=cv2.erode(img,kernel3,iterations=1)
crosion4=cv2.erode(img,kernel4,iterations=1)
crosion5=cv2.erode(img,kernel5,iterations=1)
cv2.imshow('fushi2',crosion2)
cv2.imshow('fushi3',crosion3)
cv2.imshow('fushi4',crosion4)
cv2.imshow('fushi5',crosion5)
cv2.waitKey(0)
cv2.destroyAllWindows()
2)接下来我们探讨一下迭代次数的大小对于图像的腐蚀效果差异是什么样子的?: 分别设置迭代次数如下:
crosion1=cv2.erode(img,kernel,iterations=1)
crosion2=cv2.erode(img,kernel,iterations=2)
crosion3=cv2.erode(img,kernel,iterations=3)
crosion4=cv2.erode(img,kernel,iterations=4)
当然肯定是迭代次数越多,腐蚀越明显。 完整代码:
import cv2
import numpy as np
X1=cv2.imread('xinpian.jpeg',0)
thresh,img = cv2.threshold(X1, thresh=128, maxval=255, type=cv2.THRESH_BINARY)
cv2.imshow('xinpian',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel=np.ones((2,2),np.uint8)
crosion1=cv2.erode(img,kernel,iterations=1)
crosion2=cv2.erode(img,kernel,iterations=2)
crosion3=cv2.erode(img,kernel,iterations=3)
crosion4=cv2.erode(img,kernel,iterations=4)
cv2.imshow('fushi1',crosion1)
cv2.imshow('fushi2',crosion2)
cv2.imshow('fushi3',crosion3)
cv2.imshow('fushi4',crosion4)
cv2.waitKey(0)
cv2.destroyAllWindows()
1.2膨胀
膨胀运算和腐蚀运算就是反过来的
import cv2
import numpy as np
X1=cv2.imread('xinpian.jpeg',0)
thresh,img = cv2.threshold(X1, thresh=128, maxval=255, type=cv2.THRESH_BINARY)
cv2.imshow('xinpian',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel=np.ones((5,5),np.uint8)
crosion=cv2.dilate(img,kernel,iterations=1)
cv2.imshow('fushi',crosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
最后的膨胀效果:
改变参数效果和腐蚀运算类似,这就不单独再列出了。
二、开运算与闭运算
1)开运算简单的说,就是对图像先腐蚀再膨胀 直接上代码看看
import cv2
import numpy as np
X1=cv2.imread('xinpian.jpeg',0)
thresh,img = cv2.threshold(X1, thresh=128, maxval=255, type=cv2.THRESH_BINARY)
cv2.imshow('xinpian',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel=np.ones((5,5),np.uint8)
crosion=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
cv2.imshow('fushi',crosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
2)闭运算简单的说,就是对图像先膨胀再腐蚀
import cv2
import numpy as np
X1=cv2.imread('xinpian.jpeg',0)
thresh,img = cv2.threshold(X1, thresh=128, maxval=255, type=cv2.THRESH_BINARY)
cv2.imshow('xinpian',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
kernel=np.ones((5,5),np.uint8)
crosion=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
cv2.imshow('fushi',crosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
三、礼帽与黑帽
礼帽运算:原始图像减去开运算图像 黑帽运算:闭运算图像减去原始图像 1)礼帽:
import numpy as np
X1=cv2.imread('xinpian.jpeg',0)
thresh,img = cv2.threshold(X1, thresh=128, maxval=255, type=cv2.THRESH_BINARY)
kernel=np.ones((5,5),np.uint8)
crosion=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)
cv2.imshow('xinpian',img)
cv2.imshow('limao',crosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
2)黑帽:
import cv2
import numpy as np
X1=cv2.imread('xinpian.jpeg',0)
thresh,img = cv2.threshold(X1, thresh=128, maxval=255, type=cv2.THRESH_BINARY)
kernel=np.ones((5,5),np.uint8)
crosion=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)
cv2.imshow('xinpian',img)
cv2.imshow('heimao',crosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
|