'how to consider only the white region on the image as contour

I have a binary image, from which I need to consider only the white regions as contours but it also takes black region which is surrounded by white part as contour. I don't want to use contour area, can we ignore the black regions while finding contours? Here is the binary image and the orange color marked is also considered as contour, so do not want the black region surrounded with white to be considered as contour.

enter image description here enter image description here

Contour image is:

enter image description here

My contouring code:

//contouring
    vector<vector<Point> > contours;
    findContours(img, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
    vector<vector<Point> > contours_poly(contours.size());
    vector<Rect> boundRect(contours.size());
    vector<Point2f>centers(contours.size());
    vector<float>radius(contours.size());
    for (size_t i = 0; i < contours.size(); i++)
    {
        approxPolyDP(contours[i], contours_poly[i], 3, true);
        boundRect[i] = boundingRect(contours_poly[i]);
        minEnclosingCircle(contours_poly[i], centers[i], radius[i]);
    }
    Mat drawing = Mat::zeros(img.size(), CV_8UC3);
    
    for (size_t i = 0; i < contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
        
        drawContours(drawing, contours_poly, (int)i, color);
}


Solution 1:[1]

Your code snippets do not compose a minimal reproducible example (https://stackoverflow.com/help/minimal-reproducible-example). Therefore I could not run it for testing.

However - you might be able to achieve what you want by "playing" with the 2 last parameters of cv::findContours.

From opencv documentation of cv::findContours:

    mode    Contour retrieval mode, see [RetrievalModes][1]

    method  Contour approximation method, see [ContourApproximationModes][1]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wohlstad