Home >Backend Development >C++ >How to Overcome Lag in OpenCV VideoCapture Caused by Capture Buffer Accumulation?
You have encountered a lag issue with OpenCV's VideoCapture due to the accumulation of frames in the capture buffer. As a workaround, you have resorted to reading the buffer at 30fps to clear it quickly.
OpenCV provides a way to set the buffer size manually:
<code class="cpp">cv::VideoCapture cap; cap.set(CV_CAP_PROP_BUFFERSIZE, 3); // store only 3 frames</code>
However, this solution is only supported by the DC1394 v 2.x backend.
An alternative approach is to measure the time it takes to retrieve a frame:
You can create a third thread that continuously grabs frames at a high speed using cv::VideoCapture.grab() to keep the buffer empty. Synchronize access between this thread and your worker thread using a spin-lock.
The above is the detailed content of How to Overcome Lag in OpenCV VideoCapture Caused by Capture Buffer Accumulation?. For more information, please follow other related articles on the PHP Chinese website!