Code to fill the holes in an image
1 view (last 30 days)
Show older comments
In my project, I want to fill holes which are 2 pixel thin.
To fill 2-pixel thin hollows, we use a process in two steps.
During the first step, the filling template(attached) is centered on each pixel p with value i = 0 in DM. The central pixel of the template is 0, while the eight pixels on the four arms of the cross are 1, and all other pixels are don’t care elements.
Any pixel p with value i = 0 in DM and having at least a pixel with value i = 0 in both two opposite horizontal or vertical arms of the cross is marked in parallel as candidate to be moved from the background to the foreground. Here DM is an image matrix.
I attached the mask.
Can anyone tell me how to do this process using MATLAB and provide code for this.

0 Comments
Answers (1)
Image Analyst
on 21 Feb 2016
Why not just use
binaryImage = imfill(binaryImage, 'holes');
Otherwise you can use conv2()
kernel = [0, 0, 1, 0, 0,...
0, 0, 1, 0, 0,...
1, 1, 0, 1, 1,...
0, 0, 1, 0, 0,...
0, 0, 1, 0, 0]
holeImage = conv2(double(binaryImage), kernel, 'same') == 4
% Fill them
binaryImage(holeImage) = true;
If the hole is in the first or second row or column next to an edge of the image, it won't get filled. You'd have to use the 'full' option to conv2() and then call imcrop(), so it's b it trickier
holeImage = conv2(double(binaryImage), kernel, 'full') == 4
% Fill them
binaryImage(holeImage) = true;
binaryImage = binaryImage(3:end-2, 3:end-2); % Crop
3 Comments
Image Analyst
on 22 Feb 2016
It was filled. The problem is that you do not have closed outer perimeters. You should maybe not use an edge detector but use a different approach. What does the ophthalmic literature say?
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!