参考视频: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)
紧接着我们调用腐蚀命令进行操作,需要定义一个内核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) #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) #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) #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) #0为任意键位终止
cv2.destroyAllWindows()
膨胀运算和腐蚀运算就是反过来的
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) #0为任意键位终止
cv2.destroyAllWindows()
kernel=np.ones((5,5),np.uint8)
crosion=cv2.dilate(img,kernel,iterations=1)
cv2.imshow('fushi',crosion)
cv2.waitKey(0) #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) #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) #0为任意键位终止
cv2.destroyAllWindows()
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) #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) #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) #0为任意键位终止
cv2.destroyAllWindows()
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) #0为任意键位终止
cv2.destroyAllWindows()