在 python 中使用 OpenCV 2.4.0 创建视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14440400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:30:21  来源:igfitidea点击:

Creating a video using OpenCV 2.4.0 in python

pythonvideoopencv

提问by ATOzTOA

I am trying to create a video using OpenCV 2.4.0in python 2.7.2. But the avifile size is 0.

我正在尝试使用OpenCV 2.4.0in创建视频python 2.7.2。但是avi文件大小为0。

My code:

我的代码:

from cv2 import *

im1 = cv.LoadImage("1.jpg")

fps = 20
frame_size = cv.GetSize(im1)

#writer = cv.CreateVideoWriter("out.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, frame_size, True)

v = VideoWriter()

v.open("out.avi", cv.CV_FOURCC('F', 'M', 'P', '4'), fps, (800,600), True)
print v.isOpened()

isOpened()is always returning false.

isOpened()总是回来false

Another try:

另一个尝试:

#!/usr/bin/env python
import sys

from cv2 import *

im1 = cv.LoadImage("1.jpg")

if not im1:
    print "Error loading image"

im2 = cv.LoadImage("2.jpg")

if not im1:
    print "Error loading image"

fps = 20
frame_size = cv.GetSize(im1)

writer = cv.CreateVideoWriter("out.avi", cv.CV_FOURCC('M', 'J', 'P', 'G'), fps, frame_size, True)

if not writer:
    print "Error in creating video writer"
    sys.exit(1)
else:
    cv.WriteFrame(writer, im1)
    cv.WriteFrame(writer, im2)

del writer

No errors, but the output is empty.

没有错误,但输出为空。

What am I missing?

我错过了什么?

采纳答案by Ayub Khan

import cv2

img1 = cv2.imread('1.jpg')
img2 = cv2.imread('2.jpg')
img3 = cv2.imread('3.jpg')

height , width , layers =  img1.shape

video = cv2.VideoWriter('video.avi',-1,1,(width,height))

video.write(img1)
video.write(img2)
video.write(img3)

cv2.destroyAllWindows()
video.release()

A simple code for what you want to do. for details here

您想要做什么的简单代码。详情请看这里

回答by lama12345

Found this code, which works for me (generating colored noise):

找到了对我有用的代码(产生有色噪声):

writer = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 30,(640,480))
for frame in range(1000):
    writer.write(np.random.randint(0, 255, (480,640,3)).astype('uint8'))
writer.release()

Source: https://github.com/ContinuumIO/anaconda-issues/issues/223#issuecomment-285523938

来源:https: //github.com/ContinuumIO/anaconda-issues/issues/223#issuecomment-285523938

回答by YakovK

height, width, layers = img.shape
out = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"XVID"), 30,(width,height))
out.write(img)
out.release()