Fill image regions and holes
BW2 = imfill(BW)
[BW2,locations] = imfill(BW)
BW2 = imfill(BW,locations)
BW2 = imfill(BW,'holes')
I2 = imfill(I)
BW2 = imfill(BW,locations,conn)
BW2 = imfill(BW) displays the binary image BW on the screen and lets you define the region to fill by selecting points interactively by using the mouse. To use this interactive syntax, BW must be a 2-D image. Press Backspace or Delete to remove the previously selected point. A shift-click, right-click, or double-click selects a final point and starts the fill operation. Pressing Return finishes the selection without adding a point.
[BW2,locations] = imfill(BW) returns the locations of points selected interactively in locations. locations is a vector of linear indices into the input image. To use this interactive syntax, BW must be a 2-D image.
BW2 = imfill(BW,locations) performs a flood-fill operation on background pixels of the binary image BW, starting from the points specified in locations. If locations is a P-by-1 vector, it contains the linear indices of the starting locations. If locations is a P-by-ndims(BW) matrix, each row contains the array indices of one of the starting locations.
BW2 = imfill(BW,'holes') fills holes in the binary image BW. 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) fills holes in the grayscale image I. In this syntax, a hole is defined as an area of dark pixels surrounded by lighter pixels.
BW2 = imfill(BW,locations,conn) fills the area defined by locations, where conn specifies the connectivity. conn can have any of the following scalar values.
Connectivity can 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.
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:
BW2 = imfill(BW,locations,conn) BW2 = imfill(BW,conn,'holes') I2 = imfill(I,conn)
To override the default connectivity and interactively specify the starting locations, use this syntax:
BW2 = imfill(BW,0,conn)
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.
imfill supports the generation of efficient, production-quality C/C++ code from MATLAB.
When generating code, note the following:
The optional input arguments, conn and 'holes' must be a compile-time constants.
Supports only up to 3-D inputs. (No N-D support.)
The interactive mode to select points, imfill(BW,0,CONN) is not supported.
With the locations input argument, once you select a format at compile-time, you cannot change it at run-time. However, the number of points in locations can be varied at run-time.
Fill in the background of a binary image from a specified starting location.
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)
Fill in the holes of a binary image.
BW4 = im2bw(imread('coins.png')); BW5 = imfill(BW4,'holes'); imshow(BW4), figure, imshow(BW5)
Fill in the holes of a grayscale image.
I = imread('tire.tif'); I2 = imfill(I,'holes'); figure, imshow(I), figure, imshow(I2)
 Soille, P., Morphological Image Analysis: Principles and Applications, Springer-Verlag, 1999, pp. 173-174.