Image Processing Toolbox    

Connected-Components Labeling

The bwlabel function performs connected-components labeling, which is a method for indicating each discrete object in a binary image. You specify an input binary image and the type of neighborhood, and bwlabel returns a matrix of the same size as the input image. The different objects in the input image are distinguished by different integer values in the output matrix.

For example, suppose you have this binary image.

You call bwlabel, specifying 4-connected neighborhoods.

In the output matrix, the 1's represent one object, the 2's a second object, and the 3's a third. Notice that if you had used 8-connected neighborhoods (the default), there would be only two objects, because the first and second objects would be a single object, connected along the diagonal.

The output matrix is not a binary image, and its class is double. A useful approach to viewing it is to display it as a pseudocolor indexed image, first adding 1 to each element, so that the data is in the proper range. Each object displays in a different color, so the objects are easier to distinguish than in the original image.

The example below illustrates this technique. Notice that this example uses a colormap in which the first color (the background) is black and the other colors are easily distinguished.

Figure 9-7: A Binary Image Displayed with a Colormap After Connected-Components Labeling


 Flood Fill Object Selection