Implementation Of Labeling Connected Components in C# by asif


Brief Description:

Connected components labeling scans an image and groups its pixels into components based on pixel connectivity, i.e. all pixels in a connected component share similar pixel intensity values and are in some way connected with each other. Once all groups have been determined, each pixel is labeled with a gray level or a color (color labeling) according to the component it was assigned to.

Extracting and labeling of various disjoint and connected components in an image is central to many automated image analysis applications.

How It Works

Connected component labeling works by scanning an image, pixel-by-pixel (from top to bottom and left to right) in order to identify connected pixel regions, i.e. regions of adjacent pixels which share the same set of intensity values V. (For a binary image V={1}; however, in a gray level image V will take on a range of values, for example: V={51, 52, 53, …, 77, 78, 79, 80}.)

Connected component labeling works on binary or gray level images and different measures of connectivity are possible. However, for the following we assume binary input images and 8-connectivity. The connected components labeling operator scans the image by moving along a row until it comes to a point p (where p denotes the pixel to be labeled at any stage in the scanning process) for which V={1}. When this is true, it examines the four neighbors of p which have already been encountered in the scan (i.e. the neighbors (i) to the left of p, (ii) above it, and (iii and iv) the two upper diagonal terms). Based on this information, the labeling of p occurs as follows:

  • If all four neighbors are 0, assign a new label to p, else
  • if only one neighbor has V={1}, assign its label to p, else
  • if one or more of the neighbors have V={1}, assign one of the labels to p and make a note of the equivalences.

After completing the scan, the equivalent label pairs are sorted into equivalence classes and a unique label is assigned to each class. As a final step, a second scan is made through the image, during which each label is replaced by the label assigned to its equivalence classes. For display, the labels might be different gray levels or colors.

Guidelines for Use

To illustrate connected components labeling, we start with a simple image containing some distinct artificial objects( specifically text)

Now we apply Grayscale conversion to the image to convert it to Grayscale image.

Now, we will convert the image into binary so only two intensities will be present in the image….. Black and white or in other words 0 and 1.

After scanning this image and labeling the distinct pixels classes with a different gray value, we obtain the labeled output image.

C# Sample Program:

The algorithm is coded in C# using unsafe so the quality and speed of the program may not be affected. The class BitmapData is used to read and process the pixels in the image. This is the specialty of C# to provide such a speed even on image processing applications. There is a set of modules that are designed to implement the algorithm. The program scans the image to convert the image into grayscale Levels. Then it converts the image into binary. This binary image will be subjected to splitting it into components. Here we can us 4-Connected or 8-Connected Algorithm but we used 8-Connected Algorithm for finding the Blobs in the image as described above.

The final output is a colored image showing separate colors for every blob…. An array named objects of Type Class Objects contains the sizes of each blob or object being segmented….

Attachments:

   Project Files: connected_components_image.zip