Ask Your Question
1

How to extract only top-level contours?

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

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

Comments

1

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

sammy gravatar imagesammy ( 2012-08-02 12:16:50 -0600 )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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

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

rics gravatar image

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

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

Question Tools

Stats

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

Seen: 7,159 times

Last updated: Jan 05 '16