如何在OpenCV中获得单独的轮廓(并填充它们)? [英] How to get separate contours (and fill them) in OpenCV?

查看:1020
本文介绍了如何在OpenCV中获得单独的轮廓(并填充它们)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to separate the contours of an image (in order to find uniform regions) so I applied cvCanny and then cvFindContours, then I use the following code to draw 1 contour each time I press a key:

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

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

But in the first iteration it draws ALL the contours, in the second it draws ALL but one, the third draws all but two, and so on.

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

And if I use the cvFillConvexPoly function it fills most of the screen (although as I wrote this I realized a convex polygon won't work for me, I need to fill just the insideof the contour)

那么,如何在for的每次迭代中只取一个轮廓,而不是所有其余轮廓?

So, how can I take just 1 contour on each iteration of the for, instead of all the remaining contours?

谢谢.

推荐答案

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

You need to change the last parameter you are passing to the function, which is currently 100, to either 0 or a negative value, depending on whether you want to draw the children.

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

According to the documentation (http://opencv.willowgarage.com/documentation/drawing_functions.html#drawcontours), the function has the following signature:

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

根据同一文档,max_level具有以下目的(最适用的部分为粗体):

From the same docs, max_level has the following purpose (most applicable part is in bold):

max_level –绘制轮廓的最大水平. 如果为0,则仅轮廓为 绘制.如果为1,则轮廓及其后的所有轮廓都在同一位置 水平绘制.如果为2,则所有轮廓都遵循,所有轮廓都为1 绘制轮廓下方的水平,依此类推. 如果值是 负数,此功能在之后不绘制轮廓 轮廓,但绘制子轮廓的轮廓直到 $ | \ texttt {max_ level} | -1 $级别.

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参数使用负值:

To fill the contour, use a negative value for the thickness parameter:

厚度–绘制等高线的粗度.如果是 负数(例如= CV_FILLED),则绘制轮廓内部.

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

这篇关于如何在OpenCV中获得单独的轮廓(并填充它们)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆