查找 cv2.findContours() 的面积(Python、OpenCV)

Lin*_*ink 5 python opencv

我正在尝试查找/计算下一张图像中的轮廓区域:

轮廓

目标是删除您在图像中可以看到的所有点,因此斑点的轮廓面积小于我给出的值。

移动

我该如何设置这个?

这是我使用的代码...

import cv2

im = cv2.imread('source.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

img = cv2.drawContours(im, contours, -1, (0,255,0), 1)

cv2.imshow('contour',img)
cv2.waitKey(0)
cv2.imwrite('contour.png',img)
Run Code Online (Sandbox Code Playgroud)

...这是图像:

起源

谢谢

Mar*_*ans 9

您可以使用cv2.contourArea()来决定绘制哪一个:

for contour in contours:
    if cv2.contourArea(contour) < 80:
        cv2.drawContours(im, contour, -1, (255, 255, 255), 3)
Run Code Online (Sandbox Code Playgroud)

  • 使用“-1”作为宽度来填充它们。也许尝试做两次,即一次填充,一次填充“3”。您还可以尝试在阈值之前模糊图像。 (2认同)