RaspiCam: C++ API for using Raspberry camera with/without OpenCv
Main features
Compiling
tar xvzf raspicamxx.tgz cd raspicamxx mkdir build cd build cmake ..
make sudo make install sudo ldconfig
Using it in your projects
Download the project example at SourceForge
#include <ctime> #include <fstream> #include <iostream> #include <raspicam/raspicam.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; }
cmake_minimum_required (VERSION 2.8) project (raspicam_test) find_package(raspicam REQUIRED) add_executable (simpletest_raspicam simpletest_raspicam.cpp) target_link_libraries (simpletest_raspicam ${raspicam_LIBS})
OpenCV Interface
#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 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; }
cmake_minimum_required (VERSION 2.8) project (raspicam_test) find_package(raspicam REQUIRED) find_package(OpenCV) IF ( OpenCV_FOUND AND raspicam_CV_FOUND) MESSAGE(STATUS "COMPILING OPENCV TESTS") add_executable (simpletest_raspicam_cv simpletest_raspicam_cv.cpp) target_link_libraries (simpletest_raspicam_cv ${raspicam_CV_LIBS}) ELSE() MESSAGE(FATAL_ERROR "OPENCV NOT FOUND IN YOUR SYSTEM") ENDIF()
mkdir build cd build cmake .. make ./simpletest_raspicam_cv
g++ simpletest_raspicam_cv.cpp -o simpletest_raspicam_cv -I/usr/local/include/ -lraspicam -lraspicam_cv -lmmal -lmmal_core -lmmal_util -lopencv_core -lopencv_highgui