Main Content

bwlabeln

Label connected components in binary image

Description

example

L = bwlabeln(BW) returns a label matrix, L, containing labels for the connected components in BW.

L = bwlabeln(BW,conn) returns a label matrix, where conn specifies the connectivity.

[L,n] = bwlabeln(___) also returns n, the number of connected objects found in BW.

Examples

collapse all

Create simple sample 3-D binary image.

BW = cat(3, [1 1 0; 0 0 0; 1 0 0],...
            [0 1 0; 0 0 0; 0 1 0],...
            [0 1 1; 0 0 0; 0 0 1])
BW = 
BW(:,:,1) =

     1     1     0
     0     0     0
     1     0     0


BW(:,:,2) =

     0     1     0
     0     0     0
     0     1     0


BW(:,:,3) =

     0     1     1
     0     0     0
     0     0     1

Label connected components in the image.

bwlabeln(BW)
ans = 
ans(:,:,1) =

     1     1     0
     0     0     0
     2     0     0


ans(:,:,2) =

     0     1     0
     0     0     0
     0     2     0


ans(:,:,3) =

     0     1     1
     0     0     0
     0     0     2

Input Arguments

collapse all

Binary image, specified as a numeric or logical array of any dimension. For numeric input, any nonzero pixels are considered to be 1 (true).

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

Pixel connectivity, specified as one of the values in this table. The default connectivity is 8 for 2-D images, and 26 for 3-D images.

Value

Meaning

Two-Dimensional Connectivities

4

Pixels are connected if their edges touch. Two adjoining pixels are part of the same object if they are both on and are connected along the horizontal or vertical direction.

Center pixel connected to four pixels

Current pixel is shown in gray.

8

Pixels are connected if their edges or corners touch. Two adjoining pixels are part of the same object if they are both on and are connected along the horizontal, vertical, or diagonal direction.

Center pixel connected to eight pixels

Current pixel is shown in gray.

Three-Dimensional Connectivities

6

Pixels are connected if their faces touch. Two adjoining pixels are part of the same object if they are both on and are connected in:

  • One of these directions: in, out, left, right, up, and down

Center pixel connected to the faces of 6 pixels

Current pixel is center of cube.

18

Pixels are connected if their faces or edges touch. Two adjoining pixels are part of the same object if they are both on and are connected in:

  • One of these directions: in, out, left, right, up, and down

  • A combination of two directions, such as right-down or in-up

Center pixel connected to the faces of 6 pixels and the edges of 12 pixels

Current pixel is center of cube.

26

Pixels are connected if their faces, edges, or corners touch. Two adjoining pixels are part of the same object if they are both on and are connected in:

  • One of these directions: in, out, left, right, up, and down

  • A combination of two directions, such as right-down or in-up

  • A combination of three directions, such as in-right-up or in-left-down

Center pixel connected to the faces of 6 pixels, the edges of 12 pixels, and the corners of 8 pixels

Current pixel is center of cube.

For higher dimensions, bwlabeln uses the default value conndef(ndims(BW),'maximal').

Connectivity can also be defined in a more general way for any dimension by specifying 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 about its center element. See Specifying Custom Connectivities for more information.

Data Types: double | logical

Output Arguments

collapse all

Label matrix, returned as an array of nonnegative integers with the same size as BW. The pixels labeled 0 are the background. The pixels labeled 1 make up one object; the pixels labeled 2 make up a second object; and so on.

Data Types: double

Number of connected objects in BW, returned as a nonnegative integer.

Data Types: double

Tips

  • This function sorts the connected components from left to right based on the top-left extremum of each component. When multiple components have the same horizontal position, the function then sorts those components from top to bottom, and again along any higher dimensions. This figure illustrates the extrema of two different 2-D regions.

    Two differently shaped regions, each with their eight extrema points labeled

  • The functions bwlabel, bwlabeln, and bwconncomp all compute connected components for binary images. bwconncomp uses significantly less memory and is sometimes faster than the other functions.

    FunctionInput DimensionOutput FormMemory UseConnectivity
    bwlabel2-DLabel matrix with double-precisionHigh4 or 8
    bwlabelnN-DDouble-precision label matrixHighAny
    bwconncompN-DCC structLowAny
  • To extract features from a binary image using regionprops with default connectivity, pass BW directly into regionprops using the command regionprops(BW).

Algorithms

bwlabeln uses the following general procedure:

  1. Scan all image pixels, assigning preliminary labels to nonzero pixels and recording label equivalences in a union-find table.

  2. Resolve the equivalence classes using the union-find algorithm [1].

  3. Relabel the pixels based on the resolved equivalence classes.

References

[1] Sedgewick, Robert, Algorithms in C, 3rd Ed., Addison-Wesley, 1998, pp. 11-20.

Extended Capabilities

Version History

Introduced before R2006a

expand all