opencv read grab retrieve 关系

VideoCapure里的read是grab和retrieve的结合,由下面的函数介绍可知grab是指向下一个帧,retrieve是解码并返回一个帧,而且retrieve比grab慢一些,所以当不需要当前的帧或画面时,可以使用grab跳过,与其使用read更省时间。因为有的时候缓冲区的画面是存在了延迟的。当不需要的时候可以多grab之后再read的话,就能比一直read更省时间,因为没有必要把不需要的帧解码,由介绍可知也可以使用grab实现硬件同步。


VideoCapture::grab

Grabs the next frame from video file or capturing device.

C++:  bool  VideoCapture:: grab ( )
Python: cv2.VideoCapture.grab() → retval
C:  int  cvGrabFrame (CvCapture* capture )
Python:   cv. GrabFrame (capture ) → int

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

The primary use of the function is in multi-camera environments, especially when the cameras do not have hardware synchronization. That is, you call VideoCapture::grab() for each camera and after that call the slower method VideoCapture::retrieve() to decode and get frame from each camera. This way the overhead on demosaicing or motion jpeg decompression etc. is eliminated and the retrieved frames from different cameras will be closer in time.

Also, when a connected camera is multi-head (for example, a stereo camera or a Kinect device), the correct way of retrieving data from it is to call VideoCapture::grab first and then call VideoCapture::retrieve() one or more times with different values of the channel parameter. See https://github.com/opencv/opencv/tree/master/samples/cpp/openni_capture.cpp


VideoCapture::retrieve

Decodes and returns the grabbed video frame.

C++:  bool  VideoCapture:: retrieve (Mat& image, int channel=0 )
Python:   cv2.VideoCapture. retrieve ( [image [, channel ] ] ) → retval, image
C:  IplImage*  cvRetrieveFrame (CvCapture* capture, int streamIdx=0  )
Python:   cv. RetrieveFrame (capture [, index ] ) → image

The methods/functions 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.

Note

 

OpenCV 1.x functions cvRetrieveFrame and cv.RetrieveFrame return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using cvCloneImage() and then do whatever you want with the copy.



VideoCapture::read

Grabs, decodes and returns the next video frame.

C++:  VideoCapture&  VideoCapture:: operator>> (Mat& image )
C++:  bool  VideoCapture:: read (Mat& image )
Python:   cv2.VideoCapture. read ( [image ] ) → retval, image
C:  IplImage*  cvQueryFrame (CvCapture* capture )
Python:   cv. QueryFrame (capture ) → image

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.

Note

 

OpenCV 1.x functions cvRetrieveFrame and cv.RetrieveFrame return image stored inside the video capturing structure. It is not allowed to modify or release the image! You can copy the frame using cvCloneImage() and then do whatever you want with the copy.

参考: 点击打开链接


猜你喜欢

转载自blog.csdn.net/zhenguo26/article/details/80744262