Click here to Skip to main content
15,879,535 members
Articles / High Performance Computing / Vectorization
Tip/Trick

Connected Component Labeling and Vectorization

Rate me:
Please Sign up or sign in to vote.
4.82/5 (9 votes)
2 May 2014CPOL1 min read 38.8K   1.3K   16   6
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)


Written By
Software Developer
Australia Australia
Experience hardware/software design engineer with specialization in the field of embedded controllers and software. Software credits include Free Pascal development RTL, Cipher Multitask engraving software and Symmetry Laser/Cutting software. Firmware/hardware credits include Cipher CNC controllers series 1-3, Vision series 1 engraver controller and I-Marc pet tagger controller.

It is about now you realize you have been doing this for far too long Smile | :)

contact: ldeboer@gateway.net.au

Comments and Discussions

 
QuestionLicensing Pin
Serrator2-Sep-14 21:36
Serrator2-Sep-14 21:36 
AnswerRe: Licensing Pin
leon de boer16-Jun-15 3:52
leon de boer16-Jun-15 3:52 
Questionopen problem Pin
xuancaoyy28-Apr-14 1:15
xuancaoyy28-Apr-14 1:15 
AnswerRe: open problem Pin
xuancaoyy28-Apr-14 1:32
xuancaoyy28-Apr-14 1:32 
SuggestionGreat Job!!! Pin
KimJinWoo8-Oct-12 0:00
KimJinWoo8-Oct-12 0:00 
Thanks a lot!!!
GeneralRe: Great Job!!! Pin
Member 1020950314-Aug-13 22:32
Member 1020950314-Aug-13 22:32 

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

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