Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

besides @LorenaGdL 's approach what you could do in a simpler way is after you have your binary image apply floodFill() with a mask check the following code:

Mat bin;
threshold(gray_image, bin, 50, 255, THRESH_BINARY | THRESH_OTSU);

// A image with size greater than the present object is created, it is needed from floodFill()
cv::Mat mask = cv::Mat::zeros(src.rows + 2, src.cols + 2, CV_8U);

cv::floodFill(bin, mask, cv::Point(0,0), 255, 0, cv::Scalar(), cv::Scalar(), 4 + (255 << 8) + cv::FLOODFILL_MASK_ONLY);
//NOTE Since the mask is larger than the filled image, a pixel  (x, y) in image corresponds to the pixel (x+1, y+1) in the mask .

//remove the extra lines added earlier in the initialization of the mask if you want "optional"
Mat dst;
mask(Range(1, mask.rows - 1), Range(1, mask.cols-1)).copyTo(dst);

Result:

image description

besides @LorenaGdL 's approach what you could do in a simpler way is after you have your binary image apply floodFill() with a mask check the following code:

Mat bin;
threshold(gray_image, bin, 50, 255, THRESH_BINARY | THRESH_OTSU);

// A image with size greater than the present object is created, it is needed from floodFill()
cv::Mat mask = cv::Mat::zeros(src.rows + 2, src.cols + 2, CV_8U);

cv::floodFill(bin, mask, cv::Point(0,0), 255, 0, cv::Scalar(), cv::Scalar(), 4 + (255 << 8) + cv::FLOODFILL_MASK_ONLY);
//NOTE Since the mask is larger than the filled image, a pixel  (x, y) in image corresponds to the pixel (x+1, y+1) in the mask .

//remove the extra lines rows/cols added earlier in the initialization of the mask mask, if you want of course it is just "optional"
Mat dst;
mask(Range(1, mask.rows - 1), Range(1, mask.cols-1)).copyTo(dst);

Result:

image description

besides @LorenaGdL 's approach what you could do in a simpler way is after you have your binary image apply floodFill() floodFill() with a mask check the following code:

Mat bin;
threshold(gray_image, bin, 50, 255, THRESH_BINARY | THRESH_OTSU);

// A image with size greater than the present object is created, it is needed from floodFill()
cv::Mat mask = cv::Mat::zeros(src.rows + 2, src.cols + 2, CV_8U);

cv::floodFill(bin, mask, cv::Point(0,0), 255, 0, cv::Scalar(), cv::Scalar(), 4 + (255 << 8) + cv::FLOODFILL_MASK_ONLY);
//NOTE Since the mask is larger than the filled image, a pixel  (x, y) in image corresponds to the pixel (x+1, y+1) in the mask .

//remove the extra rows/cols added earlier in the initialization of the mask, if you want of course it is just "optional"
Mat dst;
mask(Range(1, mask.rows - 1), Range(1, mask.cols-1)).copyTo(dst);

Result:

image description