Writing numpy arrays using cv2 VideoWriter


Question

I have a problem with writing a toy example video using opencv2.3.1 VideoWriter, here is how I do it:

writer = cv2.VideoWriter('test1.avi',cv.CV_FOURCC('P','I','M','1'),25,(640,480))
for i in range(1000):
    x = np.random.randint(10,size=(480,640)).astype('uint8')
    writer.write(x)
#del writer (with or without tested)

I tried every possible combination resulting with a 0 bytes file if the extension was mpg, and 5.5kb if it was avi. I should say that some pointed out that I should build the ffmpeg library from source and not apt-get it. Well I did that on a fresh machine based on the help of this site http://vinayhacks.blogspot.com/2011/11/installing-opencv-231-with-ffmpeg-on-64.html. which also presented an error while compiling opencv(the error was related to ffmpeg). Now I am really out of ideas, How to generate a video using OPENCV?

Thanks in advance

1
4
2/14/2012 4:46:13 PM

VideoWriter has last argument isColor with default value True. So if you change it to False then you can write your 2D arrays.

import cv2
import numpy as np

writer = cv2.VideoWriter('test1.avi', cv2.VideoWriter_fourcc(*'PIM1'), 25, (640, 480), False)
for i in range(100):
    x = np.random.randint(255, size=(480, 640)).astype('uint8')
    writer.write(x)
4
7/8/2015 12:34:44 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon