【发布时间】:2013-08-05 00:40:06
【问题描述】:
问题:
我在正确渲染/播放由 OpenCV 的 videowriter 创建的视频时遇到问题。
详情:
Python 不会吐出任何错误并成功运行/创建视频。当我尝试使用 VLC 播放视频时,看起来 VLC 正在尝试播放一个空视频(看起来它的时间长度为 00:00)。此外,视频大小仅为 5kbs。我已经将此函数与其他 NumPy 图像数组一起使用,并且它们有效。
我的 NumPy 数组准备代码:
for i in range(len(frames)-1):
fig = PIV.runPIV(frames[i].astype(np.int32),frames[i+1].astype(np.int32),dt = self.context.attrs.dt)
agg = fig.canvas.switch_backends(FigureCanvasAgg)
agg.draw() #This produces a successful image. Simple Quiver plot.
image = np.fromstring(agg.tostring_rgb(),dtype = np.uint8)
image.shape = agg.get_width_height() + (3,)
images.append(image)
self._render(images)
图像数组:
[[[191 191 191]
[191 191 191]
[191 191 191]
...,
[191 191 191]
[191 191 191]
[191 191 191]]]
注意:
该数组的形状为 (640,480,3),这与以前工作的其他数组类似相同。这与过去其他成功之间的唯一区别是,在我加载图像并在渲染之前对它们进行处理之前,但这个堆栈来自 matplotlib.pyplot,后端切换到了 FigureCanvasAgg;但是,当我保存图像在其 NumPy 转换之前时,图像看起来非常好。
我的 OpenCV VideoWriter 代码:
def writeMovie(self, fileName, fps=20):
isColor = len(self[0].shape) == 3
writer = cv2.VideoWriter(fileName,
fps=fps,
fourcc=cv2.cv.CV_FOURCC(*"PIM1"),
frameSize=self[0].shape[0:2],
isColor=isColor)
for image in self:
if image.dtype == np.bool:
image = image.astype(np.uint8)*255
writer.write(image)
我很乐意尽我所能提供更多信息来澄清和帮助回答这个问题。任何意见/建议将不胜感激。
【问题讨论】:
标签: python opencv video python-2.7 numpy