如何去除贴片边缘的黑点?

How to remove black dots from the edge of the patch?

我正在使用轮廓滤镜去除补丁边缘的黑点,但问题仍然存在。有什么办法可以去掉那些点吗?

绿色染色的提取结果:

这是已经叠加在背景图片上的图片,你可以看到边缘的黑点:

这是提取绿色污点的代码:

image = cv2.imread('/content/frame_patch.jpg')

img_hsv=cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

#color boundaries [H, S, V]
lower = (44, 30, 10)
upper = (120, 255, 255)

# threshold on green color
thresh = cv2.inRange(img_hsv, lower, upper)

# get largest contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# draw
mask = np.zeros_like(image)
cv2.drawContours(mask, [big_contour], 0, (255,255,255), -1)

img_result = image.copy()
img_result[mask==0] = 0

背景图片:

为合并代码

您可以尝试的一件事是在 Python/OpenCV 中使用形态学来关闭和侵蚀您的脱粒图像。

请注意,我将其绘制在灰色背景上以查看黑色边框如何减少。如果需要,您可以返回黑色背景以进行适当的后续处理。

输入:

import cv2
import numpy as np

image = cv2.imread('stain.png')

img_hsv=cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

#color boundaries [H, S, V]
lower = (44, 30, 10)
upper = (120, 255, 255)

# threshold on green color
thresh = cv2.inRange(img_hsv, lower, upper)

# use morphology to (optionally close and then) erode the thresh image
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel)

# get largest contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# draw
mask = np.zeros_like(image)
cv2.drawContours(mask, [big_contour], 0, (255,255,255), -1)

img_result = image.copy()
img_result[mask==0] = 128

# write results to disk
cv2.imwrite("stain_mask.png", mask)
cv2.imwrite("stain_result.png", img_result)

# display it
cv2.imshow("thresh", thresh)
cv2.imshow("result", img_result)
cv2.waitKey(0)

形态学脱粒:

灰色结果: