如何在opencv中设置相机fps? [英] How to set camera fps in opencv?

查看:561
本文介绍了如何在opencv中设置相机fps?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的计算机环境是Intel i5-4690K和Windows7,Visual Studio 2015,opencv 3.1

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

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

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

但是,在带有Opencv的Visual Studio中,以60 fps的速度无法正常工作.

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

它只能以12〜15 fps的速度工作.

It just work only 12~15 fps.

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

}

在上面的代码中.我检查了捕获时间",它通常记录为0.07s〜0.09s.

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

因此,我尝试使用VideoCapture :: set(CV_CAP_PROP_FPS,60),但这是行不通的. (当我使用代码VideoCapture :: get(CV_CAP_PROP_FPS)获取FPS时,它返回值0.)

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.)

如何控制摄像头的FPS?

How can I control the webcam FPS?

谢谢.

推荐答案

当我如下修改代码时,它以60 fps的速度工作.

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;

}

相机以60 fps的速度工作的关键是

The key for camera working @ 60 fps is

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

我的相机在MJPG模式下以@ 60 fps的速度工作.因此,我添加了上面的代码,效果很好!

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

这篇关于如何在opencv中设置相机fps?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆