Ask Your Question
0

Skipping all but the latest frame in VideoCapture

asked 2016-01-01 14:09:19 -0600

Kellerspeicher gravatar image

updated 2016-01-02 12:47:37 -0600

As an answer to the question "Skipping frames in VideoCapture" Will Stewart presented grap() for skipping frames. But how many frames to I have to skip until I reach the current one?

In a loop the frames of a camera are processed using a very time consuming function. In the meantime the camera is providing further frames. Thus if the loop is doing the next vcap.read() it gets an old buffered frame. That delays the output even further than it is already delayed by the processing anyway. How to skip all of theses frames. I am looking for something like vcap.flush_read_buffer() except for the latest frame.

Some sort of non blocking grap() would also do the job while(vcap.grap()); and then vcap.retrieve() the last successful graped frame. Or a function providing a flag if frames are buffered while(vcap.buffered()) vcap.grap(). Any of these would be better that nothing.

Doing multi-threading (like harsha proposed) would surely do the job. But it is like taking a sledgehammer to crack a nut. Especially on an embedded system this won't feel good. Processing as many frames of a camera as possible but these just in time, should be possible on a system designed "with a strong focus on real-time applications."

Any further idea?

edit retag flag offensive close merge delete

Comments

Did you solve this in the end? I'm surprised there's not more people interested in this issue, for an embedded environment multi-threading is indeed both overkill and resource consuming

fteys gravatar imagefteys ( 2018-01-25 03:22:09 -0600 )edit

Hi... thanks for you good understanding issue. i have same problem. do you think opencv team not gave any idea still for this problem ? are you find better idea for skip multi threading or another low efficient ways ? thanks in advance

arsh gravatar imagearsh ( 2018-06-06 10:37:05 -0600 )edit

btw, grab() , not grap()

berak gravatar imageberak ( 2018-10-04 03:31:45 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2018-10-04 02:46:30 -0600

I had the same issue. Any VideoCapture property that could have been useful returned -1.0 in my case, so I had to figure it out myself. I made a simple program that reads and displays a frame from camera on a key press:

VideoCapture camera(0);
Mat frame;
while(true) {
    camera.read(frame);
    imshow("window", frame);
    waitKey();
}

I put something in front of the camera and counted how many frames it took until it appeared on the screen, then took it away and did the same. Turns out in my case there are exactly 5 frames buffered. And as new frames are available, the last one is being overwritten. So if my slow running operation takes more than 5 frames, I can easily use the following:

...
for(int i = 0; i < 5; i++) {
    camera.grab();
}
camera.retrieve(frame);
process(frame);
...

In the worst case, if your operation is instant, you will have to wait for 4 extra frames, so it will reduce your fps to 20%. But either way, the displayed image, once you get it, will be "current".

edit flag offensive delete link more

Comments

be aware though, that this is only a problem on linux, using the V4L capture backend.

it has an internal fifo (of iirc 5 images), if your loop needs too long, it will fall behind.

berak gravatar imageberak ( 2018-10-04 03:34:19 -0600 )edit
0

answered 2016-01-01 18:39:42 -0600

harsha gravatar image

updated 2016-01-01 18:58:13 -0600

You can keep reading frames and save them in to a buffer of size n, and throw out old frames making way for new ones. While all the capture and save to buffer runs in one thread, processing can be done in a separate thread which has access to the buffer. This way you will always have access to n latest frames and n can be chosen depending on the application.

edit flag offensive delete link more

Comments

Yes thanks. Looks like plenty of effort just to clean a read buffer. But it would be a workaround.

Kellerspeicher gravatar imageKellerspeicher ( 2016-01-02 03:45:28 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2016-01-01 14:08:32 -0600

Seen: 14,255 times

Last updated: Jan 02 '16