从 OpenCV 3 切换到 OpenCV 4 会导致网络摄像头以最大 5 fps 的速度录制,而不是通常的 30 f

Switching from OpenCV 3 to OpenCV 4 causes webcam to record at a max of 5 fps instead the usual 30 fps

本文关键字:OpenCV 速度 fps 摄像头 网络      更新时间:2023-10-16

自从我在C++项目中从OpenCV 3.x更改为4.x(从源代码编译(以来,我遇到了一些麻烦。我在一个小示例中复制了这种行为,该示例仅打开网络摄像头并记录 5 秒钟。

使用 3.x,我可以将全高清网络摄像头帧速率设置为 30,但 4.x 的相同代码只是忽略camera.set(cv::CAP_PROP_FPS,30)并将其设置为 5。如果我使用 720p,则 fps 设置为 10。

也许代码在这里无关紧要,因为它是一个经典的例子,但以防万一我会把它留在这里。

#include "opencv2/opencv.hpp"
#include "iostream"
#include "thread"
#include <unistd.h>
using namespace cv;
VideoCapture camera(0);
bool stop = false;
int fc = 0;
void saveFrames()
{
while(!stop)
{
Mat frame;
camera >> frame;
cv::imwrite("/tmp/frames/frame" + std::to_string(fc) + ".jpg", frame);
fc++;
}
}
int main()
{
if(!camera.isOpened())
return -1;
camera.set(cv::CAP_PROP_FRAME_WIDTH,1920);
camera.set(cv::CAP_PROP_FRAME_HEIGHT,1080);
camera.set(cv::CAP_PROP_FPS,30);
double fps = camera.get(cv::CAP_PROP_FPS);
std::cout << "FPS setting: " << fps << std::endl; // 5 with OCV4, 30 with OCV3
std::thread tr(saveFrames);
int waitSeconds = 5;
usleep(waitSeconds * 1e6);
stop = true;
tr.join();
std::cout << "Written " << fc << " frames of " << fps * waitSeconds << std::endl;
return 0;
}

编辑:与其他计算机进行的更多测试会产生相同的结果,除了在Macbook Pro(但运行相同的发行版(中,OpenCV 4.3似乎可以工作。其他 2 台计算机是带有 USB 网络摄像头的台式机。

编辑 2:从源代码构建 3.4 版本存在相同的问题。目前,回购中只有 3.2 在两台带有 usbcams 的计算机上工作正常。

这是一个已知的错误,会影响OpenCV> 3.3