由于捕獲緩沖區,OpenCV VideoCapture延遲 [英] OpenCV VideoCapture lag due to the capture buffer

查看:14330
本文介紹了由于捕獲緩沖區,OpenCV VideoCapture延遲的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我通過網絡攝像頭捕獲視頻,該網絡攝像頭會提供mjpeg流。
我在工作線程中捕獲了視頻。
我這樣開始捕獲:

I am capturing video through a webcam which gives a mjpeg stream. I did the video capture in a worker thread. I start the capture like this:

const std::string videoStreamAddress = "http://192.168.1.173:80/live/0/mjpeg.jpg?x.mjpeg";
qDebug() << "start";
cap.open(videoStreamAddress);
qDebug() << "really started";
cap.set(CV_CAP_PROP_FRAME_WIDTH, 720);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 576);

相機正在以20fps的速度饋送流。
但是如果我在20fps中這樣讀?。?em class="showen">

the camera is feeding the stream at 20fps. But if I did the reading in 20fps like this:

if (!cap.isOpened()) return;

        Mat frame;
        cap >> frame; // get a new frame from camera
        mutex.lock();

        m_imageFrame = frame;
        mutex.unlock();

然后有3+秒的滯后。
原因是捕獲的視頻首先存儲在緩沖區中。當我第一次啟動相機時,緩沖區已累積,但我沒有讀取幀。所以如果我從緩沖區讀取它總是給我舊的幀。
現在我唯一的解決方案是讀取30fps的緩沖區,所以它會快速清理緩沖區,沒有更嚴重的滯后。

Then there is a 3+ seconds lag. The reason is that the captured video is first stored in a buffer.When I first start the camera, the buffer is accumulated but I did not read the frames out. So If I read from the buffer it always gives me the old frames. The only solutions I have now is to read the buffer at 30fps so it will clean the buffer quickly and there's no more serious lag.

有沒有其他可能的解決方案,使我可以清理/刷新緩沖區手動每次我開始相機?

Is there any other possible solution so that I could clean/flush the buffer manually each time I start the camera?

推薦答案

根據源代碼,您可以設置 cv :: VideoCapture的buffersize 對象。

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.

這篇關于由于捕獲緩沖區,OpenCV VideoCapture延遲的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持IT屋!

查看全文
登錄 關閉
掃碼關注1秒登錄
發送“驗證碼”獲取 | 15天全站免登陸
全免费A级毛片免费看无码播放