Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

imfill

Fill image regions and holes

Syntax

  • BW2 = imfill(BW)
  • BW2 = imfill(BW,0,conn)
  • [BW2,locations_out] = imfill(BW)
  • BW2= imfill(BW,locations,conn)
  • BW2= imfill(BW,conn,'holes')
    example
  • I2= imfill(I,conn)
    example
  • gpuarrayB = imfill(gpuarrayA,___)
    example

Description

example

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. 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.

example

BW2= 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.

example

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) 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.

example

BW2= imfill(BW,conn,'holes') fills holes in the binary image BW, where conn specifies the connectivity.

example

I2= imfill(I,conn) fills holes in the grayscale image I, where conn specifies the connectivity.

example

gpuarrayB = 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.

Examples

collapse all

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)
BW2 =

  8×8 logical array

   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

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')

Fill holes in the binary image and display the result.

BW2 = imfill(BW,'holes');
figure
imshow(BW2)
title('Filled Image')

I = imread('tire.tif');
I2 = imfill(I);
figure, imshow(I), figure, imshow(I2)

Create 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     0

Create 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

Input Arguments

collapse all

Input binary image, specified as a real, nonsparse, numeric or logical array of any dimension.

Example: BW = imread('text.png');

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Linear indices identifying pixel locations, specified as a 2-D, real, numeric vector or matrix of positive integers.

Example: BW2 = imfill(BW,[3 3],8);

Data Types: double

Input grayscale image, specified as a real, nonsparse, numeric array of any dimension.

Example: I = imread('cameraman.tif');I2 = imfill(I);

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Connectivity, specified as a one of the scalar values in the following table. By default, imfill uses 4-connected neighborhoods for 2-D images and 6-connected neighborhoods for 3-D inputs. For higher dimensions, imfill uses conndef(ndims(I),'minimal'). 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 0s and 1s. The 1-valued elements define neighborhood locations relative to the center element of conn. Note that conn must be symmetric around its center element.

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

Example:

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Input image, specified as a gpuArray.

Output Arguments

collapse all

Filled image, returned as an array the same class as the input image.

Linear indices of pixel locations, returned as a numeric vector or matrix.

Filled grayscale image, returned as a numeric array.

Output image, returned as a gpuArray.

More About

collapse all

Code Generation

This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic MATLAB Host Computer target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

When generating code, note the following:

  • The optional input arguments, conn and 'holes' must be a compile-time constants.

  • imfill supports up to 3-D inputs only. (No N-D support.)

  • The interactive syntax 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.

MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.

Algorithms

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

References

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

Introduced before R2006a

Was this topic helpful?