This library 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.
Update 2015/02/28: version 0.1.3 released Download at SourceForge
Notes:
Requires to update the firmware to use shutterspeed (sudo rpi-update)
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
Compiling
Download the file to your raspberry. Then, uncompress the file and compile
tar xvzf raspicamxx.tgz
cd raspicamxx
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=
--
-- 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 raspicam_test and raspicam_cv_test (if opencv was enabled).
Run the first program to check that compilation is ok.
Using it in your projects
Download the project example at SourceForge
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 with cmake.
First, create a directory for our own project. Then, go in and create a file with the name simpletest_raspicam.cpp and add the following code
/**
*/
#include
#include
#include
#include
using namespace std;
int main ( int argc,char **argv ) {
raspicam::RaspiCam Camera; //Cmaera object
//Open camera
cout<<"Opening Camera..."<
if ( !Camera.open()) {cerr<<"Error opening camera"<
//wait a while until camera stabilizes
cout<<"Sleeping for 3 secs"<
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"<
outFile.write ( ( char* ) data, Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_RGB ) );
cout<<"Image saved at raspicam_image.ppm"<
//free resrources
delete data;
return 0;
}
For cmake users, create a file named CMakeLists.txt and add:
#####################################
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})
#####################################
Finally, create build dir,compile and execute
mkdir build
cd build
cmake ..
make
./simpletest_raspicam
If you do not like cmake, simply
g++ simpletest_raspicam.cpp -o
simpletest_raspicam -I/usr/local/include
-lraspicam -lmmal -lmmal_core -lmmal_util
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 using cmake.
First create a file with the name simpletest_raspicam_cv.cpp and add the following code
#include
#include
#include
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..."<
if (!Camera.open()) {cerr<<"Error opening the camera"<
//Start capture
cout<<"Capturing "<
time ( &timer_begin );
for ( int i=0; i
Camera.grab();
Camera.retrieve ( image);
if ( i%5==0 ) cout<<"\r captured "<
}
cout<<"Stop camera..."<
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 ) <
//save image
cv::imwrite("raspicam_cv_image.jpg",image);
cout<<"Image saved at raspicam_cv_image.jpg"<
}
For cmake users, create a file named CMakeLists.txt and add:
#####################################
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()
#####################################
Finally, create,compile and execute
mkdir build
cd build
cmake ..
make
./simpletest_raspicam_cv
If you do not like cmake:
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
or similar
make
seems to have completed without error. There's a.so
inbuild/src
and some tests inbuild/utils
. – goldilocks ♦ Nov 10 '14 at 15:33