【问题标题】:Python/Open CV: Creating a video from cos image sequence, the video is emptyPython/Open CV:从cos图像序列创建视频,视频为空
【发布时间】:2017-05-02 11:28:59
【问题描述】:

我正在尝试从我创建的 cos 图像创建一个视频,但我制作的 avi 视频是空的。

我必须使用 cv2.VideoWriter()。创建的视频正确显示帧为 0 字节。你知道如何解决这个问题吗?

如果我从 avi 视频中获得一帧,那么我成功地创建了一个新的可播放视频。

提前谢谢你!

我的代码是

import cv2
import math
import numpy as np

N=128
Icos_1 = np.zeros((N,N))
Icos_2 = np.zeros((N,N))
for i in range(0,N):
    for j in range(0,N):
        myPi = 2*math.pi/N
        th1 = 1*i + 3*j
        th2 = 2*i + 4*j
        Icos_1[i,j] = 255*(math.cos(myPi*th1))
        Icos_2[i,j] = 255*(math.cos(myPi*th2))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('sample_video.avi',fourcc, 25.0, (128,128))

or x in range(1, 100):
    if (x % 2) != 0:
        frame = Icos_1
    else:
        frame = Icos_2

    out.write(np.uint8(frame))
    cv2.imshow('frame',np.uint8(frame))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release video objects if job is finished
out.release()

【问题讨论】:

  • 如果你使用np.uint8(frame)在屏幕上显示那你为什么不使用write(np.uint8(frame))
  • 您也可能没有安装正确的编解码器,或者您想要支持编写视频的编解码器。您使用什么操作系统?您是否也尝试过使用 MPEG-4 而不是 XVID?
  • @furas 没用!结果还是一样。还有其他想法吗?

标签: python python-2.7 opencv video


【解决方案1】:

我找到了解决办法!

我必须在out = cv2.VideoWriter('sample_video.avi',0, 25.0, (128,128)) 中使用 0 作为编解码器

完整代码为:

import cv2
import math
import numpy as np

N=128
Icos_1 = np.zeros((N,N))
Icos_2 = np.zeros((N,N))
for i in range(0,N):
    for j in range(0,N):
        myPi = 2*math.pi/N
        th1 = 1*i + 3*j
        th2 = 2*i + 4*j
        Icos_1[i,j] = 255*(math.cos(myPi*th1))
        Icos_2[i,j] = 255*(math.cos(myPi*th2))

out = cv2.VideoWriter('sample_video.avi',0, 25.0, (128,128))

or x in range(1, 100):
    if (x % 2) != 0:
        frame = Icos_1
    else:
        frame = Icos_2

    out.write(np.uint8(frame))
    cv2.imshow('frame',np.uint8(frame))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release video objects if job is finished
out.release()

【讨论】:

  • 0 弹出一个窗口,允许您选择系统上安装的编解码器。您问题的实际解决方案是您选择了正确的编解码器。请更新您的帖子,让我们知道您实际选择的编解码器。
  • @rayryeng 在运行cv2.VideoWriter() 时,编解码器为 0 实际上我的程序运行正常。如果我将0 也设置为帧速率,程序会弹出一个窗口。但在这里我的帧速率 = 25.0fps,所以我的程序正在运行。
猜你喜欢
  • 1970-01-01
  • 2013-12-04
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
相关资源
最近更新 更多