Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to eliminate small contours in a binary image?

I am currently working on image processing project. I am using Opencv 3.x.x with VC++.

My code:

Mat result;
vector<vector<Point> >contours;
vector<Vec4i>hierarchy;
int savedContour = -1;
double maxArea = 0.0;
// Find the largest contour
findContours(binarayImage, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point());
for (int i = 0; i< contours.size(); i++)
{
    double area = contourArea(contours[i]);
    if (area > maxArea)
    {
        maxArea = area;
        savedContour = i;
    }
}
// Create mask
drawContours(result, contours, savedContour, Scalar(255), CV_FILLED, 8);

The binary image which I obtained :

image description

The result/output image which I want to obtain :

image description

but when I applied the above code, the result was

image description

did not look like I expected.

Help me! Thank you!

How to eliminate small contours in a binary image?

I am currently working on image processing project. I am using Opencv 3.x.x with VC++.

My code:

Mat result;
vector<vector<Point> >contours;
vector<Vec4i>hierarchy;
int savedContour = -1;
double maxArea = 0.0;
// Find the largest contour
findContours(binarayImage, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point());
for (int i = 0; i< contours.size(); i++)
{
    double area = contourArea(contours[i]);
    if (area > maxArea)
    {
        maxArea = area;
        savedContour = i;
    }
}
// Create mask
drawContours(result, contours, savedContour, Scalar(255), CV_FILLED, 8);

The binary image which I obtained :

image description

The result/output image which I want to obtain :

image description

but when I applied the above code, the result was

image description

did not look like I expected.

Help me! Thank you!

P/s: I know this problem is caused by drawContours. But I have no way to fix it.