opencv-python 图像基础处理(三),,腐蚀操作#腐蚀操作i


腐蚀操作

#腐蚀操作import  cv2import numpy as  npimg=cv2.imread("d:/ke.png")kernel = np.ones((3,3),np.uint8)erosion = cv2.erode(img,kernel,iterations = 1)#腐蚀操作 iterations控制腐蚀程度erosion1 = cv2.erode(img,kernel,iterations = 2)erosion2 = cv2.erode(img,kernel,iterations = 3)res=np.hstack((img,erosion,erosion1,erosion2))cv2.imshow(‘erosion‘, res)cv2.waitKey(0)cv2.destroyAllWindows()

技术图片

膨胀操作

import  cv2import numpy as npimg=cv2.imread("d:/ke.png")kernel=np.ones((3,3),np.uint8)img_diate=cv2.dilate(img,kernel,iterations=1) #膨胀操作img_diate1=cv2.dilate(img,kernel,iterations=2)img_diate2=cv2.dilate(img,kernel,iterations=3)res=np.hstack((img_diate,img_diate1,img_diate2)) #水平展示cv2.imshow("ditae",res)cv2.waitKey(0)cv2.destroyAllWindows()

技术图片

开闭运算

import  cv2import  numpy as npimg=cv2.imread("d:/ke.png")kernel=np.ones((3,3),np.uint8)open=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)#开运算:先腐蚀,后膨胀close=cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)#关运算:先膨胀,后腐蚀res=np.hstack((open,close))cv2.imshow("kai vs close",res)cv2.waitKey(0)cv2.destroyAllWindows()

技术图片

梯度运算

#梯度运算import  cv2import  numpy as npimg=cv2.imread("d:/ke.png")kernel=np.ones((3,3),np.uint8)dilate=cv2.dilate(img,kernel,iterations=3) #膨胀erosion=cv2.erode(img,kernel,iterations=3) #腐蚀gradient=cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel) #梯度res=np.hstack((dilate,erosion,gradient))cv2.imshow("show",res)cv2.waitKey(0)cv2.destroyAllWindows()

技术图片

礼帽与黑帽

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

黑帽 = 闭运算-原始输入

import cv2import numpy as  npimg=cv2.imread("d:/ke.png")kernel=np.ones((3,3),np.uint8)tophar=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel) #礼帽blackhar=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel) #黑帽res=np.hstack((tophar,blackhar))cv2.imshow("hat",res)cv2.waitKey(0)cv2.destoryAllWindows()

技术图片

opencv-python 图像基础处理(三)

评论关闭