我正在编写一个简单的OpenCV示例,在5秒内获得网络摄像头帧(如果摄像头工作在30 cam,则大约有150帧)。
在做了一些测试之后,我发现有时我从150帧中得到140块(我想是可以接受的),但有时是70块。更糟糕的是,有时摄像机似乎会受到这些镜头的影响,并在这种状态下停留数小时。
为了进一步研究这个问题,我删除了我的程序,直到我仍然有这个问题,即使我只读取帧,但不要将它们写到磁盘。我设置了一个cron作业,以便每分钟运行一次5秒的捕获,我已经看到了这样的情况:

我认为第一个小的下降是由于系统繁忙,但大的,永久的一个发生在半夜。早上,我停止了cron的工作,触摸了代码中的一些内容(我不记得到底是什么),然后再次开始测试,看到一个逐渐恢复的过程,然后在2-3小时后出现新的下降:

从昨天起,我关闭了电脑几个小时,再次启动它,并覆盖网络摄像头,以确保持续的光照条件,但帧计数仍然很低,并停留在70。而且,非常奇怪的是,下降(150帧中的70帧)正好是我在这台相机中看到的最大帧的一半(150帧中有140帧)。
网络摄像头模型是罗技C525。我还在2016年末的Macbook Pro Facetime时高清摄像头上进行测试,在那里,我看到了150帧中的117帧。我的一个同事也看到他的笔记本电脑里的镜框掉了下来。我的代码有问题吗?它会是线程优先级吗?
// Call the program like this: ./cameraTest pixelWidth pixelHeight fps timeLimit
// timeLimit can be 1 to run a fixed 5-seconds capture, or 0 to wait for 150 frames.
#include "opencv2/opencv.hpp"
#include "iostream"
#include "thread"
#include <unistd.h>
#include <chrono>
#include <ctime>
#include <experimental/filesystem>
using namespace cv;
namespace fs = std::experimental::filesystem;
VideoCapture camera(0);
bool stop = false;
int readFrames = 0;
std::string getTimeStamp()
{
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%Y-%m-%d %H:%M:%S",timeinfo);
std::string timeStamp(buffer);
return timeStamp;
}
void getFrame()
{
Mat frame;
camera >> frame;
// camera.read(frame);
// cv::imwrite("/tmp/frames/frame" + std::to_string(readFrames) + ".jpg", frame);
readFrames++;
}
void getFrames()
{
Mat frame;
while(!stop)
{
camera >> frame;
// cv::imwrite("/tmp/frames/frame" + std::to_string(fc) + ".jpg", frame);
readFrames++;
}
}
int main(int argc, char* argv[])
{
if(argc < 5)
{
std::cout << "Usage: width height fps timeLimit" << std::endl;
return -1;
}
if(!camera.isOpened())
{
std::cout << "Couldn't open camera " << getTimeStamp() << std::endl;
return -1;
}
if (!fs::is_directory("/tmp/frames"))
{
if(system("mkdir -p /tmp/frames") != 0)
{
std::cout << "Error creating /tmp/frames/" << std::endl;
}
}
if (!fs::is_empty("/tmp/frames"))
{
system("exec rm /tmp/frames/*");
}
camera.set(CV_CAP_PROP_FRAME_WIDTH, atoi(argv[1]));
camera.set(CV_CAP_PROP_FRAME_HEIGHT, atoi(argv[2]));
camera.set(CV_CAP_PROP_FPS, atoi(argv[3]));
//camera.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));
bool timeLimit(atoi(argv[4]));
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int waitSeconds = 5;
if(timeLimit)
{
std::thread tr(getFrames);
usleep(waitSeconds * 1e6);
stop = true;
tr.join();
}
else
{
while(readFrames < 150)
{
getFrame();
}
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << getTimeStamp() << " " << readFrames << "/" << atoi(argv[3]) * waitSeconds << " "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "ms"
<< " " << atoi(argv[1]) << "," << atoi(argv[2]) << "," << atoi(argv[3])
<< std::endl;
return 0;
}发布于 2020-04-01 09:48:21
这似乎与光线不足有关。当我发现卡马拉时,我就看到这些框架达到了预期的价值。因此,在光线较差的情况下,相机可能会改变一些设置或进一步处理图像,从而降低其帧率。

https://stackoverflow.com/questions/60967068
复制相似问题