teach me
teach me

Reputation: 485

Capture 1920x1080 video with OpenCV over Raspberry PI

Hardware:
1. Raspberry Pi 2
2. Raspberry Pi Camera

Software:
1. OpenCV 2.4.11
2. Programming with C++

I've the following trivial code that capture video from camera and displays it within window.
The frame size is always 640 x 480, trying to change the frame width and height (as shown in the remarked lines) doesn't help and it stays 640 x 480.

I'm looking for a way to change the frame width and height to 1920 x 1080 from my code (and not with the shell).
If it can be done from OpenCV or V4l2 driver it would be great

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

Upvotes: 0

Views: 2223

Answers (1)

Tony Luo
Tony Luo

Reputation: 110

Take a look at Raspicam project. It could help to adjust the resolution of camera on RPI and intergrated with OpenCV well. https://github.com/cedricve/raspicam

Edited: Attached my test code here. It works fine for me on RPI B2. The exposure value is between 1 to 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;
}

Upvotes: 2

Related Questions