Installing Raspicam on Raspberry Pi: C++ API for using Raspberry camera with/without OpenCV
IMPORTANT!! The Rasperry Pi camera is not an usb-webcam. Thus, OpenCV doesn’t work natively (forget cvCaptureFromCAM for example and all your wonderful apps you’ve thought up!). However, some nice apps (such as raspivid or raspistill) controls the pi camera using MMAL functions. The idea is to modify source code of such apps, use buffer memory of the camera to feed OpenCV image objects. Pretty easy (said like that). You can find some examples at:
- https://thinkrpi.wordpress.com/2013/05/22/opencv-and-camera-board-csi/.
- https://thinkrpi.wordpress.com/2013/05/22/opencvpi-cam-step-5-basic-use-display-a-picture/.
Another option to control the RPI camera is the Raspicam library, which allows to use the Raspberry Pi Camera under BSD License. Main Developer: Rafael Muñoz Salinas ( rmsalinas at uco dot es). Acknowledgement: thanks to Josh-Larson for his contribution.
Download at SourceForge:
- https://sourceforge.net/projects/raspicam/files/?.
Main features
- Provides class RaspiCam for easy and full control of the camera.
- Provides class RaspiCam_Still and RaspiCam_Still_Cv for controlling the camera in still mode.
- Provides class RaspiCam_Cv for easy control of the camera with OpenCV.
- Provides class RaspiCam_Still and RaspiCam_Still_Cv for controlling the camera in still mode.
- Provides class RaspiCam_Still and RaspiCam_Still_Cv for using the still camera mode.
- Easy compilation/installation using cmake.
- No need to install development file of userland. Implementation is hidden.
- Many examples.
Connect to RPI with SSH and Xephyr
First, on Ubuntu:
$ Xephyr -ac -br -keybd ephyr,,,xkbmodel=pc105,xkblayout=es -noreset -screen 1280x720 :1
Then, on RPI:
$ DISPLAY=:1 ssh -Y pi@10.42.0.246
$ startlxde
Compiling
Note: You should compile and install again after a new version of OpenCV is installed in your PC.
Download the file to your raspberry. Uncompress.
Then, compile:
$ cd raspicam-xx
$ mkdir build
$ cd build
$ cmake ..
At this point you'll see something like:
-- CREATE OPENCV MODULE=1
-- CMAKE_INSTALL_PREFIX=/usr/local
-- REQUIRED_LIBRARIES=/opt/vc/lib/libmmal_core.so;/opt/vc/lib/libmmal_util.so;/opt/vc/lib/libmmal.so
-- Change a value with: cmake -D<Variable>=<Value>
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/raspicam/trunk/build
If OpenCV development files are installed in your system, then you see:
-- CREATE OPENCV MODULE=1
otherwise this option will be 0 and the opencv module of the library will not be compiled. Finally compile, install and update the ldconfig:
$ make
$ sudo make install
$ sudo ldconfig
After that, you have the programs
Using it in your projects
You can learn how to use the library by taking a look at the examples in the utils directory and by analyzing the header files. In addition, we provide a some simple examples on how to use the library.
First, create a directory for our own project. Then, go in and create a file with the name
#include <ctime>
#include <fstream>
#include <iostream>
#include <raspicam/raspicam.h>
#include <unistd.h>
using namespace std;
int main ( int argc,char **argv ) {
raspicam::RaspiCam Camera; //Cmaera object
//Open camera
cout<<"Opening Camera..."<<endl;
if ( !Camera.open()) {cerr<<"Error opening camera"<<endl;return -1;}
//wait a while until camera stabilizes
cout<<"Sleeping for 3 secs"<<endl;
sleep(3);
//capture
Camera.grab();
//allocate memory
unsigned char *data=new unsigned char[ Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB )];
//extract the image in rgb format
Camera.retrieve ( data,raspicam::RASPICAM_FORMAT_RGB );//get camera image
//save
std::ofstream outFile ( "raspicam_image.ppm",std::ios::binary );
outFile<<"P6\n"<<Camera.getWidth() <<" "<<Camera.getHeight() <<" 255\n";
outFile.write ( ( char* ) data, Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB ) );
cout<<"Image saved at raspicam_image.ppm"<<endl;
//free resrources
delete data;
return 0;
}
Then, compile:
$ g++ -I/usr/local/include/ -L/opt/vc/lib -g -o binary simpletest_raspicam.cpp -lraspicam -lmmal -lmmal_core -lmmal_util -o simpletest_raspicam
Finally, run the program:
$ ./simpletest_raspicam
OpenCV Interface
If the OpenCV is found when compiling the library, the libraspicam_cv.so module is created and the RaspiCam_Cv class available. Take a look at the examples in utils to see how to use the class. In addition, we show here how you can use the RaspiCam_Cv in your own project.
First, create a directory for our own project. Then, go in and create a file with the name
#include <ctime>
#include <iostream>
#include <raspicam/raspicam_cv.h>
using namespace std;
int main ( int argc,char **argv ) {
time_t timer_begin,timer_end;
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
int nCount=100;
//set camera params, CV_8UC1 grayscale, CV_8UC3 colored
Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );
//Open camera
cout<<"Opening Camera..."<<endl;
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
//Start capture
cout<<"Capturing "<<nCount<<" frames ...."<<endl;
time ( &timer_begin );
for ( int i=0; i<nCount; i++ ) {
Camera.grab();
Camera.retrieve ( image);
if ( i%5==0 ) cout<<"\r captured "<<i<<" images"<<std::flush;
}
cout<<"Stop camera..."<<endl;
Camera.release();
//show time statistics
time ( &timer_end ); /* get current time; same as: timer = time(NULL) */
double secondsElapsed = difftime ( timer_end,timer_begin );
cout<< secondsElapsed<<" seconds for "<< nCount<<" frames : FPS = "<< ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;
//save image
cv::imwrite("raspicam_cv_image.jpg",image);
cout<<"Image saved at raspicam_cv_image.jpg"<<endl;
}
Then, compile:
$ g++ -I/usr/local/include/ -g -o binary simpletest_raspicam_cv.cpp -lopencv_core -lopencv_highgui -lraspicam -lraspicam_cv -o simpletest_raspicam_cv
Finally, run the program:
$ ./simpletest_raspicam_cv
Resources
- https://www.uco.es/investiga/grupos/ava/node/40.
- https://thinkrpi.wordpress.com/2013/05/22/opencv-and-camera-board-csi/.