如何在opencv中设置相机fps?

时间:2016-12-08 18:16:20

标签: c++ opencv frame-rate

我正在使用支持1280 x 720 @ 60 fps的网络摄像头。

我的电脑环境是intel i5-4690K和Windows7,Visual Studio 2015,opencv 3.1

当我在Kinovea(0.85.15,https://www.kinovea.org/)运行网络摄像头时,相机以1280 x 720 @ 60fps的速度运行。

但是,在使用Opencv的Visual Studio中,它不能正常工作@ 60 fps。

它只能工作12~15 fps。

检查相机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_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;

}

在上面的代码中。我检查了#34;捕捉时间&#34;通常记录为0.07s~0.09s。

所以,我尝试使用VideoCapture :: set(CV_CAP_PROP_FPS,60),但它不起作用。 (当我使用代码VideoCapture :: get(CV_CAP_PROP_FPS)获取FPS时,它返回值0.)

如何控制网络摄像头FPS?

感谢。

2 个答案:

答案 0 :(得分:1)

当我修改我的代码时,如下所示,它的工作原理是@ 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;

}

摄像机工作的关键是@ 60 fps

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

我的相机在MJPG模式下工作@ 60 fps。所以我添加上面的代码,它工作正常!

答案 1 :(得分:0)

您可以尝试在OpenCV之外设置相机的帧速率,例如在Linux上,您可以使用libwebcamuvcdynctrl来控制UVC相机(Logitech等)。