Question

I am having a some trouble with findContours in opencv. There is too much data in the output and I know that properly using the hierarchy is the key. Given the image below from the opencv docs:

Opencv Docs

I would like to draw all the contours except for 2a and 3a. What is the proper way to traverse the hierarchy and findContour arguments to achieve that result?

Was it helpful?

Solution

Contours found by findContours function has direction. Objects are counter clockwise, and holes are clockwise. So if you check signed area of each contour, you will know whether this is a hole or not by its sign. Signed area of contour can be calculated in following way:

contourArea(contour, true);

Of course using hierarchy is a good approach as well but I think this approach is simpler to understand and implement.

OTHER TIPS

Adding to what @Michael Burdinov said, returns +ve and -ve values for objects or holdes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top