How to fill a large hole?
Show older comments
The imfill function doesn't fill the large hole. How can I fill it?
Or, more generally:
How can I separate the image from the background?
Using a very large 'disk' might work to fill the hole, but it would also make the subject too large.
Any suggestions?
Thank you!
I = imread("sample_image.png");
BW = imbinarize(I);
se = strel('disk',40);
closeBW = imclose(BW,se);
BW2 = imfill(closeBW,'holes');
figure
imshowpair(I,BW2,'montage')

Accepted Answer
More Answers (1)
Walter Roberson
on 13 Jan 2025
Edited: Walter Roberson
on 13 Jan 2025
The provided JPEG image is RGB, not gray.
The provided JPEG image has a white border around it.
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1823171/image.jpeg');
I = rgb2gray(I);
I = imclearborder(I);
BW = imbinarize(I);
se = strel('disk',40);
closeBW = imclose(BW,se);
BW2 = imfill(closeBW,'holes');
imshow(BW2)
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


