1.主要用于移除白色噪声
2.第一对是侵蚀和膨胀
import cv2 import numpy as np cap=cv2.imread('apple1.jpg') hsv=cv2.cvtColor(cap,cv2.COLOR_BGR2HSV) lower_red = np.array([120,43,48]) upper_red = np.array([180,255,255]) mask = cv2.inRange(hsv,lower_red,upper_red) res = cv2.bitwise_and(cap,cap,mask=mask) kernel = np.ones((5,5),np.uint8) erosion = cv2.erode(mask,kernel,iterations=1) dilation = cv2.dilate(mask,kernel,iterations=1) cv2.imshow('erosion',erosion) cv2.imshow('dilation',dilation) cv2.waitKey(0) cv2.destroyAllWindows()
3.第二对是openning&closing
opening = cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernel)
closing = cv2.morphologyEx(mask,cv2.MORPH_CLOSE,kernel)