OpenCV is up to version 2.3.1 and it still lacks a basic connected component function for binary blob analysis. A quick Google will bring up cvBlobsLib (http://opencv.willowgarage.com/wiki/cvBlobsLib), an external lib that uses OpenCV calls. At the time when I needed such functionality I wasn’t too keen on linking to more libraries for something so basic. So instead I quickly wrote my own version using existing OpenCV calls. It uses cv:floodFill with 4 connected neighbours. Here is the code and example input image
UPDATE: 22th July 2013
I got rid of the hacks to work with OpenCV 2.3.1. It should now work with OpenCV 2.4.x.
On Linux, you can compile it using:
g++ blob.cpp -o blob -lopencv_core -lopencv_highgui -lopencv_imgproc
The results …