我正在尝试使用OpenCV 2.4.0
中的python 2.7.2
制作视频。但avi
文件大小为0。
我的代码:
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()
始终返回false
。
另一次尝试:
#!/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
没有错误,但输出为空。
我错过了什么?
答案 0 :(得分:39)
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()
您想要做的简单代码。详情here
答案 1 :(得分:4)
找到这段代码,对我有用(产生有色噪音):
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()
来源:https://github.com/ContinuumIO/anaconda-issues/issues/223#issuecomment-285523938
答案 2 :(得分:0)
height, width, layers = img.shape
out = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"XVID"), 30,(width,height))
out.write(img)
out.release()