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

Steve on Image Processing

July 26th, 2007

Filling holes in images

Didja know? You can fill holes, or pits, in grayscale images by using the Image Processing Toolbox function imfill.

The other day I was rereading the Tarboton paper on upslope area, trying to decide what to do next in my series on that topic. I noticed that the author described filling in "pits" in digital elevation models (DEMs) as a common preprocessing step. He outlined a method based on pixel flow directions, but didn't give details. I realized that this preprocessing step is very easy to do using the Image Processing Toolbox, but that many users might not know about this capability.

Let me first define a little more precisely what I mean by "pit" or "hole," and then I'll show you how to fill it in. It's a little easier to show in one dimension, so let's start there.

Here's a one-dimensional function:

t = linspace(0, 3*pi, 100);
y = t/2 + sin(t);
plot(t,y)

The plot above has a local minimum in its interior. This is a hole, or pit. If we are careful to define our connectivity appropriately for one dimension, we can use imfill with the 'holes' option to fill in the hole.

% Define a one-dimensional connectivity in the horizontal direction.
conn = [0 0 0; 1 1 1; 0 0 0];
y2 = imfill(y, conn, 'holes');

plot(t,y2)

The filled curve has this key property: From any interior point, you can now travel to a boundary minimum without ever going uphill. Here's what it looks for an image:

I = imread('tire.tif');
I2 = imfill(I,'holes');  % the default connectivity is fine
subplot(1,2,1)
imshow(I)
title('Original image')
subplot(1,2,2)
imshow(I2)
title('Filled image')

The hole-filling algorithm in imfill is based on morphological reconstruction (imreconstruct). If you are interested in the theoretical details, see section 6.3.7 of Morphological Image Analysis: Principles and Applications, 2nd ed., Springer, by P. Soille.


Get the MATLAB code

Published with MATLAB® 7.4

8 Responses to “Filling holes in images”

  1. masimet replied on :

    Thank Steve. imfill solves my problem on removing noise inside object caused by camera flash

  2. Hilda replied on :

    Hi Steve
    I have an image 480×640 and i want to put in the center a black circle. Is posible if i generate the cicrle in this form?
    t = imspace(0,2*pi, 100);
    h=0;
    k=0;
    r=10;
    x=r*cos(t)+k;
    y=r*sin(t)+k;
    plot(x,y), fill(x,y,’k');
    axis square;
    or i have to superimpose other image with a black circle?
    thanks

  3. Hilda replied on :

    i have to correct
    t=linspace(0,2*pi,100);
    thanks

  4. Steve replied on :

    Hilda—It depends on what you want. Do you want a single image whose center pixels in a circular region have been replaced by black pixels? Do you want to superimpose one image over another in semitransparent fashion? Do you want to superimpose a black circular patch over your image? That’s what your code looks like it’s doing. You’ll need to call:

    >> hold on
    

    after displaying your image and before calling fill.

  5. amal replied on :

    hi steve .
    you want to superimpose one image over another
    i use the code

    image(X1);
    hold on;
    image(X2);

    but i want the second image to be at the “center” of the first.
    please help
    thankx..

  6. Steve replied on :

    Amal—Look at the XData and YData properties of the image object. By setting those properties you can control where an image appears.

  7. Dan replied on :

    hi Steve,
    I am having trouble getting this to work in 3d:
    BW1 = logical([1 0 0 0 0 0 0 0
    1 1 1 1 1 0 0 0
    1 0 0 0 1 0 1 0
    1 0 0 0 1 1 1 0
    1 1 1 1 0 1 1 1
    1 0 0 1 1 0 1 0
    1 0 0 0 1 0 1 0
    1 0 0 0 1 1 1 0]);

    BW2 = imfill(BW1,[3 3],8)
    for i = 1:8
    BW13(:,:,i)=BW1;
    end
    BW133= imfill(BW13,[3 3 3],6)
    in Matlab 7.3 I keep getting errors, but it seems logical given the help documentation.
    ??? Function IMFILL expected its third input, OPTION,
    to be one of these types:

    char

    Instead its type was double.

    Error in ==> iptcheckstrs at 40
    iptcheckinput(in, {’char’}, {’row’}, function_name, variable_name, …

    Error in ==> imfill>parse_inputs at 249
    iptcheckstrs(varargin{3}, {’holes’}, mfilename, ‘OPTION’, 3);

    Error in ==> imfill at 124
    [I,locations,conn,do_fillholes] = parse_inputs(varargin{:});

  8. Steve replied on :

    Dan—It works for me using R2007b. Be sure that the class of BW13 is logical before your last call to imfill.

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.

  • Mikr: I look for answers before asking people… “But we still can’t see the coordinates!...
  • Steve: Mikr—You might want to take a look at the Getting Started section of the MATLAB documentation in order...
  • Mikr: thanks but is it possible to see and write to file (Excel ?) that matrix of pixel coordinates ? instead of...
  • Steve: Mikr—An image in MATLAB is simply a matrix of pixel values. It can be saved (exported) to several common...
  • Mikr: thanks, Steve just started to learn matlab and to clarify matlab saves image files as a matrix of pixel...
  • Steve: Mikr—As far as I know, the commonly used image file formats such as TIFF, JPEG, PNG, etc., do not...
  • Mikr: how to write pixel coordinates in file ?
  • Steve: M.S.—Code for the bwtraceboundaries function ships with the Image Processing Toolbox.
  • M.S.Cheema: i need to know the detailed algorithm for bwtraceboundaries. i want how that function works. so please...
  • Steve: Wagas—It depends on how much memory you have on your computer. You should be able to load a 94 MB TIFF...

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

Related Topics