使用CV2 VideoWriter编写numpy数组 [英] Writing numpy arrays using cv2 VideoWriter

查看:330
本文介绍了使用CV2 VideoWriter编写numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用opencv2.3.1 VideoWriter编写玩具示例视频时遇到问题,这是我的操作方法:

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)

我尝试了每种可能的组合,如果扩展名是mpg,则结果为0字节,如果是avi,则结果为5.5kb.我应该说,有人指出,我应该从源代码构建ffmpeg库,而不要适当地获取它.好吧,我是在基于此网站 http://vinayhacks.blogspot.com/2011/11/installing-opencv-231-with-ffmpeg-on-64.html .在编译opencv时也出现了错误(该错误与ffmpeg有关).现在我真的没有主意了,如何使用OPENCV生成视频?

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?

预先感谢

推荐答案

VideoWriter的最后一个参数isColor为默认值True. 因此,如果将其更改为False,则可以编写2D数组.

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)

这篇关于使用CV2 VideoWriter编写numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆