参考链接:https://www.cnblogs.com/babycomeon/p/13112687.html
开运算:先腐蚀,再膨胀
例子:
待处理图像
目标:取出电路图,消除标注
import cv2 import numpy as np import matplotlib.pyplot as plt S = cv2.imread('Source.jpg',0) plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False #plt.imshow(S,'gray'), plt.title('电路原图') #构造开运算结构元素 struct_1 = np.zeros((15,15),np.uint8) struct_2 = struct_1.copy() struct_1[7,...] = 1 struct_2[...,7] = 1 struct_3 = np.identity(9,np.uint8) struct_4 = np.fliplr(struct_3) s_5 = np.identity(3,np.uint8) s_6 = np.fliplr(s_5) #水平方向腐蚀、膨胀 Row = cv2.erode(S,struct_1,iterations = 1) Row = cv2.dilate(Row,struct_1,iterations = 1) #竖直方向腐蚀、膨胀 Col = cv2.erode(S,struct_2,iterations = 1) Col = cv2.dilate(Col,struct_2,iterations = 1) #右斜方向腐蚀、膨胀 Right = cv2.erode(S,struct_3,iterations = 1) Right = cv2.dilate(Right,s_5,iterations = 1) #左斜方向腐蚀、膨胀 Left = cv2.erode(S,struct_4,iterations = 1) Left = cv2.dilate(Left,s_6,iterations = 1) Img_out = Row + Col + 2*Right + 2*Left plt.imshow(Img_out,'gray'), plt.title('腐蚀图') cv2.imwrite('out.bmp',Img_out)
结果: