raspicam : C++ opencv 调用树莓派的 PiCamera

raspicam这个库允许我们掉用 Raspberry   PiCamera.

可以直接去https://sourceforge.net/projects/raspicam/files/  这个网址下载库文件


提示:

对树莓派进行固件升级   执行命令(sudo  rpi-update )

主要特性

 - 提供类 RaspiCam 来简单完全的控制PiCamera
 - Provides class  RaspiCam_Still and RaspiCam_Still_Cv for controlling the camera in still mode
 - 提供类RaspiCam_Cv 来方便的调用opencv来控制PiCamera
 - Provides class  RaspiCam_Still and RaspiCam_Still_Cv for controlling the camera in still mode
 - 提供类 RaspiCam_Still 和 类RaspiCam_Still_Cv 为了调节相机的状况


raspicam的安装

1.下载最新的raspicam安装包之后 直接解压缩  

2.将解压之后的文件拖到 树莓派的用户目录下

3,(我下载的是raspicam-0.1.3 直接以此为例)  执行以下命令编译文件包

cd raspicam-0.1.3

mkdir build

cd build

cmake ..(cmake 跟..之间有空格的哦)

完成之后你将会看到向下面的文字

-- 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/buildIf 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.


最后编译,安装,更新ldconfig

make
sudo make install
sudo ldconfig

下面编写两个简单的示例程序

文件命名为simpletest_raspicam.cpp

#include
#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;
}

执行编译命令

g++ simpletest_raspicam.cpp -o  simpletest_raspicam -I/usr/local/include  -lraspicam
./simpletest_raspicam

第二个示例将会调用opencv接口

#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"<
}

执行编译命令
g++  simpletest_raspicam_cv.cpp -o   simpletest_raspicam_cv -I/usr/local/include/  -lraspicam  -lraspicam_cv   -lopencv_core -lopencv_highgui  -lopencv_imgcodecs 
 
thanks



你可能感兴趣的:(树莓派)