[英]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 中使用形态学来关闭和侵蚀您的 thresh 图像。
请注意,我将其绘制在灰色背景上以查看黑色边框是如何减少的。 如果需要,您可以 go 返回黑色背景以进行适当的后续处理。
输入:
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)
形态学脱粒:
灰色结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.