Click here to Skip to main content
Click here to Skip to main content

Connected Component Labeling and Vectorization

By , 20 Jun 2012
Rate this:
Please Sign up or sign in to vote.

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.

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.

//
 

/*---------------------------------------------------------------------------
 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)

About the Author

leon de boer
Software Developer
Australia Australia
No Biography provided

Comments and Discussions

 
SuggestionGreat Job!!! PinmemberKimJinWoo8-Oct-12 0:00 
GeneralRe: Great Job!!! PinmemberMember 1020950314-Aug-13 22:32 
It is usefull for my research!
Thanks!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 20 Jun 2012
Article Copyright 2012 by leon de boer
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid