Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

remove small object faster

Hello all !

In my project I have a function that find contour within an image and if this contour is too small then the contour is set to the color of the background in other words I remove it. here is the function

Mat removeTinyVolume(Mat input, int area, Scalar color)
{
// we draw to the color of the background
Mat output = input.clone();
vector<vector<Point>> contours;
findContours(input, contours, RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

// cout << "contours : " << contours.size() << endl;

for (int i = 0; i < contours.size(); i++)
{
    if (contourArea(contours[i]) < area)
    {
        drawContours(output, contours, i, color, -1, 8);
    }
}
return output;
}

It accurate I'm happy but it is really slow so I was wondering if you guys have some tips&ticks for that ? Thank you a lot ;) By the way this function is part of an open source project that you can find in comments.