gpt4 book ai didi

c - 如何在 OpenCV 中获取单独的轮廓(并填充它们)?

转载 作者:太空狗 更新时间:2023-10-29 17:07:17 27 4
gpt4 key购买 nike

我试图分离图像的轮廓(为了找到统一的区域)所以我应用了 cvCanny,然后是 cvFindContours,然后我使用以下代码在每次按下一个键时绘制 1 个轮廓:

for( ; contours2 != 0; contours2 = contours2->h_next ){
cvSet(img6, cvScalar(0,0,0));
CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
cvDrawContours(img6, contours2, color, cvScalarAll(255), 100);
//cvFillConvexPoly(img6,(CvPoint *)contours2,sizeof (contours2),color);
area=cvContourArea(contours2);
cvShowImage("3",img6);
printf(" %d", area);
cvWaitKey();
}

但是在第一次迭代中它绘制了所有的轮廓,在第二次它绘制了除一个之外的所有轮廓,第三次绘制了除两个之外的所有轮廓,依此类推。

如果我使用 cvFillConvexPoly 函数,它会填充大部分屏幕(尽管在我写这篇文章时我意识到凸多边形对我不起作用,我只需要填充轮廓的内部)

那么,我怎样才能在 for 的每次迭代中只取 1 个轮廓,而不是所有剩余的轮廓?

谢谢。

最佳答案

您需要将传递给该函数的最后一个参数(当前为 100)更改为 0 或负值,具体取决于您是否要画 children 。

根据文档(http://opencv.willowgarage.com/documentation/drawing_functions.html#drawcontours),该函数具有以下签名:

void cvDrawContours(CvArr *img, CvSeq* contour, CvScalar external_color,
CvScalar hole_color, int max_level, int thickness=1, int lineType=8)

来自相同的文档,max_level 具有以下目的(最适用的部分以粗体显示):

max_level – Maximal level for drawn contours. If 0, only contour is drawn. If 1, the contour and all contours following it on the same level are drawn. If 2, all contours following and all contours one level below the contours are drawn, and so forth. If the value is negative, the function does not draw the contours following after contour but draws the child contours of contour up to the $|\texttt{max_ level}|-1$ level.

编辑:

要填充轮廓,请为 thickness 参数使用负值:

thickness – Thickness of lines the contours are drawn with. If it is negative (For example, =CV_FILLED), the contour interiors are drawn.

关于c - 如何在 OpenCV 中获取单独的轮廓(并填充它们)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15995092/

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