How to set camera fps in opencv?

14k views Asked by At

I'm using a webcam supporting 1280 x 720 @ 60 fps.

My computer environment is intel i5-4690K and Windows7, Visual studio 2015, opencv 3.1

When I run the webcam in Kinovea(0.85.15, https://www.kinovea.org/), the camera run at the 1280 x 720 @ 60fps.

But, In Visual studio with Opencv, it isn't work @ 60 fps.

It just work only 12~15 fps.

My code for checking the camera fps is below.

#include <stack>
#include <iostream>
#include <math.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/video.hpp>
#include "opencv2/imgcodecs.hpp"
#include <time.h>



using namespace cv;
using namespace std;


int keyboard;


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

    VideoCapture cap(0); //capture the video from web cam

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the web cam" << endl;
        return -1;
    }
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);

    while ((char)keyboard != 'q' && (char)keyboard != 27)

    {
        Mat imgOriginal;
        Mat ROOI;

        clock_t a = clock();
        bool bSuccess = cap.read(imgOriginal); 

        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }
        printf("Captue Time : %f\n", double(clock() - a) / double(CLOCKS_PER_SEC));

        imshow("Original", imgOriginal);

        if (waitKey(1) == 27) 
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }

    return 0;

}

In above code. I check the "Capture Time" and it was usually records 0.07s ~ 0.09s.

So, I attempt to VideoCapture::set(CV_CAP_PROP_FPS, 60), but it isn't work. (When I get the FPS using the code VideoCapture::get(CV_CAP_PROP_FPS), it return value 0.)

How can I control the webcam FPS?

Thanks.

2

There are 2 answers

3
szym On

You could try setting the camera's frame rate outside of OpenCV, e.g. on Linux you can control UVC cameras (Logitech, etc.) using libwebcam, and uvcdynctrl in particular.

1
H.Ann On

When I modify my code like below, it works @ 60 fps.

#include <stack>
#include <iostream>
#include <math.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/video.hpp>
#include "opencv2/imgcodecs.hpp"
#include <time.h>



using namespace cv;
using namespace std;


int keyboard;


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

    VideoCapture cap(0); //capture the video from web cam

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the web cam" << endl;
        return -1;
    }
    cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);

    while ((char)keyboard != 'q' && (char)keyboard != 27)

    {
        Mat imgOriginal;
        Mat ROOI;

        clock_t a = clock();
        bool bSuccess = cap.read(imgOriginal); 

        if (!bSuccess)
        {
            cout << "Cannot read a frame from video stream" << endl;
            break;
        }
        printf("Captue Time : %f\n", double(clock() - a) / double(CLOCKS_PER_SEC));

        imshow("Original", imgOriginal);

        if (waitKey(1) == 27) 
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }

    return 0;

}

The key for camera working @ 60 fps is

cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));

My camera works @60 fps in MJPG mode. So I add above code, it works fine!