简体   繁体   English

cv2 从具有特定时间的图像数组创建视频

[英]cv2 create video from array of images with specific time

I have here the code to generate video from array of images, it is working fine but it only displays each image for less than 1 sec how can i control the displaying time of the images for example pic 1 display in the video for 3 sec and pic 2 for 5 sec and so on >>我在这里有从图像数组生成视频的代码,它工作正常,但它只显示每个图像不到 1 秒我如何控制图像的显示时间,例如视频中的 pic 1 显示 3 秒和图 2 持续 5 秒,依此类推>>

import cv2
import numpy as np

img=[]
img.append(cv2.imread('/sent0word0.jpg'))
img.append(cv2.imread('/sent1word3.jpg'))
img.append(cv2.imread('/sent0word1.jpg'))

height, width, layers = img[2].shape
size = (width,height)

out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)

for i in range(len(img)):
    image=cv2.resize(img[i],size)
    out.write(image)
out.release()

  • To play image for 3 seconds add same image 3 times to frames list (1 list element = 1 second)要播放图像3秒,将相同的图像添加3次到帧列表(1 个列表元素 = 1 秒)
  • Set fps to 1 in VideoWriterVideoWriter fps设置为1

Code:代码:

import cv2

frames = [cv2.imread('frame1.jpg'),
          cv2.imread('frame2.jpg'),
          cv2.imread('frame2.jpg'),
          cv2.imread('frame2.jpg'),
          cv2.imread('frame2.jpg')]

height, width, _ = frames[0].shape
out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'DIVX'), 1, (width, height))
[out.write(f) for f in frames]
out.release()

Output: Output:

在此处输入图像描述

Each image is a frame in your video, since you set the frames per second to 15 in your cv2.VideoWriter object, it will display those images rather quick.每个图像都是视频中的一帧,因为您在 cv2.VideoWriter object 中将每秒帧数设置为 15,它会很快显示这些图像。 What you could do is lower the frames per seconds or duplicate your frames to match up the frames per second.您可以做的是降低每秒帧数或复制帧以匹配每秒帧数。

What you want is to change the fps dynamically.您想要的是动态更改 fps。 From the documentation of VideoWriter :VideoWriter文档中:

cv2.VideoWriter.open(filename, fourcc, fps, frameSize[, isColor]) → retval cv2.VideoWriter.open(filename,fourcc, fps, frameSize[, isColor]) → retval

You're interested in the third parameter that you've set to 15fps .您对设置为15fps的第三个参数感兴趣。 In particular this means that you're currently displaying 15 images per seconds.特别是这意味着您当前每秒显示 15 张图像。

This means that if you want to display 1 image in 1 second you're looking for an fps of 1. If you want 1 image for 3 seconds you want a fps equal to 1/3 and so on.这意味着,如果您想在 1 秒内显示 1 张图像,则您正在寻找 1 的 fps。如果您想要 3 秒内的 1 张图像,您希望 fps 等于1/3 ,依此类推。 Generally 1 image for n seconds requires an fps of 1/n .通常 1 张图像持续n秒需要 fps 为1/n

Keeping all of this in consideration there is no direct way to change the number of fps in a dynamic way but there is a workaround.考虑到所有这些因素,没有直接的方法可以以动态方式更改 fps 的数量,但有一种解决方法。 You could set the fps at the highest that you have and then just display the frames with a lower fps multiple times.您可以将 fps 设置为最高,然后多次显示具有较低 fps 的帧。

The way to build the code depends on your particular situation.构建代码的方式取决于您的特定情况。 Tell me if you need more info.如果您需要更多信息,请告诉我。

Edit: I've found something that may help you out on the OpenCV forum .编辑:我在OpenCV 论坛上找到了一些可以帮助你的东西。

import cv2
import numpy as np

img=[]
img.append(cv2.imread('C:/Users/STC/Desktop/TextToSp/sent0word0.jpg'))
img.append(cv2.imread('C:/Users/STC/Desktop/TextToSp/sent1word3.jpg'))
img.append(cv2.imread('C:/Users/STC/Desktop/TextToSp/sent0word1.jpg'))

height, width, layers = img[2].shape
size = (width,height)

out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 1.0, size)
each_image_duration = 5 # in secs

for i in range(len(img)):
    image=cv2.resize(img[i],size)
    for _ in range(each_image_duration):
        out.write(image)
out.release()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM