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
Step1: cvBlob installation
1. Download cvBlob library from this link
https://github.com/Steelskin/cvblob
and save it to
/home/pi/cvblob
2. Because I downloaded the zip file, I need to unzip it
tar xvf cvblob-0.10.4-src.zip
3. We need to make some bug fixing. So move to the source code directory
cd cvblob
cv cvBlob
and make the following changes to
a. cvblob.h: at line 84 replace
const char cvChainCodeMoves[8][2] = { {0, -1},
with
const signed char cvChainCodeMoves[8][2] = { {0, -1},
b. cvlabel.cpp: at line 34 replace
const char movesSE[4][3][4] = <line continues...>
with
const signed char movesSE[4][3][4] = <line continues...>
c. cvlabel.cpp: at line 40 replace
const char movesI[4][3][4] = <line continues...>
with
const signed char movesI[4][3][4] = <line continues...>
4. build cvBlob library
cd ..
cmake .
make
5. and finally let's install the cvBlob library
make install
Step2: develop an application that can detect blobs in images
- 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_initialize(&state); raspistill_shot(&state, camera_callback); raspistill_terminate(&state);
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
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)
and
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 blobsIplImage* labelImg;CvBlobs blobs;
labelImg = cvCreateImage(cvGetSize(image), IPL_DEPTH_LABEL, 1);
result = cvLabel(segmentated, labelImg, blobs);
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
4. Let's build application with the usual sequence of commands
cd build
cmake ..
make