Main Content

bwlabel

Label connected components in 2-D binary image

Description

L = bwlabel(BW) returns the label matrix L that contains labels for the 8-connected objects found in BW.

example

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

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

Examples

collapse all

Create a small binary image.

BW = logical ([1     1     1     0     0     0     0     0
               1     1     1     0     1     1     0     0
               1     1     1     0     1     1     0     0
               1     1     1     0     0     0     1     0
               1     1     1     0     0     0     1     0
               1     1     1     0     0     0     1     0
               1     1     1     0     0     1     1     0
               1     1     1     0     0     0     0     0]);

Create the label matrix using 4-connected objects.

L = bwlabel(BW,4)
L = 8×8

     1     1     1     0     0     0     0     0
     1     1     1     0     2     2     0     0
     1     1     1     0     2     2     0     0
     1     1     1     0     0     0     3     0
     1     1     1     0     0     0     3     0
     1     1     1     0     0     0     3     0
     1     1     1     0     0     3     3     0
     1     1     1     0     0     0     0     0

Use the find command to get the row and column coordinates of the object labeled "2".

[r, c] = find(L==2);
rc = [r c]
rc = 4×2

     2     5
     3     5
     2     6
     3     6

Input Arguments

collapse all

Binary image, specified as a 2-D numeric matrix or 2-D logical matrix. 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 these values.

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.

Data Types: double | logical

Output Arguments

collapse all

Label matrix of contiguous regions, returned as matrix 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. This figure illustrates the extrema of two different 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.

     Input DimensionOutput FormMemory UseConnectivity
    bwlabel2-DDouble-precision label matrixHigh4 or 8
    bwlabelnN-DDouble-precision label matrixHighAny
    bwconncompN-DCC structLowAny
  • You can use the MATLAB® find function in conjunction with bwlabel to return vectors of indices for the pixels that make up a specific object. For example, to return the coordinates for the pixels in object 2, enter the following:.

    [r,c] = find(bwlabel(BW)==2)

    You can display the output matrix as a pseudocolor indexed image. Each object appears in a different color, so the objects are easier to distinguish than in the original image. For more information, see label2rgb.

  • To extract features from a binary image using regionprops with default connectivity, pass BW directly into regionprops using the command regionprops(BW).

  • The bwlabel function can take advantage of hardware optimization for data types logical, uint8, and single to run faster. Hardware optimization requires marker and mask to be 2-D images and conn to be either 4 or 8.

Algorithms

bwlabel uses the general procedure outlined in reference [1], pp. 40-48:

  1. Run-length encode the input image.

  2. Scan the runs, assigning preliminary labels and recording label equivalences in a local equivalence table.

  3. Resolve the equivalence classes.

  4. Relabel the runs based on the resolved equivalence classes.

References

[1] Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Volume I, Addison-Wesley, 1992, pp. 28-48.

Extended Capabilities

Version History

Introduced before R2006a

expand all