导包:
import numpy as np import cv2 import matplotlib.pyplot as plt def show(image): plt.imshow(image) plt.axis('off') plt.show() def imread(image): image=cv2.imread(image) image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB) return image
开运算代码:
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(10,10)) opening=cv2.morphologyEx(image,cv2.MORPH_OPEN,kernel) show(opening)
闭运算代码:#下面开始闭运算,膨胀之后再腐蚀,用于去除黑点
kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(10,10)) opening=cv2.morphologyEx(image,cv2.MORPH_OPEN,kernel) show(opening)