In this post we will face some image processing technique, like blob analysys.

I will use the cvBlob library to detect blobs in the image capture by the raspicam


So, let's start.


Step1: cvBlob installation

  1. 1.       Download cvBlob library from this link


               and save it to /home/pi/cvblob


  1. 2.       Because I downloaded the zip file, I need to unzip it

               tar xvf


  1. 3.       We need to make some bug fixing (actually I discovered this bugs while debugging the application I was developing, but in this tutorial I think you can make the utmost out of my headaches :-)). So move to the source code directory

cd cvblob

cv cvBlob

and make the following  changes to

  1. a.       cvblob.h: at line 84 replace

const char cvChainCodeMoves[8][2] = { {0, -1},


const signed char cvChainCodeMoves[8][2] = { {0, -1},

  1. b.      cvlabel.cpp: at line 34 replace

const char movesSE[4][3][4] = <line continues...>


const signed char movesSE[4][3][4] = <line continues...>

  1. c.       cvlabel.cpp: at line 40 replace

const char movesI[4][3][4] = <line continues...>


const signed char movesI[4][3][4] = <line continues...>



  1. 4.       build cvBlob library

               cd ..

     cmake .



  1. 5.       and finally let's install the cvBlob library

               make install



Step2: develop an application that can detect blobs in images

The full source for the application is attached to this post

  1. 1.       I firstly created some utility functions to capture images from RaspiCam. You can find such functions in raspistill.h and raspistill.c (everything is under development, so some code clean up may be required..). Thanks to raspistill functions, the main application is very easy to implement:


     raspistill_shot(&state, camera_callback);



               The raspistill_shot take a photo and invoke the camera_callback function. The only      parameter used by this function is an IplImage object that contains the captured image


  1. 2.       Next step is to implement functions for detecting blobs in the image. Such functions are implemented in the blobs.c and blobs.h functions. There are two functions currently implemented

         detectBlobs(IplImage* image, struct ImageParams* imageParams)



         detectBlobsWithImage(IplImage* image, struct ImageParams* imageParams)


                The second one shows the results of the image processing in a set of windows. So I'm going to use the latter for this demo because it's more impressive than some text output on a console. This function basically gets the images captured by the RaspiCam and creates an image (named segmentated) where the pixel whose color is inside the given RGB color range are white and the other pixels are black

IplImage* segmentated;

cvInRangeS(image, CV_RGB(155, 0, 0), CV_RGB(255, 130, 130), segmentated);


With the segmentated image, we can detect blobs

IplImage* labelImg;
CvBlobs blobs;
labelImg = cvCreateImage(cvGetSize(image), IPL_DEPTH_LABEL, 1);
result = cvLabel(segmentated, labelImg, blobs);


and finally we can apply a filter on the blob area to discard blobs that are too small


cvFilterByArea(blobs, 500, 1000000);


  1. 3.       Let's bring everything together and make  loop that periodically invoke the raspistill_shot function so that we can experiment with the camera and it's behavior in different light conditions


  1. 4.       Let's build application with the usual sequence of commands

               cd build

     cmake ..



  1. 5.       Finally run the application



Soon the video where you will see the results of all these efforts. Enjoy it!!