This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Label connected components in binary image


L = bwlabeln(BW)
[L, NUM] = bwlabeln(BW)
[L, NUM] = bwlabeln(BW, conn)


L = bwlabeln(BW) returns a label matrix, L, containing labels for the connected components in BW. The input image BW can have any dimension; L is the same size as BW. The elements of L are integer values greater than or equal to 0. 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. The default connectivity is 8 for two dimensions, 26 for three dimensions, and conndef(ndims(BW), 'maximal') for higher dimensions.

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

[L, NUM] = bwlabeln(BW, conn) specifies the desired connectivity. conn can have any 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

Connectivity can also 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 central element of conn. Note that conn must be symmetric about its central element.

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

FunctionInput DimensionOutput FormMemory UseConnectivity
bwlabel2-DDouble-precision label matrixHigh4 or 8
bwlabelnN-DDouble-precision label matrixHighAny
bwconncompN-DCC structLowAny

Class Support

BW can be numeric or logical, and it must be real and nonsparse. L is of class double.


Calculate the centroids of the 3-D objects.

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


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

More About

collapse all


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, conn);
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?