Main Content

Label and Measure Connected Components in a Binary Image

Detect Connected Components

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.

Calculate connected components by using bwconncomp. In this sample code, BW is the binary matrix shown in the above image. Specify a connectivity of 4 so that two adjoining pixels are part of the same object if they are both on and are connected along the horizontal or vertical direction. The PixelIdxList field identifies the list of pixels belonging to each connected component.

BW = zeros(8,9);
BW(2:4,2:3) = 1;
BW(5:7,4:5) = 1;
BW(2,7:9) = 1;
BW(3,8:9) = 1;
BW
BW =

     0     0     0     0     0     0     0     0     0
     0     1     1     0     0     0     1     1     1
     0     1     1     0     0     0     0     1     1
     0     1     1     0     0     0     0     0     0
     0     0     0     1     1     0     0     0     0
     0     0     0     1     1     0     0     0     0
     0     0     0     1     1     0     0     0     0
     0     0     0     0     0     0     0     0     0
cc = bwconncomp(BW,4)
cc = 

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

Label Connected Components

Connected component labeling is the process of identifying the connected components in an image and assigning each one a unique label. The resulting matrix is called a label matrix.

Create a label matrix by using the labelmatrix function. This sample code continues with the connected component structure, cc, defined in the preceding section.

labeled = labelmatrix(cc)
labeled =

  8×9 uint8 matrix

   0   0   0   0   0   0   0   0   0
   0   1   1   0   0   0   3   3   3
   0   1   1   0   0   0   0   3   3
   0   1   1   0   0   0   0   0   0
   0   0   0   2   2   0   0   0   0
   0   0   0   2   2   0   0   0   0
   0   0   0   2   2   0   0   0   0
   0   0   0   0   0   0   0   0   0

To visualize connected components, display the label matrix as a pseudo-color image by using the label2rgb function. The label identifying each object in the label matrix maps to a different color in the associated colormap. You can specify the colormap, background color, and how objects in the label matrix map to colors in the colormap.

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

Select Objects in a Binary Image

You can use the bwselect function to select individual objects in a binary image. Specify pixels in the input image programmatically or interactively with a mouse. bwselect returns a binary image that includes only those objects from the input image that contain one of the specified pixels.

For example, use this command to select objects in the image displayed in the current axes.

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.

Measure Properties of Connected Components

The regionprops function can return measurements for several properties of connected components. Other functions measure a single property. For example, the bwarea function returns the area of a binary image.

This example uses bwarea to determine the percentage area increase in circbw.tif that results from a dilation operation. The area is a measure of the size of the foreground of the image and is roughly equal to the number of on pixels in the image. However, bwarea does not simply count the number of pixels set to on. 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.

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

    0.3456

See Also

| | | |

Related Examples

More About