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 :
The result/output image which I want to obtain :
but when I applied the above code, the result was
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.
Hi, take the biggest contour instead.
Yeah. I took the biggest contour. But when I used drawContours, it looked like the last picture