Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / HPC / vectorization

Connected Component Labeling and Vectorization

4.82/5 (9 votes)
2 May 2014CPOL1 min read 40K   1.3K  
Implementation of connected component labeling and subsequent vectorization.

Introduction

Omar Gamil created an article on connected component labeling (http://catalog.codeproject.com/Articles/336915/Connected-Component-Labeling-Algorithm).

As discussed in the comments the way he achieved it was not the fastest way to achieve the below result

Image 1

Background

There is a far faster way which is to do a one pass seperation of the image using a tecnique discussed in "A Linear-Time Component-Labeling Algorithm Using Contour Tracing Technique" by Fu Chang, Chun-Jen Chen, and Chi-Jen Lu (www.iis.sinica.edu.tw/papers/fchang/1362-F.pdf).

From the article I developed C code which is presented here.

Using the code

The use of the code is fairly straight forward to use load a bitmap (I have included a few samples) and then choose the vectorize function. There are several play around options to display different features, zoom etc.

The code is fairly well documented and the original PDF work should provide any background information that is required.

In the load bitmap section on the demo I have simply done a black/white separation based on the grayscale value of the image. In many instances the user may require more than two color seperation and they would need to modify and provide there own color reduction code.

C++
//
 

/*---------------------------------------------------------------------------
 Loads a bitmap file and converts it to a CCL map
 ---------------------------------------------------------------------------*/
HDC load_bmp(char* BMPName){
 unsigned char i;
 int x, y;
 HBITMAP Bmp;
 BITMAP bm = {0};
 HDC Dc;
 COLORREF Col;

 CleanUp();                                                    // Cleanup any allocated data
 Bmp = (HBITMAP) LoadImage(GetModuleHandle(NULL), BMPName, 
  IMAGE_BITMAP, 0, 0, 
  LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_CREATEDIBSECTION);    // Load the bitmap
 if (Bmp == 0) return (0);                                     // Bitmap laod failed so exit
 GetObject(Bmp, sizeof(bm), &bm);                          // Get bitmap dimensions
 if ((bm.bmWidth > 32765) || (bm.bmHeight > 32765)){     // Bitmap is too big to vectorize (outside int range)
  MessageBox(0, "Bitmap is too large", "BITMAP ERROR", MB_OK); // Display a message box
  return(0);                                                   // Fail return
 }
 Dc = CreateCompatibleDC(0);                                   // Create a memory DC
 SelectObject(Dc, Bmp);                                        // Select bitmap to memory DC
 FirstImage = CreateCCLMap2D((short)(bm.bmWidth+2), 
  (short)(bm.bmHeight+2), 0);                                  // Create a CCL map (IT IS BLANK HERE)


 /* THIS IS MY SIMPLE 2 COLOR SEPERATION INTO THE CCL_MAP DATA */
    
 /* THE USER WOULD CREATE THERE OWN CUSTOM COLOR BASED SEPERATION */

 /* FILLING THE CCL_MAP WIH B/W DATA FROM THE BITMAP LOOP */
 for(y = 0; y < bm.bmHeight; y++){                            // For each y height scanline
  for(x = 0; x < bm.bmWidth; x++){                            // For each x pixel
   Col = GetPixel(Dc, x, y);                                     // Fetch the pixel colour  
   if (RGB_To_GreyScale(Col) < 128) i = 1; else i = 0;        // Convert to B/W depending colour
   FirstImage->map[y+1][x+1] = i;                             // Set B/W colour to CCL Map
  };
 };

 DeleteObject(Bmp);                                           // Delete the bitmap
 return Dc;                                                   // return the memory DC
};

//

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)