Steve on Image Processing

August 5th, 2008

Filling small holes

A MATLAB user recently asked in the MATLAB newsgroup how to fill "small" holes in a binary image. The function imfill can be used to fill all holes, but this user only wanted to fill holes having an area smaller than some threshold.

That's an interesting question. It can be done using a combination of imfill, bwareaopen, and MATLAB logical operators. Here's how.

Step 1: Fill all holes using imfill:

original = imread('circbw.tif');
imshow(original)
filled = imfill(original, 'holes');
imshow(filled)
title('All holes filled')

Step 2: Identify the hole pixels using logical operators:

holes = filled & ~original;
imshow(holes)
title('Hole pixels identified')

Step 3: Use bwareaopen on the holes image to eliminate small holes:

bigholes = bwareaopen(holes, 200);
imshow(bigholes)
title('Only the big holes')

Step 4: Use logical operators to identify small holes:

smallholes = holes & ~bigholes;
imshow(smallholes)
title('Only the small holes')

Step 5: Use a logical operator to fill in the small holes in the original image:

new = original | smallholes;
imshow(new)
title('Small holes filled')

All done!


Get the MATLAB code

Published with MATLAB® 7.6

2 Responses to “Filling small holes”

  1. Sven replied on :

    Hi Steve and all,

    Just a little extra in case you want to fill *midsized* holes. The following fits in at Step 3 above, and will fill holes between 80 and 300 pixels in area.

    L_candidate_holes = bwlabel(holes);
    stats = regionprops(L_candidate_holes, ‘Area’);
    idx = find([stats.Area]>80 & [stats.Area]<300);
    midsized_holes = ismember(L_candidate_holes,idx);

    Just thought I’d share since I’ve been using this quite a bit recently. Of course, you could add more criteria to the regionprops call, and then select the holes based on more criteria too.

    Cheers,
    Sven.

  2. Steve replied on :

    Sven—Thanks for the additional tips.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Sana: hi steve, could you explain to me how i would be able to use the dir function, to do a loop through a directory...
  • Nishtha: Sir, I have preprocessed the image in following steps: [1] adaptive histogram equalization [2] thresholding...
  • Kristof: I also strongly support the idea. I have just recently bumped into the problem that im2single was not...
  • Steve: David—I’ m glad you found it useful!
  • David Lalejini: I found your example very useful for finding connected nodes in a large set of input pairs. I start...
  • tommy: Dear Steve, I have a question,please if you are kind to help me regarding the accumulator array dimensions of...
  • Steve: Abc—I don’t know how to distinguish the faces. You might try posting your question in the MATLAB...
  • Manju: well if we have a few ovals within each other like in a cell how do we measure the distance from the center...
  • Steve: Manju—What do you mean? How is each region defined?
  • Manju: if we have 2-3 regions within each other how do we measure the regions of each one?

These postings are the author's and don't necessarily represent the opinions of The MathWorks.