gpt4 book ai didi

c++ - 如何在opencv中设置相机fps?

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:39 27 4
gpt4 key购买 nike

我使用的是支持 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;

}

在上面的代码中。我查看“Capture Time”,通常是记录0.07s ~ 0.09s。

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

如何控制网络摄像头 FPS?

谢谢。

最佳答案

当我像下面这样修改我的代码时,它以 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 帧的速度工作。所以我添加了上面的代码,它工作正常!

关于c++ - 如何在opencv中设置相机fps?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41046188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com