#腐蚀:选取kernel区域内的最小值 # 矩形 kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)) print(kernel1) # 椭圆 kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)) print(kernel2) # 十字形 kernel3 = cv2.getStructuringElement(cv2.MORPH_CROSS, (5,5)) print(kernel3) image = imread('image.jpg') show(image) erosion = cv2.erode(image, kernel1) show(erosion) #膨胀:选取kernel区域内的最大值 #膨胀与腐蚀相反,取的是局部最大值。cv2.dilate() dilation = cv2.dilate(image, kernel) show(dilation) for i in range(3): dilation = cv2.dilate(image, kernel1, iterations=i+1) #输出的是迭代i+1次的结果 show(dilation) #Opening 开运算 #先符识后膨胀叫做开运算,消除小白点儿 image2 = imread('image2.jpg') show(image2) # 去除白点 opening = cv2.morphologyEx(image2, cv2.MORPH_OPEN, kernel1) show(opening) #Closing 闭运算 #先膨胀后腐蚀叫做闭运算,消除小黑点 # 去除黑点 closing = cv2.morphologyEx(image2, cv2.MORPH_CLOSE, kernel1) show(closing) #先开后闭的运算,消除黑白的小点 opening = cv2.morphologyEx(image2, cv2.MORPH_OPEN, kernel1) closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel1) show(closing) #Gradient 形态学梯度 #膨胀图减去俯视图,得到罗阔图 gradient = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel1) show(gradient) #TOP Hat/White Hat顶帽,白帽 #原图减去开运算后的图:小白点 tophat = cv2.morphologyEx(image2, cv2.MORPH_TOPHAT, kernel1) show(tophat) #Black Hat黑帽 #闭运算的图减去原图:小黑点 blackhat = cv2.morphologyEx(image2, cv2.MORPH_BLACKHAT, kernel1) show(blackhat)