【问题标题】:OpenCV - Making a video from existing imagesOpenCV - 从现有图像制作视频
【发布时间】:2018-10-28 21:09:37
【问题描述】:

我有一个名为 images 的 numpy 数组图像数组。让我们为了讨论而说 -

images = [cv2.imread("data/frame" + str(i) + ".jpg") for i in range(15)]

其中 data 是包含视频帧的目录。 然后我尝试使用以下代码将它们保存为视频:

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
shape = images[0].shape[:2]
vid = cv2.VideoWriter("my_vid.avi", fourcc, 1, shape)
for fg_frame in images:
    vid.write(np.uint8(fg_frame))
vid.release()

但保存的视频只有 5-6 KB 大小,并且没有播放任何内容。我做错了什么?

【问题讨论】:

  • 也许你的系统中不需要编解码器(或者 OpenCV 没有找到它)

标签: python numpy opencv video


【解决方案1】:

原因是cv2.VideoWriter 的构造函数在第四个参数中获取视频大小,该参数应该是(width, height) 形式的元组。

numpy 数组的shape 成员将维度存储为(height, width)

由于VideoWriter 和实际图像之间的尺寸不匹配,没有帧被写入磁盘。

当作为参数传递给VideoWriter 时,您必须交换shape 的元素。正确的代码可能如下所示:

shape = images[0].shape[:2]
video_size = (shape[1], shape[0])
vid = cv2.VideoWriter("my_vid.avi", fourcc, 1, video_size)

在 Ubuntu 14.04 上使用 Python 3 和 OpenCV 3.4 进行验证和测试。

【讨论】:

    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2021-01-03
    • 2016-10-18
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    相关资源
    最近更新 更多