Search code examples
c++opencvc++11opencv3.0areas

OpenCV drawContours strange behavior


I have a list of contours to draw. Some of these contours intersect themselves.

When I want to draw them with OpenCV, I simply use the cv::drawContours function.

However, the behavior is quite strange.

Here is a quote of the official documentation

C++: void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar& color, int thickness=1, int lineType=8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point() )
Parameters: 
contourIdx – Parameter indicating a contour to draw. If it is negative, all the contours are drawn.

So, from the documentation, if I want to draw all my areas, filled in black, I just have to do:

cv::drawContours(this->mask.raw,
                 this->areas, -1,
                 cv::Scalar(0,0,0),
                 cv::FILLED);

However, this gives me the following ouput:

enter image description here

Here, we can clearly see that all my areas are NOT filled in black.

But if I loop over my areas list and call cv::drawContours for each area:

unsigned int i = 0;
for (const auto& area : this->areas)
  cv::drawContours(this->mask.raw,
                   this->areas, i++,
                   cv::Scalar(0,0,0),
                   cv::FILLED);

I got the good ouput which is quite different from the first one:

enter image description here

Have I missed something from the documentation? Could someone explain me the behavior of cv::drawContours and what is the different of calling it one for all areas and calling it each time for each area?


Solution

  • I've finally opened an issue on the opencv github repository: https://github.com/Itseez/opencv/issues/5256.