开发者

Creating greyscale video using Python, OpenCV, and numpy arrays

开发者 https://www.devze.com 2023-04-12 05:08 出处:网络
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 :

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? Than开发者_如何转开发ks in advance.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消