Ask Your Question

How to extract only top-level contours?

asked 2012-08-02 10:06:33 -0500

Tru gravatar image

When using findContours to identify blobs, is there a way to ignore any contours present within another contour. I only need to count the number of contours that are the outermost using contours.size();

I looked into this and found the RETR_EXTERNAL parameter instead of the RETR_LIST, is this enough? Or do I have to do anything with the hierarchy Mat object (third parameter)?

edit retag flag offensive close merge delete



Using RETR_EXTERNAL gave you bad results? What have you tried so far?

sammy gravatar imagesammy ( 2012-08-02 12:16:50 -0500 )edit

Gave somewhat of a better answer (RETR_EXTERNAL). Was wondering whether it could be more refined...

Tru gravatar imageTru ( 2012-08-02 12:40:32 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-01-08 14:23:16 -0500

rics gravatar image

updated 2016-01-05 10:34:16 -0500

thdrksdfthmn gravatar image

I think that you and sammy have already found the answer. This is what the documentation says:

RETR_EXTERNAL retrieves only the extreme outer contours.

For hierarchy it states that:

Optional output vector, containing information about the image topology. It has as many elements as the number of contours. For each i-th contour contours[i] , the elements hierarchy[i] [0] , hiearchy[i][1] ,hiearchy[i][2] , and hiearchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level, the first child contour and the parent contour, respectively.

So in case of RETR_EXTERNAL you can use hierarchy[i][0] and hierarchy[i][1] for "neighbouring" contours at the top level but hierarchy[i][2] and hierarchy[i][3] will always be -1.

You can see this link for better understanding the flags of the function

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2012-08-02 10:06:33 -0500

Seen: 4,129 times

Last updated: Jan 05 '16