使用 cv2 VideoWriter 写入 numpy 数组

发布于 2025-01-05 17:24:50 字数 712 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

櫻之舞 2025-01-12 17:24:50

VideoWriter 的最后一个参数 isColor 具有默认值 True
因此,如果将其更改为 False,则可以编写二维数组。

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)

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)
老子叫无熙 2025-01-12 17:24:50

你好,我是 opencv 的新手,我也遇到了同样的问题。看来 writer.write(x) 需要 x 是一个包含 RGB 值的数组,而不是标量。我通过这样做解决了这个问题:

import cv2
import cv2.cv as cv
import numpy as np

writer = cv2.VideoWriter('test1.avi',cv.CV_FOURCC('P','I','M','1'),25,(640,480))
for i in range(1000):
    x = np.random.randint(255,size=(480,640)).astype('uint8')
    x = np.repeat(x,3,axis=1)
    x = x.reshape(480, 640, 3)
    writer.write(x)

我认为有更干净的方法可以做到这一点,但我还没有找到。

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:

import cv2
import cv2.cv as cv
import numpy as np

writer = cv2.VideoWriter('test1.avi',cv.CV_FOURCC('P','I','M','1'),25,(640,480))
for i in range(1000):
    x = np.random.randint(255,size=(480,640)).astype('uint8')
    x = np.repeat(x,3,axis=1)
    x = x.reshape(480, 640, 3)
    writer.write(x)

I assume there are cleaner ways to do it but I haven't found any.

南街九尾狐 2025-01-12 17:24:50

您使用哪个操作系统?您确定您的系统安装了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文