Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using 32-bit python with OpenCV 2.3.1. I am trying to write 2-dimensional numpy arrays to a opencv video writer. My code is similar to :

import cv2 as cv
import numpy as np

fourcc = cv.cv.CV_FOURCC('D', 'I', 'V', 'X')
writer = cv.cv.CreateVideoWriter("test.mpg", courcc, 10, (256,256))
if not writer:
     print "Error"
     sys.exit(1)

for ii in range(numberOfFrames):    
    numpy_image = GetFrame(ii)    #Gets a random image

    cv_image = cv.cv.CreateImage((256,256), cv.IPL_DEPTH_8U, 1)
    cv.cv.SetData(cv_image, numpy_image.tostring(), numpy_array.dtype.itemsize*1*256)

    cv.cv.WriteFrame(writer, cv_image)

del writer

I can see that I have the appropriate data in my numpy array. And if I try reading the data back from the iplImage I see it is still there. However, writing the frame does not appear to do anything. No file is being made or throwing any error. What could I be doing wrong? Thanks in advance.

share|improve this question
    
This snippet does not look complete. Where does GetFrame come from (would it be cv.cv.GetFrame)? What are your imports for this code? –  heltonbiker Oct 10 '11 at 16:58
    
The GetFrame is a separate function that I get the images from. It is unrelated to the cv.cv.GetFrame(). –  Doomchinchilla Oct 10 '11 at 17:02
    
facing a similar problem with cv2 and numpy matrices, it gives me no error, but it is writing nothing into the file!!!! –  JustInTime Feb 16 '12 at 11:42

1 Answer 1

I've never used OpenCV, but FWIW I write numpy arrays to video files by piping them to mencoder (based on VokkiCoder's VideoSink class here). It's very fast and seems to work pretty reliably, and it will also write RGB video.

Here's a paste. It also includes a VideoSource class for reading movie files to numpy arrays using a very similar approach.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.