Python OpenCV【视频合并:多个图像拼接在一张图像】

前言:在拼接图像合并视频这一块,Python版绝对要优于C++版,因为在数据处理方面,Python自带numpy等库。本文仅记录两种语言拼接图像方法且提供源码。 


C++版(示例是在vs2017编辑器中实现):

C++ OpenCV【视频合并:多个图像拼接在一张图像】_IT.Husky的博客-CSDN博客


Python版(示例是在Spyder编辑器中实现):

# -*- coding: utf-8 -*-
"""
Spyder Editor

@author: Husky
"""

import cv2
import numpy as np

videoLeftUp = cv2.VideoCapture('video1.mp4')
videoLeftDown = cv2.VideoCapture('video2.mp4')
videoRightUp = cv2.VideoCapture('video3.mp4')
videoRightDown = cv2.VideoCapture('video4.mp4')

fps = videoLeftDown.get(cv2.CAP_PROP_FPS)

width = (int(videoLeftDown.get(cv2.CAP_PROP_FRAME_WIDTH)))
height = (int(videoLeftDown.get(cv2.CAP_PROP_FRAME_HEIGHT)))

videoWriter = cv2.VideoWriter('video4in1.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps, (width, height))
count = 0
successLeftUp, frameLeftUp = videoLeftUp.read()
successLeftDown , frameLeftDown = videoLeftDown.read()
successRightUp, frameRightUp = videoRightUp.read()
successRightDown, frameRightDown = videoRightDown.read()

while successLeftUp and successLeftDown and successRightUp and successRightDown:
    frameLeftUp = cv2.resize(frameLeftUp, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_CUBIC)
    frameLeftDown = cv2.resize(frameLeftDown, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_CUBIC)
    frameRightUp = cv2.resize(frameRightUp, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_CUBIC)
    frameRightDown = cv2.resize(frameRightDown, (int(width / 2), int(height / 2)), interpolation=cv2.INTER_CUBIC)

    frameUp = np.hstack((frameLeftUp, frameRightUp))
    frameDown = np.hstack((frameLeftDown, frameRightDown))
    frame = np.vstack((frameUp, frameDown))

    videoWriter.write(frame)
    count+1
    successLeftUp, frameLeftUp = videoLeftUp.read()
    successLeftDown, frameLeftDown = videoLeftDown.read()
    successRightUp, frameRightUp = videoRightUp.read()
    successRightDown, frameRightDown = videoRightDown.read()
#    if count > 21600:
#        break

videoWriter.release()
videoLeftUp.release()
videoLeftDown.release()
videoRightUp.release()
videoRightDown.release()

你可能感兴趣的:(OpenCV学习,python,opencv,开发语言)