# opencv图像处理

xiaoxiao2021-02-28  1

#腐蚀 """ import cv2 import numpy as np img = cv2.imread('one.jpg') kernel = np.ones((5,5),np.uint8) erosion = cv2.erode(img,kernel,iterations=1) cv2.imshow('erosion',erosion) cv2.waitKey(0) cv2.imwrite('erosion.jpg',erosion) """ #膨胀 """ import cv2 import numpy as np img = cv2.imread('one.jpg') kernel = np.ones((5,5),np.uint8)#使用一个5*5的卷积核 dilation = cv2.dilate(img,kernel,iterations=1) cv2.imshow('erosion',erosion) cv2.waitKey(0) cv2.imwrite('膨胀.jpg',erosion) """ #开运算（先腐蚀后膨胀）去除噪点 """ import cv2 import numpy as np img = cv2.imread('安全距离.jpg') kernel = np.ones((5,5),np.uint8)#使用一个5*5的卷积核 opening = cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel) cv2.imshow('erosion',erosion) cv2.waitKey(0) cv2.imwrite('开运算.jpg',erosion) """ #闭运算（先膨胀后腐蚀)填充物体上的小黑点 """ import cv2 import numpy as np img = cv2.imread('安全距离.jpg') kernel = np.ones((5,5),np.uint8)#使用一个5*5的卷积核 closing = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel) cv2.imshow('erosion',erosion) cv2.waitKey(0) cv2.imwrite('闭运算.jpg',erosion) """ #形态学梯度 """ import cv2 import numpy as np img = cv2.imread('安全距离.jpg') kernel = np.ones((5,5),np.uint8)#使用一个5*5的卷积核 gradient = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel) cv2.imshow('erosion',erosion) cv2.waitKey(0) cv2.imwrite('形态学梯度.jpg',erosion) """ #寻找轮廓 """ import numpy as np import cv2 img = cv2.imread('opening.jpg') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img,contours,-1,(0,0,255),3) cv2.imshow("img", img) cv2.waitKey(0) """

1、depth去除安全距离后

2、闭运算

3、腐蚀

4、开运算

5、膨胀

6、形态学梯度