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

Blobby! - Shape/Blob Recognition Code

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

Sample Image

Introduction

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.

Background

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).

Training

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.

Recognition

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.

Output

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 ); 
  ipObj->detectShape(); 
  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.

History

  • May 2005: First release!

License

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

Share

About the Author

Gabriyel

Singapore Singapore
No Biography provided

Comments and Discussions

 
GeneralRe: Great Work Pinmembermrlinx_10125-Aug-05 12:57 
GeneralRe: Great Work PinmemberGabriyel26-Aug-05 6:14 
QuestionWhy not .NET? PinmemberRdRunnerxx26-May-05 10:51 
AnswerRe: Why not .NET? PinmemberRick York24-Aug-05 13:10 
As the author says, "I prefer to code in C++ because of the control I can have."
 
If you must have it in C# you may wish to have a look at the various methods for wrapping unmanaged code.

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
Web01 | 2.8.141022.2 | Last Updated 16 May 2005
Article Copyright 2005 by Gabriyel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid