Let's say I have the following binary image created in the output of cv::watershed() :

Now I want to find and fill out the contours, so I can separate the corresponding objects from the background in the original image (which was segmented by the watershed function).
To segment the image and find outlines, I use the following code:
cv::Mat bgr = cv::imread("test.png"); // Some function that provides the rough outline for the segmented regions. cv::Mat markers = find_markers(bgr); cv::watershed(bgr, markers); cv::Mat_<bool> boundaries(bgr.size()); for (int i = 0; i < bgr.rows; i++) { for (int j = 0; j < bgr.cols; j++) { boundaries.at<bool>(i, j) = (markers.at<int>(i, j) == -1); } } std::vector<std::vector<cv::Point> > contours; std::vector<cv::Vec4i> hierarchy; cv::findContours( boundaries, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
So far so good. However, if I go through the paths above to cv::drawContours() , as shown below:
cv::Mat regions(bgr.size(), CV_32S); cv::drawContours( regions, contours, -1, cv::Scalar::all(255), CV_FILLED, 8, hierarchy, INT_MAX );
This is what I get:

The leftmost contour remained open on cv::findContours() , and as a result, it was not filled by cv::drawContours() .
Now I know that this is a consequence of cv::findContours() clipping a 1-pixel border around the image (as indicated in the documentation ), but what then? It seems like a terrible waste to drop the outline just because it happened to clear the border of the image. And anyway, how can I find which circuit falls into this category? cv::isContourConvex() not a solution in this case; the region may be concave , but "closed" and therefore does not have this problem.
Edit: On the assumption of duplicating pixels from borders. The problem is that my marking function also paints all the pixels in the "background", i.e. Those regions that, I am sure, are not part of any object:

This causes the border to be drawn around the output. If I somehow avoid cv::findContours() to wring this border:

The border for the background is combined with this leftmost object:

The result is a beautiful field filled with white.