通过Raspberry PI在OpenCV上捕获1920x1080视频

时间:2015-10-15 22:38:46

标签: opencv raspberry-pi raspbian v4l2 v4l

设备:
 1.树莓派2  2. Raspberry Pi相机

软件:
 1. OpenCV 2.4.11
 2.使用C ++编程

我有以下简单的代码从相机捕获视频并在窗口中显示它 帧大小始终为640 x 480,尝试更改帧宽和高度(如标注行中所示)没有帮助,它保持640 x 480。

我正在寻找一种方法将帧宽和高度从我的代码更改为1920 x 1080(而不是使用shell)。
如果可以从OpenCV或V4l2驱动程序完成它将是伟大的

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[]) {

    int rc = 0 ;
    int device = 0 ;

    Mat frame;

    namedWindow( "Video", CV_WINDOW_AUTOSIZE ) ;

    VideoCapture capture( device ) ;
    if( capture.isOpened()) {

        cout << "Frame size : " << capture.get(CV_CAP_PROP_FRAME_WIDTH) << " x " << capture.get(CV_CAP_PROP_FRAME_HEIGHT) << endl ;

        //capture.set( CV_CAP_PROP_FRAME_WIDTH, 1920 ) ;
        //capture.set( CV_CAP_PROP_FRAME_HEIGHT, 1080 ) ;
        //capture.set( CV_CAP_PROP_FOURCC, CV_PROP('H','2','6','4')) ;
        //capture.set( CV_CAP_PROP_FOURCC, CV_PROP('M','J','P','G')) ;
        //capture.set( CV_CAP_PROP_FPS, 10 );

        for( ; ; ) {

            if( capture.read( frame )) {

                imshow( "Video", frame );

                if( waitKey( 1 ) == 27 ) {
                    cout << "Esc key pressed by the user" << endl ;
                    break ;
                }
            }
            else {
                rc = -1 ;
                cout << "Cannot read frame from video stream" << endl ;
                break ;
            }
        }
    }
    else {
        rc = -1 ;
        cout << "Cannot open the video device " << device << endl ;
    }

    return( rc ) ;
}

1 个答案:

答案 0 :(得分:2)

看看Raspicam项目。它可以帮助调整RPI上的摄像头分辨率并与OpenCV很好地集成。 https://github.com/cedricve/raspicam

编辑:在这里附上我的测试代码。它在RPI B2上对我很好。曝光值介于1到100000之间。

#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;
    Camera.set(CV_CAP_PROP_EXPOSURE, 100);
    cv::Mat image;
    int nCount=10;
    //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_100.jpg",image);
    cout<<"Image saved at raspicam_cv_image_100.jpg"<<endl;
}