Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Missing region when used findcontours

While i was playing to answer the following question link text i notices that there is missing region , the contour of this region will not be catched it seems it's a bug .

this issue happen when RETR_EXTERNAL is passed to findcontours here its the used code

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_EXTERNAL, 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

here the image C:\fakepath\BiggestRegion.jpg and here is the missing region image description

i'm using OpenCV 4.1

Missing region when used findcontours

While i was playing to answer the following question link text i notices that there is missing region , the contour of this region will not be catched it seems it's a bug .

this issue happen when RETR_EXTERNAL is passed to findcontours here its the used code

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_EXTERNAL, 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

here the image C:\fakepath\BiggestRegion.jpgC:\fakepath\BiggestRegion.jpg and here is the missing region image description

i'm using OpenCV 4.1

Missing region when used findcontours

While i was playing to answer the following question link text i notices that there is missing region , the contour of this region will not be catched it seems it's a bug .

this issue happen when RETR_EXTERNAL is passed to findcontours here its the used code

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_EXTERNAL, 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

here the image C:\fakepath\BiggestRegion.jpg and here is the missing region image description

i'm using OpenCV 4.1