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 use the "ColorWheelHSV" program on my HSV Color Conversion page to see which H, S and V values you want to threshold. 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()" (or my better HSV conversion functions).
- 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.