How to use Raspberry PI Camera module with OpenCV using C++

How to compile OpenCV on the Raspberry PI to use the Camera module using C++ for higher FPS

OpenCV doesn't support the RPI Camera module by default, there is a work around but it's too complicated. Using the python library gives a very poor FPS (3-7 FPS) which you can't do much with when intending to do video processing using openCV Lately i came across a C++ library called Raspicam which provides a full control of the RPI Camera module and also support for openCV , you can easily reach 24 FPS without the need to over clock the RPI .

In this blog page we will recompile opencv, install raspicam and we test  it with a simple color tracking program .
I presume the raspbian OS already installed on the RPI and the camera module is connected .

And  now we make sure that the software packages are up to date with :

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo rpi-updat

Then we install the requiered developpement tools with :

 $ sudo apt-get install build-essential cmake pkg-config

We install also few more packages necessary for image encoding , GTK GUI library  and install python dev files so it will be supported duing openCV compilation :

 $ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev  python2.7-dev

Next we download and extract openCV 2 :

$ wget
$ mv 2.4.11 opencv_2.4.11
$ unzip opencv_2.4.11 -d

Now just before we compile and install openCV, make sure to go get as much food as possible and some console game or whatever because the compilation process will take around 8 hours ! :) :

$ cd opencv_2.4.11/opencv_2.4.11
$ mkdir build
$ cd build
$ sudo make install
$sudo ldconfig

Next we  download  and extract raspicam  :

$ wget
$ unzip -d raspicam-0.1.3
$ cd raspicam-0.1.3/raspicam-0.1.3
$ mkdir build

Before we proceed with compiling raspicam , few modifications need to be applied to few files due some constat variables naming changes in the new openCV version .

In each of the following files in the raspicam-0.1.3 folder :



We can now compile and install  raspicam using the following command:

$ cd build
$ make
$ sudo make install
$ sudo ldconfig

If the compilation completed without error , you should find few  executable files under the utils folder that you can use to test the camera :  raspicam_test takes  few photos and stors them in files  and gives the FPS result :

$ ./raspicam_test
Usage (-help for help)
Connecting to camera
Connected to camera =00000000ea46d8e8 bufs=3686400
 capturing ...30/100Saving image30.ppm
 capturing ...60/100Saving image60.ppm
 capturing ...90/100Saving image90.ppm
 capturing ...95/100
Images saved in imagexx.ppm
4.05138 seconds for 100  frames : FPS 24.6829

As you can notice it reached abour 24 FPS .

There is also the executbale raspicam_cv_test to do the same test using openCV :

Usage (-help for help)
Connecting to camera
Connected to camera =00000000ea46d8e8
 capturing ...95/100
Images saved in imagexx.jpg
6.5143 seconds for 100  frames : FPS = 15.3508

The code source of those files can be found under raspicam-0.1.3/utls/  folder .

Comments :