Opencv Python 简明教程
OpenCV Python - Video from Images
在上一章中,我们使用 VideoWriter() 函数将相机的实时流保存为视频文件。为了将多张图像拼接成一个视频,我们应使用该函数。
In the previous chapter, we have used the VideoWriter() function to save the live stream from a camera as a video file. To stitch multiple images into a video, we shall use the same function.
首先,确保所有必需的图像都放在一个文件夹中。内置的 glob 模块中的 Python 的 glob() 函数构建一个图像数组,以便我们可以对其进行迭代。
First, ensure that all the required images are in a folder. Python’s glob() function in the built-in glob module builds an array of images so that we can iterate through it.
从文件夹中的图像中读取图像对象,并添加到图像数组。
Read the image object from the images in the folder and append to an image array.
以下程序说明如何将多个图像拼接成一个视频。
Following program explains how to stitch multiple images in a video −
import cv2
import numpy as np
import glob
img_array = []
for filename in glob.glob('*.png'):
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
通过使用 VideoWriter() 函数创建一个视频流,将图像数组的内容写入到其中。下面给出了该程序。
The create a video stream by using VideoWriter() function to write the contents of the image array to it. Given below is the program for the same.
out = cv2.VideoWriter('video.avi',cv2.VideoWriter_fourcc(*'DIVX'), 15, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
您应该在当前文件夹中找到名为 ‘video.avi’ 的文件。
You should find the file named ‘video.avi’ in the current folder.