gpt4 book ai didi

c++ - OpenCV drawContours 奇怪的行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:49:33 28 4
gpt4 key购买 nike

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

当我想用 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 的行为,以及对所有区域调用它和每次对每个区域调用它有什么不同?

最佳答案

我认为,当您将 contourIdx 作为 Negative 传递时,我认为 drawContour 函数只是绘制轮廓而不填充,正如您用 CV_FILLED 指示的那样.通过显式循环遍历每个轮廓,您将获得所得到的结果。

关于c++ - OpenCV drawContours 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31613672/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com