【问题标题】:How do I save a list of arrays (frames) to a video using OpenCV?如何使用 OpenCV 将数组(帧)列表保存到视频中?
【发布时间】:2020-09-10 20:26:54
【问题描述】:

我尝试使用以下代码将数组列表保存到视频中,但它不起作用。

out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (1280, 720))
for frame in frames:
    out.write(frame) # frame is a numpy.ndarray with shape (1280, 720, 3)
out.release()

它失败,没有错误消息,但输出文件 (output.mp4) 大约 200 字节长,并且不能在 QuickTime 或 VLC 中打开。我的假设是 out.write 不知何故默默地失败了。是否可以以这种方式将数组写入视频?如果没有,怎么办?

非常感谢任何和所有的帮助。

【问题讨论】:

  • 你能检查一下你的框架尺寸吗?最有可能造成问题
  • 我相信帧大小是正确的,但我会仔细检查。谢谢。
  • 试试这个out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (720, 1080))
  • 看起来 x 和 y 轴在 numpy 数组中被切换了。感谢您的帮助。
  • 不客气。我在下面做了一个正式的回答

标签: python python-3.x macos numpy cv2


【解决方案1】:

Numpy 数组是 (row,column) 但 OpenCV 通过 (width,height) 定义图像。因此,在您的 numpy 数组 height=row=1080width=column=720 中。所以, 将帧大小 (1080,720) 更改为 (720,1080)。

out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (720, 1080))
for frame in frames:
    out.write(frame) # frame is a numpy.ndarray with shape (1280, 720, 3)
out.release()

【讨论】:

    猜你喜欢
    • 2021-10-01
    • 2021-11-14
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 2020-04-01
    • 2011-09-09
    相关资源
    最近更新 更多