私は現在、検出された輪郭がたくさんある画像に取り組んでいます。私の目標は、輪郭の数を絞り込んで、探しているものだけにすることです。そのために、エリアとバウンディング ボックスに基づいて一連のテストを行います。
今のところ、すべてのステップの後にdrawContours
、保持したい輪郭に対して a を実行し、その後に a が続き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 : 内側の輪郭は気にしません。それらすべてをテストに合格させたいと思っています。
EDIT、解決策(リモナナが指摘)は次のとおりです。contours.erase(contours.begin()+i);