Search code examples
opencvimage-processinghierarchygpuimage

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?


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.