用openCV:empty-vid在python中保存视频捕获

2024-10-05 20:11:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python(2.7)的新手,我试着研究视频处理(使用openCv“cv2”模块)。从教程开始,我尝试使用this tutorial:段落“保存视频”的脚本。 除了我保存的视频是空的之外,一切都很好。我可以在我的目录中找到output.avi,但它的内存大小是0kb当然,当我运行它时,不会显示视频。

在做了一些修改之后,这里是我的代码:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

有人知道它为什么不能正常工作吗?

非常感谢。 埃德温


Tags: theimportoutput视频ifoutcv2frame
2条回答

可能是输出分辨率与输入不同。检查盖子的宽度和高度。

size = (int(cap.get(3)), int(cap.get(4)))

更改相机或输出分辨率。

我从没和openCV合作过,但我敢打赌问题出在

cap = cv2.VideoCapture(0)

这是VideoCapture方法的C版本http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture

也许你也可以试试。有点像

cap = cv2.VideoCapture(0)
if (not cap.isOpened()):
    print "Error"

编辑:刚刚下载了Python和OpenCV,发现问题出在编解码器上。尝试改变

out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

为了

out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))

并手动选择编解码器。

相关问题 更多 >