Ask Your Question
0

C++ OpenCV Eliminate smaller Contours

asked 2013-03-26 13:11:38 -0600

Tomazi gravatar image

I am developing a OpenCV project.

I am currently working on detecting contours of particular ROI (Region Of Interest). What I want to achieve is to eliminate all the smaller contours in other words I do not want these smaller contours to be drown at all.

So Far if I have coded this algorithm to do this job:

vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(mBlur, contours, hierarchy, CV_RETR_EXTERNAL,  CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
    //----------------------------------------------------------------------------->
    //Contours Vectors
    vector<vector<Point> > contours_poly(contours.size());
    vector<Rect> boundRect (contours.size());
    vector<Moments> ContArea(contours.size());

    //----------------------------------------------------------------------------->

    //Detecting Contours
    for( int i = 0; i < contours.size(); i++ )
    {  
        ContArea[i] = moments(contours[i], false);
    }

    vector<Point2f> ContCenter(contours.size());
    for(int i = 0; i < contours.size(); i++)
    {
        ContCenter[i] = Point2f(ContArea[i].m10/ContArea[i].m00, ContArea[i].m01/ContArea[i].m00);  
    }

    Mat drawing = Mat::zeros( mBlur.size(), CV_8UC3 );
    for(int i = 0; i < contours.size(); i++)
    {
        Scalar color = Scalar(255,255,255);
        drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
        fillPoly(drawing, contours, Scalar(255,0,0));
    }

    for(int i = 0; i < contours.size(); i++)
    {
       printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", i, ContArea[i].m00, contourArea(contours[i]), arcLength( contours[i], true ) );

       if(ContArea[i].m00 > 1000)
       {
           Scalar color = Scalar(0,255,0);
           drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
           circle( drawing, ContCenter[i], 4, color, -1, 8, 0 );
       }

    }

I have used this tutorial to compute the above code: [Link To Tutorial] (http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/moments/moments.html)

OK so everything seems to work well contours with are over 1000 are drawn and highlighted but the contours that are less then 1000 are also drawn but are not highlighted.

I want these smaller contours that are not highlighted not to be drown at all is it possible....? using the above code...?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-03-26 13:31:47 -0600

berak gravatar image

updated 2013-03-26 13:34:43 -0600

you're drawing your contours 2 x , the first time all of them, the next only the big ones.

just remove (or comment) the first loop:

// not needed:
//for(int i = 0; i < contours.size(); i++)
//{
//    Scalar color = Scalar(255,255,255);
//    drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
//    fillPoly(drawing, contours, Scalar(255,0,0));
//}
edit flag offensive delete link more

Comments

ok this seems to work but......following the tutorial it is how the code there was written so why in my instance it wouldn't work.....?

Tomazi gravatar imageTomazi ( 2013-03-26 16:04:36 -0600 )edit

Question Tools

Stats

Asked: 2013-03-26 13:11:38 -0600

Seen: 7,002 times

Last updated: Mar 26 '13