#coding=utf-8 import cv2 import numpy as np img=cv2.imread('timg.jpeg',cv2.CV_LOAD_IMAGE_GRAYSCALE) #创建矩形结构单元 g=cv2.getStructuringElement(cv2.MORPH_RECT,(9,9)) #形态学处理,开运算 img_open=cv2.morphologyEx(img,cv2.MORPH_OPEN,g) img_hat=img-img_open cv2.imshow('img',img) #cv2.imshow('erode',edge_dilate) cv2.imshow('img_open',img_open) cv2.imshow('img_open_',img_hat) cv2.waitKey(0) cv2.destroyAllWindows()