Image Processing Toolbox    
imfill

Fill image regions

Syntax

Description

BW2 = imfill(BW,LOCATIONS) performs a flood-fill operation on background pixels of the input binary image BW, starting from the points specified in LOCATIONS. LOCATIONS can be a P-by-1 vector, in which case it contains the linear indices of the starting locations. LOCATIONS can also be a P-by-ndims(BW) matrix, in which case each row contains the array indices of one of the starting locations.

BW2 = imfill(BW,'holes') fills holes in the input binary image. A hole is a set of background pixels that cannot be reached by filling in the background from the edge of the image.

I2 = imfill(I,'holes') fills holes in an input intensity image, I. In this case, a hole is an area of dark pixels surrounded by lighter pixels.

Interactive Use

BW2 = imfill(BW) displays the binary image, BW, on the screen and lets you select the starting locations using the mouse. Click the mouse button to add points. Press <BackSpace> or <Delete> to remove the previously selected point. A shift-click, right-click, or double-click selects a final point and then starts the fill operation; pressing <Return> finishes the selection without adding a point. Interactive use is supported only for 2-D images.

The syntax [BW2,LOCATIONS] = imfill(BW) can be used to get the starting points selected using the mouse. The output LOCATIONS is a vector of linear indices into the input image.

Specifying Connectivity

By default, imfill uses 4-connected background neighbors for 2-D inputs and 6-connected background neighbors for 3-D inputs. For higher dimensions the default background connectivity is determined by using conndef(NUM_DIMS,'minimal').

You can override the default connectivity with these syntaxes:

To override the default connectivity and interactively specify the starting locations, use this syntax:

CONN may have any of the following scalar values.

Value
Meaning
Two-dimensional connectivities
4
4-connected neighborhood
8
8-connected neighborhood
Three-dimensional connectivities
6
6-connected neighborhood
18
18-connected neighborhood
26
26-connected neighborhood

Connectivity may be defined in a more general way for any dimension by using for CONN a 3-by-3-by- ... -by-3 matrix of 0's and 1's. The 1-valued elements define neighborhood locations relative to the center element of CONN. Note that CONN must be symmetric about its center element.

Class Support

The input image can be numeric or logical, and it must be real and nonsparse. It can have any dimension. The output image has the same class as the input image.

Examples

Fill in the background of a binary image from a specified starting location:

Fill in the holes of a binary image:

Fill in the holes of an intensity image:

Algorithm

imfill uses an algorithm based on morphological reconstruction [1].

See Also

bwselect, imreconstruct, roifill

Reference

[1]  Pierre Soille, Morphological Image Analysis: Principles and Applications, Springer-Verlag, 1999, pp. 173-174.


  imfeature imfilter