Quantcast

Documentation Center

  • Trial Software
  • Product Updates

bwlabel

Label connected components in 2-D binary image

Syntax

L = bwlabel(BW, n)
[L, num] = bwlabel(BW, n)

Description

L = bwlabel(BW, n) returns a matrix L, of the same size as BW, containing labels for the connected objects in BW. The variable n can have a value of either 4 or 8, where 4 specifies 4-connected objects and 8 specifies 8-connected objects. If the argument is omitted, it defaults to 8.

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.

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

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 DimensionOutput FormMemory UseConnectivity
bwlabel2-DDouble-precision label matrixHigh4 or 8
bwlabelnN-DDouble-precision label matrixHighAny
bwconncompN-DCC structLowAny

Class Support

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

Examples

expand all

Label Components Using 4-connected Objects

Create a small binary image to use for this example.

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 =

     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 =

     2     5
     3     5
     2     6
     3     6

More About

expand all

Tips

Using find with bwlabel

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.

Using labelmatrix and regionprops

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

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.

See Also

| | | | |

Was this topic helpful?