Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate contours in opencv

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?

like image 521
Joe Andolina Avatar asked Feb 15 '23 02:02

Joe Andolina


2 Answers

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.

like image 140
Michael Burdinov Avatar answered Feb 27 '23 00:02

Michael Burdinov


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

like image 33
Yash K Avatar answered Feb 27 '23 00:02

Yash K