OpenCV 2.4.4中的轮廓/连接组件

时间:2013-03-06 09:59:43

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

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

现在我按照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
}

想要的方式:

contours.erase(contours.begin()+i);

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

PS:我不关心内部轮廓,我希望所有人都能通过我的测试。


编辑,解决方案(由limonana指出)是:{{1}}

1 个答案:

答案 0 :(得分:0)

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

相关问题