【问题标题】:Contours / Connected components in OpenCV 2.4.4OpenCV 2.4.4 中的轮廓/连接组件
【发布时间】:2013-03-06 09:59:43
【问题描述】:

我目前正在处理具有大量检测到的轮廓的图像。 我的目标是缩小轮廓的数量,最终只得到我正在寻找的轮廓。 为此,我根据区域和边界框进行了一系列测试。

现在我在每一步之后为我想要保留的轮廓添加一个drawContours,然后是一个findContours

我的问题是我只想做一次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 : 我不关心内部轮廓,我希望它们都通过我的测试。


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

【问题讨论】:

  • 也许你可以创建一个新的国家结构并继续添加那些通过你的测试。
  • 为什么不起作用?其余数据保存在哪里?

标签: c++ opencv stdvector mat opencv-contour


【解决方案1】:

可能是因为您没有按照应有的方式使用擦除。它应该得到一个迭代器。 http://www.cplusplus.com/reference/vector/vector/erase/

【讨论】:

  • 谢谢,我实际上就在这个页面上,但我不习惯这个。适用于 contours.erase(contours.begin()+i);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 2023-03-21
  • 2011-08-27
  • 2020-06-14
相关资源
最近更新 更多