官方原文地址:http://www.uco.es/investiga/grupos/ava/node/40
RaspiCam: 树莓派获取图像的C++库(支持OPENCV)
下载地址:https://github.com/cedricve/raspicam
PS:转载此文仅便于博主回顾,阅读者一般都为早已CET-4,所以博主不再翻译。
原文参考:
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..."< 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_raspicamIf you do not like cmake, simply
g++ simpletest_raspicam.cpp -o simpletest_raspicam -I/usr/local/include -lraspicam -lmmal -lmmal_core -lmmal_utilOpenCV 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..."< 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_cvIf 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_highguior similar
关于编译方法:
官方提供了CMakeLists.txt,可以使用cmake 明天编译链接,当然我们也可以自己编写Makefile文件,使用make 命令编写。
我的程序文本文档命名为:main.cppCC := gcc CXX := g++ Target_Object := main INCLUDE_PATH :=-I/usr/local/include LIBS_PATH :=-L/usr/local/lib LIBS := -lraspicam All:main.o $(CXX) $(LIBS_PATH) $(LIBS) main.o -o $(Target_Object) main.o:main.cpp $(CXX) -c $(INCLUDE_PATH) main.cpp -o main.o clean: rm -rf ./main.o $(Target_Object)解释:
Target_Object := main //生成的目标文件名称 INCLUDE_PATH :=-I/usr/local/include //raspicam include 目录 LIBS_PATH :=-L/usr/local/lib //raspicam 库目录 LIBS := -lraspicam //raspicam 库(libraspicam.so => -lraspicam) All:main.o $(CXX) $(LIBS_PATH) $(LIBS) main.o -o $(Target_Object) main.o:main.cpp $(CXX) -c $(INCLUDE_PATH) main.cpp -o main.o