首页 文章

OpenCV 2.4.4中的轮廓/连接组件

提问于
浏览
2

我目前正在制作一个有很多检测到的轮廓的图像 . 我的目标是缩小轮廓的数量,最终只有我正在寻找的轮廓 . 为此,我根据区域和边界框进行了一系列测试 .

现在我在每个步骤之后都做了 drawContours 的轮廓,我要保留后跟 findContours .

My problem is 我只想做一次 findContours 然后只是擦掉我不想要的轮廓,这可能吗?

目前的方式:

Mat src;
Mat BW;
src = imread("img.bmp", 0);
if( src.channels() > 1)
{
  cvtColor(src, src, CV_BGR2GRAY);
}

threshold(src, BW, 100, 255, CV_THRESH_OTSU);
imshow( "Tresh", BW );

Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);
Mat dstP = Mat::zeros(src.rows, src.cols, CV_8UC3);
Mat dst1 = Mat::zeros(src.rows, src.cols, CV_8UC3);
Mat dst2 = Mat::zeros(src.rows, src.cols, CV_8UC3);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

findContours( BW, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
for( int i = 0; i < contours.size(); i++ )
{
    Scalar color( rand()&255, rand()&255, rand()&255 );
    drawContours( dst, contours, i, color, 2/*CV_FILLED*/, 8, hierarchy );
}

/// Test on area  ******************************************************************
for( int i = 0; i < contours.size(); i++ )
{
    if ( contourArea(contours[i], false) > 100 && contourArea(contours[i], false) < 200000)
    {
        Scalar color( rand()&255, rand()&255, rand()&255 );
        drawContours( dst1, contours, i, color, CV_FILLED, 8, hierarchy );
    }
}

/// Next test **********************************************************************
    cvtColor(dst1, dstP, CV_BGR2GRAY);
    findContours( dstP, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

等等

通缉方式:

if ( contourArea(contours[i], false) < 100 && contourArea(contours[i], false) > 200000)
{
    contours.erase(i); // Doesn't work
}

现在有人如何擦除那些轮廓?

PS:我不关心内部轮廓,我希望他们都通过我的测试 .


EDIT ,解决方案(由limonana指出)是: contours.erase(contours.begin()+i);

1 回答

相关问题