Color-based Blob Detection
Blob detection is a fast and simple method that can be used for many machine vision tasks, such as tracking a red ball, finding a blue marker or detecting a person’s skin (Skin Detection can be very useful with Face Detection and Face Recognition using a skin mask, as well as for Hand Gesture Recognition).
To find colored blobs, you should convert your color image from BGR to HSV format so that the colors are easier to separate. You can’t use other graphics software to see which HSV thresholds to use because the HSV values are different in OpenCV than in other software. So for example, if you want to detect a blue ball, you could run ColorWheelHSV to see that blue is roughly between the Hues 85 to 130 in OpenCV.
Then you can use “cvThreshold()” on a Hue image (single-channel image) to find the blue pixels. But since black, white and grey can also have any Hue values, you should also threshold the Saturation and Value components of the HSV image to ignore black, white and grey. Depending on how bright or dark or dull you expect your objects and your background environment to be, you might consider for example: anything with a “Value” (brightness) below 60 to be too dark (black), and anything with a Saturation below 30 to be too dull (grey or white). And after you have got your program working, you should play around with these HSV thresholds until you are happy with the results in your images, because they will vary a lot, and you might want to use a more complicated formula such as ignoring anything that has V above 200 if it also has S below 50 (white), etc.
One trick to watch out for is that in OpenCV, red can be the Hues between 0 to 10 OR it could be 175 to 179, because the Hue component in OpenCV wraps around like a circle in 180 values, so that the Hue of 179 is almost identical to a Hue of 0.
Now that you know the approximate HSV thresholds to use, you should write your program to:
- Convert a color image from BGR to HSV using “cvConvert()”.
- Split the HSV color image into its separate H, S and V components.
- Use “cvThreshold()” to look for the pixels that are in the correct range of Hue, Saturation and Value (Brightness).
- Use one of the blob libraries (listed below) to detect the blobs in the thresholded image so you can get the sizes and positions, etc, and you can track those blobs.
Also be aware that you will almost always have to do some sort of image noise filtering to reduce the noisy pixels, such as a combination of cvSmooth(), cvErode(), cvDilate(), etc. But that will depend completely on your exact project conditions. The official OpenCV book “Learning OpenCV: Computer Vision with the OpenCV Library” explains some ways to do the image filtering.
Blob Detection Libraries
There are 3 blob detection libraries for OpenCV with almost the same name, and they are all decent but not perfect, so you could look at them all and choose one:
- “cvBlobsLib”: http://opencv.willowgarage.com/wiki/cvBlobsLib. If you look in the cvBlobsLib FAQ, it shows how to use the “cvThreshold()” to threshold an image easily and detect blobs and filter them.
- “cvBlob”: http://code.google.com/p/cvblob/. There is a blob-detection example in the cvBlob FAQ.
- “Bloblib” by Dave Grossman (also referred to as “Blob Analysis Package”). Some comments about it on this forum thread.
- There is also the OpenCV function “cvFindContours()” that gives you the contour (outline) and blob area but not other statistics.
NOTE: If you don’t just want to detect where blobs are but follow the blob movement (such as getting the velocity of many blobs moving around), then OpenCV comes with a sample program “blobtrack.cpp” that is for Blob Tracking.
How to use a Blob Detection Library in Your Code
To use any C or C++ library (such as cvBlob or cvBlobsLib or even OpenCV) in your own project, you typically need to include the header files (.h) and link to the library file (.lib). If you don’t have the .lib files then you need to compile that library (such as using CMake to compile cvBlob) to give you the correct .lib files. Then you need to add the header files to be “included” into your project, and the lib files to be “linked” into your project, using whatever steps are required for your compiler. For example, Instructions for using cvBlobsLib in Visual Studio.
- Speed testing a website
- Introduction to Face Detection and Face Recognition
- Getting your dynamic public IP in C# & .net
- Hardware Platforms for Embedded Computer Vision, Image Processing and Deep Learning
- Rotating or Resizing an Image in OpenCV
- Who is WHOIS?
- Mixing Assembly language with Visual Basic
- Script to check available diskspace in C#/.net
- Fiddling with Fiddler
- Best Concise Linux System Administrator’s Guide (SAG)