OpenCV 2.4.4中的轮廓/连接组件

问题描述:

我目前正在处理大量检测到轮廓的图像。 我的目标是缩小轮廓的数量,最终只有我正在寻找的轮廓。为此我进行了一系列基于面积和边界框的测试。OpenCV 2.4.4中的轮廓/连接组件

现在我在每一步之后都会执行一个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);

+0

也许你可以创建一个新的countour结构,并不断添加那些通过你的测试。 – 2013-03-06 10:23:31

+0

它为什么不起作用?其余数据在哪里保存? – 2013-03-06 12:15:28

也许那是因为你没有,你应该使用擦除。它应该得到一个迭代器。 http://www.cplusplus.com/reference/vector/vector/erase/

+0

谢谢,我其实是在这个页面上,但我不习惯这个。 适用于轮廓.erase(contours.begin()+ i); – Thom 2013-03-06 15:48:20