imfill() - Image Processing
exampleBW2= imfill(BW,locations) performs
a flood-fill operation on background pixels of the input 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.
exampleBW2= imfill(BW,'holes') fills
holes in the input binary image BW. In this syntax,
a hole is a set of background pixels that cannot be reached by filling
in the background from the edge of the image.
exampleI2= 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) displays
the binary image BW on the screen and lets you
define the region to fill by selecting points interactively with the
mouse. To use this syntax, BW must be a 2-D image.
Press Backspace or Delete to
remove the previously selected point. Shift-click, right-click, or
double-click to select a final point and start the fill operation.
Press Return to finish the selection without
adding a point.
BW2 = imfill(BW,0,conn) lets
you override the default connectivity as you interactively specify
locations.
[BW2,locations_out]
= imfill(BW) returns the locations
of points selected interactively in locations_out.
The return value locations_out is a vector of
linear indices into the input image. To use this syntax, BW must
be a 2-D image.
BW2= imfill(BW,locations,conn) fills
the area defined by locations, where conn specifies
the connectivity.
exampleBW2= imfill(BW,conn,'holes') fills
holes in the binary image BW, where conn specifies
the connectivity.
exampleI2= imfill(I,conn) fills
holes in the grayscale image I, where conn specifies
the connectivity.
examplegpuarrayB = imfill(gpuarrayA,___) performs
the fill operation on a GPU. The input image and the return image
are 2-D gpuArrays. Use of this syntax requires Parallel Computing Toolbox™.
When run on a GPU, imfill does not support interactive
syntaxes, where you select locations using the mouse.Code Generation support:
Yes.MATLAB Function Block support:
Yes.
Syntax
BW2= imfill(BW,locations) exampleBW2= imfill(BW,'holes') exampleI2= imfill(I) exampleBW2 = imfill(BW)BW2 = imfill(BW,0,conn)[BW2,locations_out]
= imfill(BW)BW2= imfill(BW,locations,conn)BW2= imfill(BW,conn,'holes') exampleI2= imfill(I,conn) examplegpuarrayB = imfill(gpuarrayA,___) example
Example
Fill Image from Specified Starting PointOpen This ExampleBW1 = 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)
BW2 =
1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 1 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
1 0 0 1 1 1 1 0
1 0 0 0 1 1 1 0
1 0 0 0 1 1 1 0
Fill Holes in a Binary ImageOpen This Example
Read image into workspace.I = imread('coins.png');
figure
imshow(I)
title('Original Image')
Convert image to binary image.BW = imbinarize(I);
figure
imshow(BW)
title('Original Image Converted to Binary Image')
File holes in the binary image and display the result.BW2 = imfill(BW,'holes');
figure
imshow(BW2)
title('Filled Image')
Fill Holes in a Grayscale ImageOpen This ExampleI = imread('tire.tif');
I2 = imfill(I);
figure, imshow(I), figure, imshow(I2)
Fill Operation on a GPUCreate a simple sample binary image.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])BW1 =
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 0Create a gpuArray.BW1 = gpuArray(BW1);
Fill in the background of the image from a specified starting
location.BW2 = imfill(BW1,[3 3],8)BW2 =
1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 1 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
1 0 0 1 1 1 1 0
1 0 0 0 1 1 1 0
1 0 0 0 1 1 1 0
Output / Return Value
Limitations
Alternatives / See Also
Reference