OpenCV drawContours奇怪的行为

时间:2015-07-24 14:53:45

标签: c++ opencv c++11 opencv3.0 areas

我有一个要绘制的轮廓列表。其中一些轮廓相互交叉。

当我想用OpenCV绘制它们时,我只需使用cv::drawContours函数。

然而,这种行为很奇怪。

以下是官方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.

所以,从文档来看,如果我想绘制所有区域,填充黑色,我只需要这样做:

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

然而,这给了我以下输出:

enter image description here

在这里,我们可以清楚地看到我的所有区域都没有填充黑色。

但如果我遍历我的区域列表并为每个区域调用cv::drawContours

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);

我获得了与第一个完全不同的良好输出:

enter image description here

我是否错过了文档中的内容?有人可以向我解释一下cv::drawContours的行为吗?对于所有区域称之为一个并且每次为每个区域调用它有什么不同?

2 个答案:

答案 0 :(得分:0)

我认为当您将contourIdx作为否定时,我认为drawContour函数只会绘制轮廓而不会像CV_FILLED所示那样填充。通过显式循环每个轮廓,您将获得结果。

答案 1 :(得分:0)

我终于在opencv github存储库上打开了一个问题:https://github.com/Itseez/opencv/issues/5256