Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I will write some other methods to find the largest contour the first method use flooFill to eliminate small regions the second method uses connected componet parameter ( RETR_CCOMP) in findcontours then path the biggest contour to drawContours C:\fakepath\biggest.pngwith hierarchy

Mat gray = imread(".\\temp\\Image.jpg", IMREAD_GRAYSCALE);
gray = (gray > 200) * 255;          
Mat binarayImage = (gray > 200) * 255;  
vector<vector<Point> >contours;
vector<Vec4i>hierarchy;
int savedContour = -1;
double maxArea = 0.0;   
Mat result1 = binarayImage.clone();
// Find the largest contour
// findContours will "eat" the input image, so clone the binary Mat, we need it later:  
Rect rect;
findContours(binarayImage.clone(), contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_NONE, Point());
for (int i = 0; i < contours.size(); i++)
{
    //ignore holes the following condition for  
    if (hierarchy[i][3] > -1)
        continue;
    double area = contourArea(contours[i]);
    if (area > maxArea)
    {
        if (savedContour >= 0)
            floodFill(result1, contours[savedContour][0], Scalar(0), &rect, 0, 0, 8);
        maxArea = area;
        savedContour = i;
    }
    else
        floodFill(result1, contours[i][0], Scalar(0), &rect, 0, 0, 8);
}

imwrite("c:\\temp\\biggest1.png", result1);//save method 1  
Mat result2 = Mat::zeros(binarayImage.size(), CV_8U);
drawContours(result2, contours, savedContour, Scalar(255), FILLED, 8,hierarchy);
imwrite("c:\\temp\\biggest2.png", result2);//save method 2

I will write some other methods to find the largest contour the first method use flooFill to eliminate small regions the second method uses connected componet parameter ( RETR_CCOMP) in findcontours then path the biggest contour to drawContours with hierarchy C:\fakepath\biggest.pngwith hierarchy

Mat gray = imread(".\\temp\\Image.jpg", IMREAD_GRAYSCALE);
gray = (gray > 200) * 255;          
Mat binarayImage = (gray > 200) * 255;  
vector<vector<Point> >contours;
vector<Vec4i>hierarchy;
int savedContour = -1;
double maxArea = 0.0;   
Mat result1 = binarayImage.clone();
// Find the largest contour
// findContours will "eat" the input image, so clone the binary Mat, we need it later:  
Rect rect;
findContours(binarayImage.clone(), contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_NONE, Point());
for (int i = 0; i < contours.size(); i++)
{
    //ignore holes the following condition for  
    if (hierarchy[i][3] > -1)
        continue;
    double area = contourArea(contours[i]);
    if (area > maxArea)
    {
        if (savedContour >= 0)
            floodFill(result1, contours[savedContour][0], Scalar(0), &rect, 0, 0, 8);
        maxArea = area;
        savedContour = i;
    }
    else
        floodFill(result1, contours[i][0], Scalar(0), &rect, 0, 0, 8);
}

imwrite("c:\\temp\\biggest1.png", result1);//save method 1  
Mat result2 = Mat::zeros(binarayImage.size(), CV_8U);
drawContours(result2, contours, savedContour, Scalar(255), FILLED, 8,hierarchy);
imwrite("c:\\temp\\biggest2.png", result2);//save method 2

I will write some other methods to find the largest contour the first method use flooFill to eliminate small regions the second method uses connected componet parameter ( RETR_CCOMP) in findcontours then path the biggest contour to drawContours with hierarchy C:\fakepath\biggest.pngC:\fakepath\biggest.png

Mat gray = imread(".\\temp\\Image.jpg", IMREAD_GRAYSCALE);
gray = (gray > 200) * 255;          
Mat binarayImage = (gray > 200) * 255;  
vector<vector<Point> >contours;
vector<Vec4i>hierarchy;
int savedContour = -1;
double maxArea = 0.0;   
Mat result1 = binarayImage.clone();
// Find the largest contour
// findContours will "eat" the input image, so clone the binary Mat, we need it later:  
Rect rect;
findContours(binarayImage.clone(), contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_NONE, Point());
for (int i = 0; i < contours.size(); i++)
{
    //ignore holes the following condition for  
    if (hierarchy[i][3] > -1)
        continue;
    double area = contourArea(contours[i]);
    if (area > maxArea)
    {
        if (savedContour >= 0)
            floodFill(result1, contours[savedContour][0], Scalar(0), &rect, 0, 0, 8);
        maxArea = area;
        savedContour = i;
    }
    else
        floodFill(result1, contours[i][0], Scalar(0), &rect, 0, 0, 8);
}

imwrite("c:\\temp\\biggest1.png", result1);//save method 1  
Mat result2 = Mat::zeros(binarayImage.size(), CV_8U);
drawContours(result2, contours, savedContour, Scalar(255), FILLED, 8,hierarchy);
imwrite("c:\\temp\\biggest2.png", result2);//save method 2

I will write some other methods to find the largest contour the first method use flooFill to eliminate small regions the second method uses connected componet parameter ( RETR_CCOMP) in findcontours then path pass the biggest contour to drawContours with hierarchy C:\fakepath\biggest.png

Mat gray = imread(".\\temp\\Image.jpg", IMREAD_GRAYSCALE);
gray = (gray > 200) * 255;          
Mat binarayImage = (gray > 200) * 255;  
vector<vector<Point> >contours;
vector<Vec4i>hierarchy;
int savedContour = -1;
double maxArea = 0.0;   
Mat result1 = binarayImage.clone();
// Find the largest contour
// findContours will "eat" the input image, so clone the binary Mat, we need it later:  
Rect rect;
findContours(binarayImage.clone(), contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_NONE, Point());
for (int i = 0; i < contours.size(); i++)
{
    //ignore holes the following condition for  
    if (hierarchy[i][3] > -1)
        continue;
    double area = contourArea(contours[i]);
    if (area > maxArea)
    {
        if (savedContour >= 0)
            floodFill(result1, contours[savedContour][0], Scalar(0), &rect, 0, 0, 8);
        maxArea = area;
        savedContour = i;
    }
    else
        floodFill(result1, contours[i][0], Scalar(0), &rect, 0, 0, 8);
}

imwrite("c:\\temp\\biggest1.png", result1);//save method 1  
Mat result2 = Mat::zeros(binarayImage.size(), CV_8U);
drawContours(result2, contours, savedContour, Scalar(255), FILLED, 8,hierarchy);
imwrite("c:\\temp\\biggest2.png", result2);//save method 2