使用 cv2 VideoWriter 写入 numpy 数组
我在使用 opencv2.3.1 VideoWriter 编写玩具示例视频时遇到问题,以下是我的做法:
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 库,而不是 apt-get 它。好吧,我在这个网站的帮助下在一台新机器上做到了这一点 http://vinayhacks.blogspot.com/2011/11/installing-opencv-231-with-ffmpeg-on-64.html。编译 opencv 时也出现错误(该错误与 ffmpeg 有关)。现在我真的没有主意了,如何使用 OPENCV 生成视频?
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
VideoWriter 的最后一个参数
isColor
具有默认值True
。因此,如果将其更改为 False,则可以编写二维数组。
VideoWriter has last argument
isColor
with default valueTrue
.So if you change it to
False
then you can write your 2D arrays.你好,我是 opencv 的新手,我也遇到了同样的问题。看来 writer.write(x) 需要 x 是一个包含 RGB 值的数组,而不是标量。我通过这样做解决了这个问题:
我认为有更干净的方法可以做到这一点,但我还没有找到。
Hello I am new to opencv and I had this same problem. It appears that the writer.write(x) Needs x to be an array whit RGB values and not scalars. I solved the issue by doing this:
I assume there are cleaner ways to do it but I haven't found any.
您使用哪个操作系统?您确定您的系统安装了 PIM1 编解码器吗?
我使用 Windows,可以使用
cv.FOURCC(*"DIB ")
来显示未压缩的视频,或者使用 -1 显示编解码器对话框。安装ffdshow后,我可以使用
cv.FOURCC(*"ffds")
通过MPEG-4对视频进行编码。Which OS are you using? Are you sure your system have PIM1 codec installed?
I use windows, and I can use
cv.FOURCC(*"DIB ")
for uncompressed video, or use -1 to show a codec dialog.After install ffdshow, I can use
cv.FOURCC(*"ffds")
to encode the video by MPEG-4.