Label and Measure Objects in a Binary Image

Understanding Connected-Component Labeling

A connected component in a binary image is a set of pixels that form a connected group. For example, the binary image below has three connected components.

Connected component labeling is the process of identifying the connected components in an image and assigning each one a unique label, like this:

The matrix above is called a label matrix.

bwconncomp computes connected components, as shown in the example:

cc = bwconncomp(BW)
cc = 

    Connectivity: 8
       ImageSize: [8 9]
      NumObjects: 3
    PixelIdxList: {[6x1 double]  [6x1 double]  [5x1 double]}

The PixelIdxList identifies the list of pixels belonging to each connected component.

For visualizing connected components, it is useful to construct a label matrix. Use the labelmatrix function. To inspect the results, display the label matrix as a pseudo-color image using label2rgb.

Construct a label matrix:

labeled = labelmatrix(cc);

Create a pseudo-color image, where the label identifying each object in the label matrix maps to a different color in the associated colormap matrix. Use label2rgb to choose the colormap, the background color, and how objects in the label matrix map to colors in the colormap:

RGB_label = label2rgb(labeled, @copper, 'c', 'shuffle');


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

Selecting Objects in a Binary Image

You can use the bwselect function to select individual objects in a binary image. You specify pixels in the input image, and bwselect returns a binary image that includes only those objects from the input image that contain one of the specified pixels.

You can specify the pixels either noninteractively or with a mouse. For example, suppose you want to select objects in the image displayed in the current axes. You type

BW2 = bwselect;

The cursor changes to cross-hairs when it is over the image. Click the objects you want to select; bwselect displays a small star over each pixel you click. When you are done, press Return. bwselect returns a binary image consisting of the objects you selected, and removes the stars.

See the reference page for bwselect for more information.

Finding the Area of the Foreground of a Binary Image

The bwarea function returns the area of a binary image. The area is a measure of the size of the foreground of the image. Roughly speaking, the area is the number of on pixels in the image.

bwarea does not simply count the number of pixels set to on, however. Rather, bwarea weights different pixel patterns unequally when computing the area. This weighting compensates for the distortion that is inherent in representing a continuous image with discrete pixels. For example, a diagonal line of 50 pixels is longer than a horizontal line of 50 pixels. As a result of the weighting bwarea uses, the horizontal line has area of 50, but the diagonal line has area of 62.5.

This example uses bwarea to determine the percentage area increase in circbw.tif that results from a dilation operation.

BW = imread('circbw.tif'); 
SE = ones(5);
BW2 = imdilate(BW,SE);
increase = (bwarea(BW2) - bwarea(BW))/bwarea(BW)
increase =


See the reference page for bwarea for more information about the weighting pattern.

Finding the Euler Number of a Binary Image

The bweuler function returns the Euler number for a binary image. The Euler number is a measure of the topology of an image. It is defined as the total number of objects in the image minus the number of holes in those objects. You can use either 4- or 8-connected neighborhoods.

This example computes the Euler number for the circuit image, using 8-connected neighborhoods.

BW1 = imread('circbw.tif'); 
eul = bweuler(BW1,8)

eul =


In this example, the Euler number is negative, indicating that the number of holes is greater than the number of objects.