OpenCv_Python - 将帧序列转换为视频

时间:2018-05-27 08:55:46

标签: resize

我是使用Python的OpenCV新手。我目前正在使用python语言处理与opencv相关的项目。我有一个名为“VideoDataSet / dynamicBackground / canoe / input”的视频数据集,用于存储图像帧序列,我想将帧序列从文件路径转换为视频。但是,执行程序时出错。我尝试了各种编解码器,但它仍然给我同样的错误,你们中的任何人都可以对可能出错的问题有所了解吗?谢谢。 这是我的示例代码:

import cv2
import numpy as np
import os
import glob as gb

filename = "VideoDataSet/dynamicBackground/canoe/input"

img_path = gb.glob(filename)

videoWriter = cv2.VideoWriter('test.avi', cv2.VideoWriter_fourcc(*'MJPG'), 
25, (640,480))

for path in img_path:
    img  = cv2.imread(path) 
    img = cv2.resize(img,(640,480))
    videoWriter.write(img)
print ("you are success create.")

这是错误:

Error prompt out:cv2.error: OpenCV(3.4.1) D:\Build\OpenCV\opencv-3.4.1\modules\imgproc\src\resize.cpp:4044: error: (-215) ssize.width > 0 && ssize.height > 0 in function cv::resize

(注意:问题出现在img = cv2.resize(img,(640,480)))

2 个答案:

答案 0 :(得分:0)

它正在返回此错误,因为您正在尝试重新调整目录条目的大小!你需要把:

filename = "VideoDataSet/dynamicBackground/canoe/input/*"

这样当它全局化时它将匹配文件夹中的所有文件。该错误实际上表明源图像具有零宽度或零高度。把:

print( img_path )

在你的glob尝试显示它只返回目录条目之后。

您随后发现虽然它现在正在生成文件,但它已损坏。这是因为您错误地指定了编解码器。将fourcc参数替换为:

cv2.VideoWriter_fourcc('M','J','P','G')

答案 1 :(得分:0)

您可以尝试以下操作:

img_path = gb.glob(filename)

videoWriter = cv2.VideoWriter('frame2video.avi', cv2.VideoWriter_fourcc(*'MJPG'), 25, (640,480))

for path in img_path:
    img  = cv2.imread(path)
    img = cv2.resize(img,(640,480))
    videoWriter.write(img)