I have just started using OpenCV 2.4.8.2
in Python 2.7.6
on a MacBook Pro Retina running OS X 10.9.2
. My main goal is to make a video file using a few NumPy
-arrays. I would also like to do the inverse: decompose a video into separate frames (and consequently in NumPy
-arrays).
To make a video, I use the following piece of code:
import cv2
# Composes a movie from separate frames.
videoMaker = cv2.VideoWriter("videoTest.mov", cv2.cv.CV_FOURCC('m', 'p', '4', 'v'), 1, (256, 256))
if (videoMaker.isOpened()):
videoMaker.write(cv2.imread("videoTestFrame0.jpg"))
videoMaker.write(cv2.imread("videoTestFrame1.jpg"))
videoMaker.write(cv2.imread("videoTestFrame2.jpg"))
videoMaker.release()
This piece of code seems to work fine - videoTest.mov
is created, can be played in Quicktime, is 3 seconds long and consists of 3 different frames.
To load a video, I have put directly under the above code piece:
# Decomposes a movie into separate frames.
videoReader = cv2.VideoCapture("videoTest.mov")
count = 0
while True:
gotImage, image = videoReader.read()
if (gotImage):
cv2.imwrite("videoTestDecomposedFrame%d.jpg" %count, image)
count += 1
else:
break
The problem: When inspecting the 3 decomposed frames, the first frame of the movie (videoTestFrame0.jpg
) is not one of them, while the last frame of the movie (videoTestFrame2.jpg
) is stored twice.
How can I fix this problem and retrieve videoTestFrame0.jpg
from the video without having to modify it by putting in this first frame twice? Might something be wrong with cv2.VideoCapture.read()
? I have tried saving the movie as videoTest.avi
instead of videoTest.mov
, but the behavior at decomposition is the same.
Thanks in advance for your kind help! Martijn