Color-based Blob Detection:

Last updated on 27th December, 2010 by Shervin Emami. Posted originally on 3rd October, 2010.

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.

An RGB photo split into H, S and V components

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:

  1. Convert a color image from BGR to HSV using "cvConvert()" (or my better HSV conversion functions).
  2. Split the HSV color image into its separate H, S and V components.
  3. Use "cvThreshold()" to look for the pixels that are in the correct range of Hue, Saturation and Value (Brightness).
  4. 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.

Thresholded image           Skin blob

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:

  1. "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.
  2. "cvBlob": http://code.google.com/p/cvblob/
    There is a blob-detection example in the cvBlob FAQ.
  3. "Bloblib" by Dave Grossman (also referred to as "Blob Analysis Package"): http://www.shervinemami.com/files/BlobAnalysisPackage_DG.zip.
    Some comments about it on this forum thread.
  4. 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:

If you don't know how to add cvBlobsLib or cvBlob to your project, then I will quickly explain. 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.

 

DetectSkinBlobs demo program:

DetectSkinBlobs is a free program I created that splits a color image into separate Hue, Saturation and Value images, then applies a threshold on each of those 3 image planes for detecting human skin, then it combines the 3 thresholded images (using bitwise AND) to get just the pixels that pass the thresholds for skin's Hue, Saturation and Brightness. But the result is very noisy, so it then uses a blob detection library (cvBlobsLib, listed earlier) to detect blobs in the binary image of skin pixels. It then discards any blobs that are too small, so it hopefully will end up with just the large skin blob regions.

Here you can see the H,S,V components before and after they were thresholded for skin pixels:

H,S,V components before and after thresholds

When all 3 thresholded components (Output Hue, Output Saturation and Output Value from the above screenshot) are combined together (using a bitwise 'AND' operator), it results in the image below on the left, where most skin pixels are white and most background pixels are black. Notice that there are also some flowers that are detected as skin pixels.
Skin pixels and blobs

cvBlobsLib is used to filter out any blobs with area less than 100 pixels, and then generates the final image on the right, showing just the detected skin. Note that there are many different thresholds or techniques in the literature for detecting skin, I just used H,S,V thresholds of 18, 50 and 80 (respectively). For example, you could filter Hues to be less than 18 OR greater than 175.

Note: you may want to use cvInRanges() instead of cvThreshold() to set both a min & max threshold for each channel instead of just a min OR max.

Update (11th Oct 2010): I added a new feature for people that want to try separating color objects. After you have become familiar with the program, you can set "SHOW_EXTENDED_COLOR_IMAGES = true" (at the top of the file), to also show the 3 extended Hue,Sat,Brightness images. This will be useful for some people so they can see the Hue colors of pixels at max brightness or max saturation, etc. For example, the "Hue colored" image shows just the Hue pixels of the original image, if Saturation and Brightness are both set to max. This lets you see what Hue any pixel was detected as, even if it is a black or white pixel, etc. This makes it easier to see how you should be thresholding your images.

 

Download DetectSkinBlobs:

The software and source-code is available here (open-source freeware), to use on Windows, Mac, Linux, iPhone, etc as you wish for educational or commercial purposes but NOT military purposes.

Click here to download "DetectSkinBlobs": detectSkinBlobs.zip

(0.5MB file including VS2008 C/C++ source code, the Win32 executable and cvBlobsLib, created 11th Oct 2010).

Note that I included cvBlobsLib in the ZIP file just so it is ready to compile, but you still need OpenCV installed. The EXE file is compiled for OpenCV 2.1 on 32-bit Windows, but can be compiled for any version of OpenCV or standard operating system with any OpenCV compiler.

If you are using a 64-bit computer and the demo crashes, you should use this 64-bit version of cvBlobsLib, created by Kei Chan. (Add 'cvblobslib_x64.lib' to your linker in Release mode and 'cvblobslibd_x64.lib' in Debug mode).

And if you want to run the program but dont have the Visual Studio 2008 runtime installed then you can just get the Win32 DLLs ('msvcr90.dll', etc): MS_VC90_CRT.7z (0.4MB 7-Zip file).

To open Zip or 7z files you can use the freeware 7-Zip program (better than WinZip and WinRar in my opinion) from HERE.