首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中删除小的黑色轮廓/不需要的轮廓

在Python中删除小的黑色轮廓/不需要的轮廓
EN

Stack Overflow用户
提问于 2020-11-22 16:12:50
回答 1查看 306关注 0票数 1

我已经研究了一整天了,我们如何去除红色圆圈中的小背景噪音?我需要它在像这样的其他图片样本上工作。

我使用的想法是findContours,然后添加一个带有小于特定区域的所有小黑色噪声的掩模(试错法)。

Removing noise in red ellipse

代码语言:javascript
复制
image = cv2.imread("11_Image_after_noise_removal.png")

# copy image
img = image.copy()
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0, cv2.THRESH_BINARY)
thresh = 255 - thresh
# Use cv2.CCOMP for two level hierarchy
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP,
                                       cv2.CHAIN_APPROX_SIMPLE)  # Use cv2.CCOMP for two level hierarchy
cv2.drawContours(img, contours, -1, (0, 255, 0), 1)

cv2.imshow("First detection", img)
# loop through the contours
for i, cnt in enumerate(contours):
    # if the contour has no other contours inside of it
    if hierarchy[0][i][3] != -1:  # basically look for holes
        # if the size of the contour is less than a threshold (noise)
        if cv2.contourArea(cnt) < 70:
            # Fill the holes in the original image
            cv2.drawContours(img, [cnt], 0, (0, 0, 0), -1)
            # display result
# Visualize the image after the Otsu's method application

cv2.imshow("Image after noise removal", img)

cv2.waitKey(0)
cv2.destroyAllWindows().destroyAllWindows()
EN

回答 1

Stack Overflow用户

发布于 2020-11-22 22:47:24

您可以使用area = cv.contourArea(cnt)检查轮廓区域,如果它低于某个阈值,则忽略它。

下面是OpenCV文档:https://docs.opencv.org/4.3.0/dd/d49/tutorial_py_contour_features.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64951935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档