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.
- Training (with an input, as reference for recognition).
- Recognition (based on some criteria).
- 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.
int main( void )
ImageProc* ipObj = new ImageProc();
ipObj->loadTrainingImage( "training.ppm" );
ipObj->loadWorkingImage( "working.ppm" );
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
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.