Image Processing Toolbox    

Flood-Fill Operations

The imfill function performs a flood-fill operation on binary and grayscale images. For binary images, imfill changes connected background pixels (0s) to foreground pixels (1s), stopping when it reaches object boundaries. For grayscale images, imfill brings the intensity values of dark areas that are surrounded by lighter areas up to the same intensity level as surrounding pixels. (In effect, imfill removes regional minima that are not connected to the image border. See Finding Areas of High- or Low-Intensity for more information.) This operation can be useful in removing irrelevant artifacts from images.

This section includes information about:

Specifying Connectivity

For both binary and grayscale images, the boundary of the fill operation is determined by the connectivity you specify.

The implications of connectivity can be illustrated with this matrix.

If the background is 4-connected, this binary image contains two separate background elements (the part inside the loop and the part outside). If the background is 8-connected, the pixels connect diagonally, and there is only one background element.

Specifying the Starting Point

For binary images, you can specify the starting point of the fill operation by passing in the location subscript or by using imfill in interactive mode, selecting starting pixels with a mouse. See the reference page for imfill for more information using imfill interactively.

For example, if you call imfill, specifying the pixel BW(4,3) as the starting point, imfill only fills in the inside of the loop because, by default, the background is 4-connected.

If you specify the same starting point, but use an 8-connected background connectivity, imfill fills in the entire image.

Filling Holes

A common use of the flood-fill operation is to fill "holes" in images. For example, suppose you have an image, binary or grayscale, in which the foreground objects represent spheres. In the image, these objects should appear as disks, but instead are donut shaped because of reflections in the original photograph. Before doing any further processing of the image, you may want to first fill in the "donut holes" using imfill.

Because the use of flood-fill to fill holes is so common, imfill includes special syntax to support it for both binary and grayscale images. In this syntax, you just specify the argument 'holes'; you do not have to specify starting locations in each hole.

To illustrate, this example fills holes in a grayscale image of blood cells.

  Pixel Connectivity Finding Peaks and Valleys