Ask Your Question

Revision history [back]

How to get newest VideoCapture frame

Hello, I'm trying to load the very newest frame from a usb webcam; the following code works well enough using v3.1.0 via Codeblocks on Windows 7, I get a 1 sec delay between frame1 and frame2, and 2sec delay between frame2 and frame3, not exactly how the program reads. Problem is when I copy it onto a Pi3, using v2.4.13, Rasbian, the delay is ignored completely (or interrupted ??). I've tried using sleep() with unistd.h; I get a total delay of 6 seconds, but the cap.read() statements return consecutive frames about .04secs apart and not the 1, 2 or 3 second delay I want, because it's retrieving old frames in the buffer I assume. So how can I always get the very newest frame from VideoCapture? Any help would be great, I'm new to C++ and OpenCV. Thanks

#include <opencv cv.hpp=""> #include <iostream> #include <time.h> using namespace cv; using namespace std;

void delay(unsigned int mseconds){ clock_t goal = mseconds + clock(); while (goal> clock()); }

Mat frame1, frame2, frame3; VideoCapture cap(0);

int main(){ delay(1000); cap.read(frame1); delay(2000); cap.read(frame2); delay(3000); cap.read(frame3); imshow("1",frame1); imshow("2",frame2); imshow("3",frame3); waitKey(0); return 0; }

click to hide/show revision 2
No.2 Revision

updated 2016-10-28 08:42:08 -0600

berak gravatar image

How to get newest VideoCapture frame

Hello, I'm trying to load the very newest frame from a usb webcam; the following code works well enough using v3.1.0 via Codeblocks on Windows 7, I get a 1 sec delay between frame1 and frame2, and 2sec delay between frame2 and frame3, not exactly how the program reads. Problem is when I copy it onto a Pi3, using v2.4.13, Rasbian, the delay is ignored completely (or interrupted ??). I've tried using sleep() with unistd.h; I get a total delay of 6 seconds, but the cap.read() statements return consecutive frames about .04secs apart and not the 1, 2 or 3 second delay I want, because it's retrieving old frames in the buffer I assume. So how can I always get the very newest frame from VideoCapture? Any help would be great, I'm new to C++ and OpenCV. Thanks

 #include <opencv cv.hpp="">
<opencv/cv.hpp>
 #include <iostream>
 #include <time.h>
using namespace cv;
using namespace std;

std; void delay(unsigned int mseconds){ clock_t goal = mseconds + clock(); while (goal> clock()); }

} Mat frame1, frame2, frame3; VideoCapture cap(0);

cap(0); int main(){ delay(1000); cap.read(frame1); delay(2000); cap.read(frame2); delay(3000); cap.read(frame3); imshow("1",frame1); imshow("2",frame2); imshow("3",frame3); waitKey(0); return 0; }

}