import cv2标签:kernel,img,python,imshow,cv2,笔记,cat,opencv,np From: https://www.cnblogs.com/kun-sir/p/16735583.html
import numpy as np
#
# img=cv2.imread('cat.jpeg')
# # cv2.imshow('cat',img)
# # cv2.waitKey()
# # cv2.destroyAllWindows()
#
#对图像进行腐蚀操作
# ken=np.ones((5,5),np.uint8)
# esion=cv2.erode(img,ken,iterations=1)
# cv2.imshow('cat',esion)
# cv2.waitKey()
# cv2.destroyAllWindows()
#膨胀操作
# kernel=np.ones((5,5),np.uint8)
# cat_img=cv2.dilate(esion,kernel,iterations=1)
# cv2.imshow('cat_img',cat_img)
# cv2.waitKey()
# cv2.destroyAllWindows()
'''腐蚀与膨胀互为逆运算,其中np.ones()函数返回给定形状和数据类型的新数组,其中元素的值设置为1'''
#开运算,先腐蚀,后膨胀
# img=cv2.imread('cat.jpeg')
# kernel=np.ones((5,5),np.uint8)
# opening=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
# cv2.imshow('opening',opening)
# cv2.waitKey()
# cv2.destroyAllWindows()
#闭运算,先膨胀,后腐蚀
# img=cv2.imread('cat.jpeg')
# kernel=np.ones((5,5),np.uint8)
# closeing=cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)
# cv2.imshow('closeing',closeing)
# cv2.waitKey()
# cv2.destroyAllWindows()
#梯度运算
#原图(膨胀)- 腐蚀
# img=cv2.imread('cat.jpeg')
# kernel=np.ones((7,7),np.uint8)
#
# gradient=cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)
#
# cv2.imshow('closeing',gradient)
# cv2.waitKey()
# cv2.destroyAllWindows()
# 礼帽运算
#原始运算- 开运算结果
# img=cv2.imread('cat.jpeg')
#
# kernel=np.ones((7,7),np.uint8)
# tophat=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)
# cv2.imshow('tophat',tophat)
# cv2.waitKey()
# cv2.destroyAllWindows()
#黑帽运算
#闭运算- 原始运算
img=cv2.imread('cat.jpeg')
kernel=np.ones((7,7),np.uint8)
blackhat=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)
cv2.imshow('blackhat',blackhat)
cv2.waitKey()
cv2.destroyAllWindows()