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

Connected Component Labeling and Vectorization

, 2 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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

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)

Share

About the Author

leon de boer
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 | :)

Comments and Discussions

 
QuestionLicensing PinmemberSerrator2-Sep-14 22:36 
Questionopen problem Pinmemberxuancaoyy28-Apr-14 2:15 
AnswerRe: open problem Pinmemberxuancaoyy28-Apr-14 2:32 
SuggestionGreat Job!!! PinmemberKimJinWoo8-Oct-12 1:00 
GeneralRe: Great Job!!! PinmemberMember 1020950314-Aug-13 23:32 

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 | Terms of Use | Mobile
Web02 | 2.8.1411022.1 | Last Updated 2 May 2014
Article Copyright 2012 by leon de boer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid