Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

C++ OpenCV Eliminate smaller Contours

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...?