【问题标题】:cannot save video in opencv 3无法在opencv 3中保存视频
【发布时间】:2018-03-04 10:19:53
【问题描述】:

我正在尝试对视频应用 Canny 过滤器,以在 output.mp4 文件中生成带有 canny 过滤器的视频。 我发现输出文件是 1 千字节大小,虽然原始视频大小是 30 兆字节

这是我使用的代码:

import numpy as np
import cv2
Video = cv2.VideoCapture('video.mp4')
width = int(Video.get(cv2.CAP_PROP_FRAME_WIDTH) + 0.5)
height = int(Video.get(cv2.CAP_PROP_FRAME_HEIGHT) + 0.5)
fourcc = cv2.VideoWriter_fourcc(*'mp4v') 
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (width, height))
while(Video.isOpened()):
ret, frame = Video.read()
if ret==True:
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 100, 100)
    cv2.imshow('frame',edges)
    out.write(edges)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Video.release()
out.release()
cv2.destroyAllWindows()

【问题讨论】:

  • 你安装了编解码器吗?
  • 是的。我在 Windows 上使用 spyder 在 Anconda 3 上运行此代码
  • 尝试安装 XVID 编解码器并将文件保存为 .avi 格式

标签: python-3.x opencv


【解决方案1】:

因为您正在编写灰色图像,所以您必须将 False 传递给 VideoWriter 的 isColor 值。
只需将您的 out = 行更改为:

out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (width, height), False)

另外,当您将int 放在它们前面时,为什么还要在宽度和高度上添加 0.5!?请注意,VideoWriter 必须具有与您要通过它的帧相同的 w 和 h,因此如果您更改 w 和 h 而不调整您正在编写的帧的大小,VideoWriter 将不起作用(尽管在您的示例中那个 0.5 没有做任何事情,因为int 只会让它落地)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2020-09-24
    相关资源
    最近更新 更多