Missing region when used findcontours

asked 2019-12-14 15:23:03 -0500

essamzaky gravatar image

updated 2019-12-15 00:52:15 -0500

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

edit retag flag offensive close merge delete

Comments

1

I don't understand your question. In doc RETR_EXTERNAL means retrieves only the extreme outer contours.

this pixel is not external contour. if you want to find its contour then use RETR_LIST

LBerger gravatar imageLBerger ( 2019-12-15 03:40:26 -0500 )edit
1

Thanks @LBerger , i was thinking that RETR_EXTERNAL working as RETR_CCOMP , but RETR_EXTERNAL retrieves only the outer contour , but it seems it's designed to ignore also any included regions

essamzaky gravatar imageessamzaky ( 2019-12-15 04:21:43 -0500 )edit