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<Variable>=<Value>

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

执行编译命令

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

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

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

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



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值