# How can I get the most current frame in OpenCV?

**URL:** <https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635>\
**Category:** C++\
**Created:** [October 21, 2022, 6:53pm UTC](https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635 "2022-10-21T18:53:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piter217](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/piter217/32/6315_2.png) [@Piter217](https://forum.opencv.org/u/Piter217)\
**Post date:** [October 21, 2022, 6:53pm UTC](https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635/1 "2022-10-21T18:53:31Z")

</div>

I’m trying to take and display the most current frame on any key event. It is important for me that the photo will be taken only once (without using infinity loop). I have a problem because displayed photo is the previous one not the most current. It looks like some buffor with captured frames in VideoCapture object.

I’m using OpenCV 4.6.0 and laptop camera.

```auto
#include <iostream>
#include <opencv2/opencv.hpp>

int main() {
    cv::VideoCapture cap0(0);

    cv::Mat frame0;
    for (;;)
    {
        cap0.read(frame0);
        if (!frame0.empty())
        {
            imshow("cap0", frame0);
        }
        cv::waitKey(0);
    }
    return 0;
}

```

Is it possible to get and display current photo without doing infinity loop whenever I click any key?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 21, 2022, 7:06pm UTC](https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635/2 "2022-10-21T19:06:37Z")

</div>

crosspost:

> <https://stackoverflow.com/questions/74145112/how-can-i-get-the-most-current-frame-in-opencv>

---

<div class="post-metadata">

**Author:** ![Rukerin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/rukerin/32/6326_2.png) [@Rukerin](https://forum.opencv.org/u/Rukerin)\
**Post date:** [October 24, 2022, 4:02am UTC](https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635/3 "2022-10-24T04:02:16Z")

</div>

Hello Piter217,  
this might not be the best or smartest solution but you can capture 4 frames before calling imshow(). This will clear the old frames and leave you with the most current frame. You can do this by calling cap0.read(frame0); multiple times.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 24, 2022, 9:20am UTC](https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635/4 "2022-10-24T09:20:23Z")

</div>

> [@Rukerin](#):
>
> 4

note that _that_ number is arbitrary and not justified by any technical fact. there are some hacks to flush the queue, such as setting buffer size to 0 or 1 or something else small that works. the _usual_ way is to run capture in a separate thread and keep a reference to the last captured frame, for the rest of the program to access whenever needed.

the initial post should simply **not** use `waitKey(0)` but **instead** `waitKey(1)` or `pollKey()`. all of this was already discussed on Stack Overflow

---

<div class="post-metadata">

**Author:** ![Rukerin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/rukerin/32/6326_2.png) [@Rukerin](https://forum.opencv.org/u/Rukerin)\
**Post date:** [October 24, 2022, 10:45pm UTC](https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635/5 "2022-10-24T22:45:27Z")

</div>

Thank you for clarifying, I was under the impression that the buffer size defaulted to store 4 frames.

---

<div class="post-metadata">

**Author:** ![FilipBaas](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/filipbaas/32/802_2.png) [@FilipBaas](https://forum.opencv.org/u/FilipBaas)\
**Post date:** [October 26, 2022, 8:50pm UTC](https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635/6 "2022-10-26T20:50:42Z")

</div>

Another way of getting the most current frame from camera might be measuring time of grabing. Usual grab times for web cameras are 20 - 40 ms. If grabbing is faster than this time, you can assume that the frame was copied from buffer, so you can grab again unitil the grabbing time is more than threshold: ~20 ms (50 FPS).

Make sure that the actual grab time is more than the threshold, your code might end up in infinite loop.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 9, 2024, 8:31am UTC](https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635/7 "2024-02-09T08:31:12Z")

</div>

Split this topic on February 9, 2024, 8:31am UTC

A post was merged into an existing topic: [How would you synchronize two webcams to be acquiring their images at the same times?](https://forum.opencv.org/t/how-would-you-synchronize-two-webcams-to-be-acquiring-their-images-at-the-same-times/16409/4)

---

<div class="post-metadata">

**Author:** ![matan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/matan/32/1521_2.png) [@matan](https://forum.opencv.org/u/matan)\
**Post date:** [June 3, 2024, 11:21am UTC](https://forum.opencv.org/t/how-can-i-get-the-most-current-frame-in-opencv/10635/8 "2024-06-03T11:21:36Z")

</div>

Usual grab times for web cameras are not 20 - 40 ms. The grab time depends on whether your polling the camera to grab comes after it’s grabbed its latest already or whether it’s waiting on its cadence of the requested frame rate (if you’re in streaming mode at least). And it can be even more unexpected since different cameras may have different logic implemented, as the UVC standard keeps things rather open, or conformance to some of its details may not be as stellar. That’s before mentioning host drivers.

It is highly variable, in any event, when putting the webcam into streaming mode at least. Have you been grabbing outside of a streaming mode?
