This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Label connected components in binary image


L = bwlabeln(BW)
L = bwlabeln(BW,conn)
[L,n] = bwlabeln(___)



L = bwlabeln(BW) returns a label matrix, L, containing labels for the connected components in BW. bwlabeln uses a default connectivity of 8 for two dimensions, 26 for three dimensions, and conndef(ndims(BW),'maximal') for higher dimensions.

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.


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.

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 real, nonsparse, numeric or logical array of any dimension. For numeric input, any nonzero pixels are considered to be on.

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

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

Connectivity for the connected components, specified as one of the following scalar values.



Two-dimensional connectivities


4-connected neighborhood


8-connected neighborhood

Three-dimensional connectivities


6-connected neighborhood


18-connected neighborhood


26-connected neighborhood

To calculate the default connectivity for higher dimensions, bwlabeln uses conndef(ndims(BW),'maximal').

Connectivity can be defined in a more general way for any dimension using a 3-by-3-by- ... -by-3 matrix of 0s and 1s. conn must be symmetric about its center element. The 1-valued elements define neighborhood locations relative to conn.

Example: BW = imread('text.png'); L = bwlabeln(BW,4);

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

Label matrix of contiguous regions, returned as a 2-D array of nonnegative integers of class double. The kth region includes all elements in L that have value k. The number of objects and holes represented by L is equal to max(L(:)). The zero-valued elements of L make up the background.

Data Types: double

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

Data Types: double


  • The functions bwlabel, bwlabeln, and bwconncomp all compute connected components for binary images. bwconncomp replaces the use of bwlabel and bwlabeln. It 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, just pass BW directly into regionprops, i.e. regionprops(BW).

  • To compute a label matrix having a more memory-efficient data type (e.g., uint8 versus double), use the labelmatrix function on the output of bwconncomp:

    C = bwconncomp(BW);
    L = labelmatrix(CC);
    CC = bwconncomp(BW,n);
    S = regionprops(CC);


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.


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

Introduced before R2006a

Was this topic helpful?