2

I want to query frames from the the middle of a video, beginning from some starting frame. Following OpenCV Seek Function/Rewind, I use CV_CAP_PROP_POS_FRAMES.

To verify that it works as I expect, I seek to Frame 10 and print a slice from the image. Then I seek to Frame 10 again and print the same slice.

In [1]: import cv

In [2]: capture = cv.CaptureFromFile('T35V1.MOV')

In [3]: cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES, 10)
Out[3]: 1

In [4]: np.asarray(cv.GetMat(cv.QueryFrame(capture)))[100] # an arbitrary slice
Out[4]: 
array([[ 36, 104, 120],
       [ 36, 104, 120],
       [ 36, 107, 123],
       ..., 
       [ 50, 114, 136],
       [ 54, 115, 136],
       [ 55, 116, 137]], dtype=uint8)

In [5]: cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES, 10)
Out[5]: 1

In [6]: np.asarray(cv.GetMat(cv.QueryFrame(capture)))[100]
Out[6]: 
array([[ 32, 107, 119],
       [ 32, 107, 119],
       [ 35, 109, 124],
       ..., 
       [ 42, 114, 135],
       [ 47, 118, 139],
       [ 50, 121, 142]], dtype=uint8)

The data do not agree. Am I misusing this?

Related: http://code.opencv.org/issues/481 This user of OpenCV in C++ finds that setting and getting the property twice works around this problem. For me, in whether I use cv or cv2, the problem persists.

Community
  • 1
  • 1
Dan Allan
  • 34,073
  • 6
  • 70
  • 63
  • I often have the same problem. It differs from different video formats and codec. I think this is also a problem of the connection to you codec and ffmpeg you have installed. – Tobias Senst Mar 28 '13 at 22:48
  • Thanks for the comment. That corroborates some of what I have read: it can depend on the codec. Can you clarify what you mean by "connection to you codec and ffmpeg you have installed"? – Dan Allan Mar 29 '13 at 04:04
  • This is a long shot given that this question was asked nearly one year ago --- but did you come up with any workaround? I have been stuck on a similar problem for hours... – Jealie Jun 10 '14 at 20:52
  • As Tobias says, it varies from codec to codec. This seems to be a bug in OpenCV that I never found documented officially. The only safe "workaround" is to step through the video from the beginning linearly to the frame you want. If your goal is to read video data into Python, see this [documentation in progress for scikit-image](https://github.com/scikit-image/scikit-image/pull/1012) in which I review options. – Dan Allan Jun 10 '14 at 23:03

0 Answers0