簡體   English   中英

通過Raspberry PI使用OpenCV捕獲1920x1080視頻

[英]Capture 1920x1080 video with OpenCV over Raspberry PI

硬件:
1. Raspberry Pi 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 ) ;
}

看一下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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM