你对你的结构元素做了什么?你的其余代码在哪里?
我建议使用 morphological opening using cv2.morphologyEx
,如下所示:
import cv2
import numpy as np
from skimage import io # Only needed for web reading images
# Web read image; use cv2.imread(...) for local images
img = cv2.cvtColor(io.imread('https://i.stack.imgur.com/jnCvG.jpg'), cv2.COLOR_RGB2GRAY)
# Get rid of JPG artifacts
img = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)[1]
# Create structuring elements
horizontal_size = 11
vertical_size = 11
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontal_size, 1))
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, vertical_size))
# Morphological opening
mask1 = cv2.morphologyEx(img, cv2.MORPH_OPEN, horizontalStructure)
mask2 = cv2.morphologyEx(img, cv2.MORPH_OPEN, verticalStructure)
# Outputs
cv2.imshow('img', img)
cv2.imshow('mask1', mask1)
cv2.imshow('mask2', mask2)
cv2.waitKey(0)
cv2.destroyAllWindows()
我得到以下两个面具,对我来说看起来相当不错:
希望有帮助!
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.1
NumPy: 1.18.1
OpenCV: 4.2.0
----------------------------------------