Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

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


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.

  • Assaf: Hi, I have 3 questions regarding the following sentence: “Rather than using the power spectrum from a...
  • Steve: Cara—I do not understand your question. Can you clarify it?
  • Cara Schiek: Hi. How could I do this same thing with image vales within the patches? cara
  • Steve: Vincent—OK, thanks for the information.
  • Vincent: My only data point on multithreading is that the Performance tab of the Task Manager shows increased CPU...
  • Steve: Vincent—Thanks for giving it a try and reporting back. I’m a bit skeptical that multithreading...
  • Vincent: Oops numbers were wrong. Data set was 450MB large so the numbers are: Results: ImageJ alone =< 5 s (or 90...
  • Vincent: Steve- I just had a quick run at the new imread.m patch. It’s much faster than the previous version...
  • Steve: Erik—Also, separability of the kernel provides no speed benefit in FFT-based implementations....
  • Steve: Erik—Good questions. Remember that, practically speaking, when we filter a 2-D signal with a 1-D filter...

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

Related Topics