Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between video capture read and grab

Tags:

python

opencv

I'm not doing nothing in particular, but i'm reading the opencv documentation and I can't understand the difference between the method read and grab from the VideoCapture, here this what they do:

grab: The methods/functions grab the next frame from video file or camera and return true (non-zero) in the case of success.

read: decodes and returns the next video frame.

In the two method extract the next frame. what is the difference? Link to the documentation

like image 638
Dogo-San Avatar asked Aug 29 '19 19:08

Dogo-San


1 Answers

There's a subtle yet significant difference that becomes more obvious when taking a closer look at the documentation for each of the two methods.

  • grab "only" gets the image from the camera and holds it for further processing:

    The methods/functions grab the next frame from video file or camera and return true (non-zero) in the case of success. [...] you call VideoCapture::grab() for each camera and after that call the slower method VideoCapture::retrieve() to decode and get frame from each camera.

    In this case, you still have to retrieve the frame (i.e. "decode and get") from OpenCV so that you can work with it. grab only returns a boolean that indicates whether OpenCV was able to get the frame from the camera - the return parameter in this case is not the frame.

  • read combines both grab and retrieve operations into one command and returns the already decoded frame:

    The methods/functions combine VideoCapture::grab() and VideoCapture::retrieve() in one call. This is the most convenient method for reading video files or capturing data from decode and return the just grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.

    This method returns the actual frame back to your application so you don't have to worry about checking the boolean parameter and writing an additional line of code to get the frame back by OpenCV.

Depending on your application it may be easier to just use read as it will likely save you one or two lines of code.

like image 167
m_____z Avatar answered Oct 08 '22 22:10

m_____z