在经过图像处理每一帧后,想将连续帧组合成高帧率视频,可以采用下面的方法。
一、准备好连续帧图片集
准备好连续帧图片集,如下,我自己准备了八千多张连续帧图像,图片格式都是.jpg格式,当然其它图片格式也是可以的,后面只需在代码中修改一下即可使用,如果学者需要批量重命名图片集,详细方法见我的另外一篇博客,链接为:添加链接描述
二、使用代码须知
使用代码之前注意几个需要修改的地方:
1、图片格式
2、合成视频的帧率
3、合成视频的保存路径
4、加载图像集的路径
5、代码
import cv2
import glob
def resize(img_array, align_mode):
_height = len(img_array[0])
_width = len(img_array[0][0])
for i in range(1, len(img_array)):
img = img_array[i]
height = len(img)
width = len(img[0])
if align_mode == 'smallest':
if height < _height:
_height = height
if width < _width:
_width = width
else:
if height > _height:
_height = height
if width > _width:
_width = width
for i in range(0, len(img_array)):
img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
img_array[i] = img1
return img_array, (_width, _height)
def images_to_video(path):
img_array = []
for filename in glob.glob(path + '/*.jpg'):
img = cv2.imread(filename)
if img is None:
print(filename + " is error!")
continue
img_array.append(img)
# 图片的大小需要一致
img_array, size = resize(img_array, 'largest')
fps = 25
out = cv2.VideoWriter('demo.avi', cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
def main():
# path = "../pictures/test/"
path = "G:/Code/Matalab/ColorTransfer/Save_Images_2"
images_to_video(path)
if __name__ == "__main__":
main()
三、合成效果
四、总结
以上就是将多帧连续图像合成高帧率视频的方法,希望此方法能帮助到你,谢谢!