Click here to Skip to main content
14,327,470 members

Blobby! - Shape/Blob Recognition Code

Rate this:
4.38 (16 votes)
Please Sign up or sign in to vote.
4.38 (16 votes)
15 May 2005LGPL3
A project on colored shape/blob recognition.

Sample Image


I came across the need to make use of shape recognition in one of the projects in my workplace. I was quite surprised to find out that there wasn't much usable code or examples from the net that I could make use of. The tricky part was that I had only two weeks or so to focus on the image processing/blob analysis part. Hence I designed my own shape/blob recognition algorithm which encompasses a simple core idea, is simple and effective.

Do note that I'm not writing an industrial-strength shape recognition code that is based on published research literature. I have contacted a few reputable image-processing software vendors concerning their products and I can tell you that they indeed have very refined tools for specific needs such as defect detection, cell analysis etc. However, I believe there may be some folks out there who'd need some basic shape recognition functionality integrated into their projects but do not have the luxury to buy such products. Therefore, I hope the code contributed here can provide a starting point for some basic usage or a platform for further development.


Before we plunge into how the code works, here's just a short preamble to how the shape recognition process works.

  1. Training (with an input, as reference for recognition).
  2. Recognition (based on some criteria).
  3. Output (detected information).


The logic for shape recognition is derived from a "knowledge database". To train the shape recognition function, you only need to prepare sample images containing the shapes you want the function to learn and pass it into the program. There are a couple of sample image files in the project zip file to demonstrate the recognition of randomly oriented shapes. An additional note on making your own training images - the images have to be of the size of the bounding rectangle of the shape, i.e. make sure your shape fits exactly within your image boundaries. I have also written a method called trimImage() in the ImageProc class which you can use to create a tool for making such images.


We discussed about training the shape recognition function previously but without specifying the recognition criteria. Common attributes would be color, size (scale) and shape. There is really a lot to study into for color and shape recognition. I'm forsaking details into color segmentation from say, a photo-image possibly. And also shape recognition using line hulls, hue invariants... etc. I built the shape/blob recognition functionality around simple input conditions - colors are already segmented and have well-defined color separation at edges. You are free to improve it if your situation is trickier like shadows, gradated edges at the input stage etc. More details are given at the source code on how I did shape, size and color recognition. Credit is given to Eran Yariv's code which I've used for rotating images and the Vidcapture Project for handling PPM images.


My code dumps information like the centroid of the blob (location) and also whether the shape exists in the given working bitmap. You can easily dump other information like pixel count, bounding box and color etc., based on your application needs.

Using the code

I prefer to code in C++ because of the control I can have. You can easily port it to VB or any other UI-based software framework once you understand how the shape recognition class works. Refer to the Main.cpp to see how the code runs. There's really nothing intricate about getting it to run.

#include "ImageProc.h" 
int main( void ) 
  ImageProc* ipObj = new ImageProc();

  // Training 
  ipObj->loadTrainingImage( "training.ppm" ); 
  ipObj->loadWorkingImage( "working.ppm" );

  // Recognition 
  Color key( 0, 0, 255 ); // set the color of the blob you want to capture 
  ipObj->catchBlobs( key ); 
  Output ipObj->markBlobCentroid(); // output map for verification

  // Cleanup and exit 
  delete ipObj; 
  return 0;

Points of Interest

I find the challenge is really in the post-processing of the real-life images that you want to work with. If you do not capture images under very stringent lighting conditions to ensure color uniformity (killing shadows too) and also with high-resolution devices and further image "cleaning" processes, chances are that you would not even come near to some sane shape recognition stage. To put it simple, shape recognition breaks down if your image is not "clean" enough.


  • May 2005: First release!


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Singapore Singapore
No Biography provided

Comments and Discussions

Generalhello Pin
abdo12124-May-08 0:06
memberabdo12124-May-08 0:06 
Generalworking with blobby Pin
Syndroom30-Apr-07 3:08
memberSyndroom30-Apr-07 3:08 
GeneralRe: working with blobby Pin
bubifengyun27-Sep-12 18:22
memberbubifengyun27-Sep-12 18:22 
GeneralDetecting play card. Pin
forkus20005-Nov-06 3:27
memberforkus20005-Nov-06 3:27 
Questionshape racocnition question Pin
aajymi17-Apr-06 9:44
memberaajymi17-Apr-06 9:44 
GeneralMasters Degree Thesis Pin
JR Ryan23-Mar-06 11:07
memberJR Ryan23-Mar-06 11:07 
Generalimage processing-shape Pin
elips13-Mar-06 3:43
memberelips13-Mar-06 3:43 
QuestionVC 6.0? Pin
ori kovacsi12-Mar-06 23:00
memberori kovacsi12-Mar-06 23:00 
Generaldetecting shirt Pin
djsubari13-Sep-05 21:35
memberdjsubari13-Sep-05 21:35 
GeneralRe: detecting shirt Pin
Anonymous14-Sep-05 3:16
memberAnonymous14-Sep-05 3:16 
GeneralRe: detecting shirt Pin
Gabriyel14-Sep-05 3:27
memberGabriyel14-Sep-05 3:27 
GeneralRe: detecting shirt Pin
djsubari14-Sep-05 3:49
memberdjsubari14-Sep-05 3:49 
GeneralRe: detecting shirt Pin
Gabriyel14-Sep-05 4:07
memberGabriyel14-Sep-05 4:07 
GeneralRe: detecting shirt Pin
djsubari14-Sep-05 4:19
memberdjsubari14-Sep-05 4:19 
GeneralRe: detecting shirt Pin
R_Zhang16-Jan-07 21:31
memberR_Zhang16-Jan-07 21:31 
GeneralRe: detecting shirt Pin
elips13-Mar-06 15:48
memberelips13-Mar-06 15:48 
GeneralRe: detecting shirt Pin
ptr_Electron12-Sep-07 20:13
memberptr_Electron12-Sep-07 20:13 
GeneralGreat Work Pin
mrlinnx24-Aug-05 11:31
membermrlinnx24-Aug-05 11:31 
GeneralRe: Great Work Pin
Gabriyel25-Aug-05 5:01
memberGabriyel25-Aug-05 5:01 
General[Message Deleted] Pin
mrlinnx25-Aug-05 12:04
membermrlinnx25-Aug-05 12:04 
GeneralRe: Great Work Pin
mrlinnx25-Aug-05 12:57
membermrlinnx25-Aug-05 12:57 
GeneralRe: Great Work Pin
Gabriyel26-Aug-05 6:14
memberGabriyel26-Aug-05 6:14 
QuestionWhy not .NET? Pin
RdRunnerxx26-May-05 10:51
memberRdRunnerxx26-May-05 10:51 
AnswerRe: Why not .NET? Pin
Rick York24-Aug-05 13:10
mveRick York24-Aug-05 13:10 

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.

Posted 15 May 2005


51 bookmarked