Functions > Image Processing > Feature Extraction > Example: Connected Component Labeling
Example: Connected Component Labeling
Use the concomp function to perform connected component labeling of an image. Two pixels are connected if they have the same value, and connectivity can be classified as 4 (only horizontal or vertical neighbors) or 8 (horizontal, vertical and diagonal neighbors). Connected components are a useful way to automate image feature recognition, bounding like areas and holes in the image.
For information on using this example, refer to About Image Processing Examples.
1. Define an input matrix.
Click to copy this expression
2. Define a target foreground intensity value.
Click to copy this expression
3. Perform the connected component labeling using the first connectivity type.
Click to copy this expression
Click to copy this expression
In this case, there are two 4-connected components with pixel value 100.
4. Perform the connected component labeling using the second connectivity type.
Click to copy this expression
Click to copy this expression
In this case, there is one 8-connected component with pixel value 100.
5. Examine the effect of connected component labeling on a binary image, defined by a two-dimensional sinc function.
Click to copy this expression
6. Define range variables.
Click to copy this expression
Click to copy this expression
Click to copy this expression
7. Define a range of values.
Click to copy this expression
8. Define an image matrix.
Click to copy this expression
9. Define a threshold, use the binarize function to binarize the image matrix, and then use the WRITEBMP function to write the image to a file.
Click to copy this expression
Click to copy this expression
Click to copy this expression
(cc_binarized.bmp)
10. Define a foreground intensity value and the connectivity type, then apply the connected-components labeling algorithm to this image.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Counting the distinct image regions, the algorithm is expected to identify 9 unique connected components in this image.
11. Calculate the number of components found by the algorithm.
Click to copy this expression
12. Define a color palette matrix, then use the gray_to_rgb function to convert the grayscale matrix to color. This allows you to look at the connected-component image using false color to enhance the components visually.
Click to copy this expression
Click to copy this expression
Click to copy this expression
(color_d.bmp)
The 4-column colormap matrix specifies grayscale intensity in the first column, and RGB values in the second through fourth columns, respectively.
13. Use the norm1 function to compute the size of the 9 components.
Click to copy this expression
Click to copy this expression
Click to copy this expression
14. Extract the largest single component (index 3, size 35 pixels) from this image and display it, again in false color.
Click to copy this expression
Click to copy this expression
15. Use the WRITERGB function to write the image to a file.
Click to copy this expression
(cimg_3.bmp)
The matrix comp is a 101 x 101 matrix of 0's except for elements that correspond to the largest component. The pixels of comp are multiplied by 3 in order to preserve the color of the component.
Was this helpful?