Link Link -3 years ago 318
Python Question

Find area of cv2.findContours() (Python, OpenCV)

I am trying to find/calculate the contour area in the next image:


The goal is to delete all the dots you can see in image, so the blobs contoured with an area smaller than a value I give.


How can I set this ?

This is the code I used...

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)


...and this is the source image:


Thank you

Answer Source

You can use cv2.contourArea() to decide which to draw:

for contour in contours:
    if cv2.contourArea(contour) < 80:
        cv2.drawContours(im, contour, -1, (255, 255, 255), 3)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download