且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

由于捕获缓冲区,OpenCV VideoCapture延迟

更新时间:2021-12-26 15:34:15

根据源代码,您可以设置 cv :: VideoCapture的buffersize $ c>对象。

According to this source, you can set the buffersize of a cv::VideoCapture object.

cv::VideoCapture cap;
cap.set(CV_CAP_PROP_BUFFERSIZE, 3); // internal buffer will now store only 3 frames

// rest of your code...

但是有一个重要的限制:

There is an important limitation however:


CV_CAP_PROP_BUFFERSIZE内部缓冲存储器中存储的帧数(注意:只支持DC1394 v 2.x后端






该解决方案不起作用,请查看这个帖子解释了如何解决这个问题。


If the solution does not work, take a look at this post that explains how to hack around the issue.

简而言之:查询框架所需的时间是测量的;如果它太低,则意味着帧被从缓冲器读取并且可以被丢弃。继续查询帧,直到测量的时间超过某个限制。发生这种情况时,缓冲区为空,返回的帧是最新的。

In a nutshell: the time needed to query a frame is measured; if it is too low, it means the frame was read from the buffer and can be discarded. Continue querying frames until the time measured exceeds a certain limit. When this happens, the buffer was empty and the returned frame is up to date.

(链接的帖子显示:从缓冲区返回一个帧大约需要1 /

(The answer on the linked post shows: returning a frame from the buffer takes about 1/8th the time of returning an up to date frame. Your mileage may vary, of course!)

不同的解决方案,受到帖子的启发,是创建一个连续抓取框架的第三个线程以高速保持缓冲器空。此主题应使用 cv :: VideoCapture.grab() 以避免开销。

A different solution, inspired by this post, is to create a third thread that grabs frames continuously at high speed to keep the buffer empty. This thread should use the cv::VideoCapture.grab() to avoid overhead.

您可以使用简单的自旋锁来同步实际工作线程和第三线程之间的读取帧。

You could use a simple spin-lock to synchronize reading frames between the real worker thread and the third thread.