2013-06-03 52 views
3

我一直在尝试使用opencv将视频制作模块添加到我的程序中,但尝试一下,因为我可能无法让视频显示超过1帧。 我得到这个例子代码,同时寻找答案 (Creating a video using OpenCV 2.4.0 in python使用opencv制作视频,只能获得1帧

import cv2 
from cv import * 

img1 = cv2.imread('i001.png') 
img2 = cv2.imread('i002.png') 
img3 = cv2.imread('i003.png') 

height , width , layers = img1.shape 

video=cv2.VideoWriter('test.avi', CV_FOURCC('D', 'I', 'V', 'X'),1,(width,height)) 

video.write(img1) 
video.write(img2) 
video.write(img3) 

cv2.destroyAllWindows() 
video.release() 

这将创建一个3秒钟运行的视频,但只显示了所有3秒,第一张图像。有没有编解码器错误,或者我错过了什么?

+2

看看这个http://stackoverflow.com/questions/5426637/writing-video-with-opencv-python-mac – fGo

+1

尝试使用不同的编解码器。它似乎是一个解码/编码问题。尝试'mjpg'或'i420'作为fourcc,看它是否改变了任何东西。 –

+0

谢谢,i420编解码器似乎工作,我以前尝试过一堆,他们没有。 – Johan

回答

0

u可以使用:

cv.WriteFrame(writer, image) 

这样的:

import cv 

img1 = cv.LoadImage('i001.png') 
img2 = cv.LoadImage('i002.png') 
img3 = cv.LoadImage('i003.png') 

writer=cv.CreateVideoWriter(filename, fourcc, fps, frame_size, is_color) 

cv.WriteFrame(writer, img1) 
cv.WriteFrame(writer, img2) 
cv.WriteFrame(writer, img3) 

看也:

http://opencv.willowgarage.com/documentation/python/highgui_reading_and_writing_images_and_video.html?highlight=createvideowriter#CreateVideoWriter