0

硬件:
1.树莓派 2
2.树莓派相机

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

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

我正在寻找一种将框架宽度和高度从我的代码(而不是外壳)更改为 1920 x 1080 的方法。
如果可以从 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 ) ;
}
4

1 回答 1

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;
}
于 2016-04-09T04:52:47.500 回答