4

I've been trying to add a video making module to my program using opencv, but try as I might I can't get the video to show more than 1 frame. I got this example code while searching for answers (Creating a video using OpenCV 2.4.0 in python)

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()

This creates a video that runs for 3 seconds, but shows only the first image for all 3 seconds. Is there a codec error or am I missing something?

Community
  • 1
  • 1
Johan
  • 105
  • 1
  • 6
  • 2
    take a look at this http://stackoverflow.com/questions/5426637/writing-video-with-opencv-python-mac – fGo Jun 03 '13 at 13:18
  • 1
    try a different codec. it seems as a decoding/encoding matter. try `mjpg` or `i420` as the fourcc to see if it changes anything. – Kamyar Infinity Jun 03 '13 at 20:29
  • Thanks, the i420 codec seems to work, I've tried a bunch of them earlier that didn't. – Johan Jun 07 '13 at 11:04

1 Answers1

0

u can use :

 cv.WriteFrame(writer, image)

like this :

 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)

look also :

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

mevaka
  • 1,980
  • 2
  • 17
  • 24