python opencv学习(五)形态学 腐蚀膨胀

 

形态学 膨胀操作


#腐蚀操作,去毛刺
import cv2


import numpy as np
img1 = cv2.imread('1.png')

kernel=np.ones((3,3),np.uint8)  
dige_erosion=cv2.erode(img1,kenel,iterations = 1)

#膨胀操作,减少腐蚀的伤害

kernel=np.ones((3,3),np.uint8)
dige_dilate=cv2.dilate(dige_ersion,keenel,iterations = 1)



cv2.imshow('dige_dilate',dige_dilate)
cv2.waitKey(0)
cv2.destroyAllWindow()

开运算与闭运算


#开运算:先腐蚀,后膨胀
import cv2
import numpy as np


img = cv2.imread('1.png')

kernel=np.ones((3,3),np.uint8)  
opening = cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)#cv2.MORPH_OPEN开运算


cv2.imshow('opening',opening)
cv2.waitKey()
cv2.destroyAllWindows()


#闭运算:先膨胀,后腐蚀
import cv2
import numpy as np


img = cv2.imread('1.png')

kernel=np.ones((3,3),np.uint8)  
closing = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)#cv2.MORPH_CLOSE闭运算


cv2.imshow('closing',closing)
cv2.waitKey()
cv2.destroyAllWindows()

梯度运算

得到轮廓信息:执行原图膨胀减去原图腐蚀的差值 

gradient=cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)

礼帽与黑帽

  • 礼帽 = 原始输入 - 开运算结果
  • 黑帽 = 闭运算 - 原始输入

 

#礼帽

tophat=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)


#黑帽

blackhat=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)

python opencv学习(五)形态学 腐蚀膨胀_第1张图片

你可能感兴趣的:(python opencv学习(五)形态学 腐蚀膨胀)