gpt4 book ai didi

python - 用opencv制作视频,只有一帧

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:13 26 4
gpt4 key购买 nike

我一直在尝试使用 opencv 将视频制作模块添加到我的程序中,但我可能无法让视频显示超过 1 帧。我在搜索答案时得到了这个示例代码( Creating a video using OpenCV 2.4.0 in python )

import cv2
from cv import *

img1 = cv2.imread('i001.png')
img2 = cv2.imread('i002.png')
img3 = cv2.imread('i003.png')

height , width , layers = img1.shape

video=cv2.VideoWriter('test.avi', CV_FOURCC('D', 'I', 'V', 'X'),1,(width,height))

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

cv2.destroyAllWindows()
video.release()

这会创建一个播放 3 秒的视频,但在所有 3 秒内只显示第一张图片。是否存在编解码器错误或我遗漏了什么?

最佳答案

你可以使用:

 cv.WriteFrame(writer, image)

像这样:

 import cv

img1 = cv.LoadImage('i001.png')
img2 = cv.LoadImage('i002.png')
img3 = cv.LoadImage('i003.png')

writer=cv.CreateVideoWriter(filename, fourcc, fps, frame_size, is_color)

cv.WriteFrame(writer, img1)
cv.WriteFrame(writer, img2)
cv.WriteFrame(writer, img3)

也看看:

http://opencv.willowgarage.com/documentation/python/highgui_reading_and_writing_images_and_video.html?highlight=createvideowriter#CreateVideoWriter

关于python - 用opencv制作视频,只有一帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16897480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com