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.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Label connected components in 2-D binary image

`L = bwlabel(BW)`

`L = bwlabel(BW,n)`

```
[L,num]
= bwlabel(___)
```

```
[gpuarrayL,num]
= bwlabel(gpuarrayBW,n)
```

`[`

performs
the labeling operation on a GPU. The input image and output image
are `gpuarrayL`

,`num`

]
= bwlabel(`gpuarrayBW`

,`n`

)`gpuArrays`

. The variable `n`

can
be a numeric array or a `gpuArray`

. This syntax requires
the Parallel Computing Toolbox™.

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.Input Dimension Output Form Memory Use Connectivity `bwlabel`

2-D Double-precision label matrix High 4 or 8 `bwlabeln`

N-D Double-precision label matrix High Any `bwconncomp`

N-D CC struct Low Any 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 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`

. For more information, see the reference page for each function.To extract features from a binary image using

`regionprops`

with default connectivity, just pass`BW`

directly into`regionprops`

, i.e.,`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.

`bwlabel`

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

Run-length encode the input image.

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

Resolve the equivalence classes.

Relabel the runs based on the resolved equivalence classes.

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

`bwconncomp`

| `bwlabeln`

| `bwselect`

| `label2rgb`

| `labelmatrix`

| `regionprops`

Was this topic helpful?