opencv python 学习笔记 10开运算和闭运算 梯度运算

梯度运算

import cv2
import numpy as np
pie=cv2.imread('d:/opencv/tx/pie.png')
kernel = np.ones((7,7),np.uint8)
#膨胀
dilate = cv2.dilate(pie,kernel ,iterations= 5)
#腐蚀
erosion = cv2.erode(pie,kernel ,iterations= 5)
res =np.hstack((dilate ,erosion ))
cv2.imshow('res',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
#膨胀-腐蚀=梯度运算
gradient = cv2.morphologyEx(pie,cv2.MORPH_GRADIENT ,kernel)
cv2.imshow('gradient ',gradient )
cv2.waitKey(0)
cv2.destroyAllWindows()

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

#礼帽
img=cv2.imread('D:/opencv/tx/dige.png')
kernel = np.ones((3,3),np.uint8)
tophat = cv2.morphologyEx(img,cv2.MORPH_TOPHAT ,kernel)
cv2.imshow('tophat ',tophat )
cv2.waitKey(0)
cv2.destroyAllWindows()
#黑帽
img=cv2.imread('D:/opencv/tx/dige.png')
kernel = np.ones((3,3),np.uint8)
blackhat = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT ,kernel)
cv2.imshow('blackhat ',blackhat )
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
#开运算与闭运算:
# 开:先腐蚀后膨胀
img = cv2.imread('D:/opencv/tx/dige.png')
kernel = np.ones((3,3),np.uint8)
opening =cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
cv2.imshow('opening',opening)
cv2.waitKey(0)
cv2.destroyAllWindows()
#闭:先膨胀再腐蚀
img = cv2.imread('D:/opencv/tx/dige.png')
kernel = np.ones((3,3),np.uint8)
closing = cv2.morphologyEx(img,cv2.MORPH_CLOSE , kernel )
cv2.imshow('closing',closing)
cv2.waitKey(0)
cv2.destroyAllWindows()

你可能感兴趣的:(opencv)