如何将多个视频合并为一个视频并使用Python设置它们的位置 [英] How can I combine a multiple videos into one single video and set the position of them using Python

查看:993
本文介绍了如何将多个视频合并为一个视频并使用Python设置它们的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我有4个视频,我想将它们组合成一个视频,然后使用Python一次播放.每个视频都像全息视频一样设置在位置(例如,顶部,底部,左侧,右侧)喜欢这样.有什么方法可以帮助我实现这一目标?我找到了一些与我的问题类似的相关资源,但是我无法设法将其应用于我的问题.

My problem is that, I have 4 videos and I would like to combine and fit them into one single video and play them at once using Python. Each of the video are set in the position (e.g. top, bottom, left, right) like a hologram video like this. Are there any ways which can help me implement this? I've found some related source which is similar to my problem but I cannot manage to apply it for my problem.

提前谢谢

推荐答案

您可以尝试通过将所有图像复制到一个黑色框中来将它们合并在一起.这是一个在所有4个地方都具有相同图像的示例:

You can try to merge all images together by copying them into one black frame. Here is an example with the same image in all 4 places:

import cv2
import numpy as np

#loads images and gets data
img = cv2.imread("img.png")
h,w,_ = img.shape    

# creates the resulting image with double the size and 3 channels 
output = np.zeros((h * 2, w * 2, 3), dtype="uint8")

# copies the image to the top left
output[0:h, 0:w] = img 
# copies the image to the top right
output[0:h, w:w * 2] = img 
# copies the image to the bottom left
output[h:h * 2, w:w * 2] = img 
# copies the image to the bottom right
output[h:h * 2, 0:w] = img 

您始终可以将img更改为其他内容.您也可以像这样将它们串联起来:

You can always change the img to something different. Also you can concatenate them like this:

top = np.hstack((img, img))
bottom = np.hstack((img, img))
result = np.vstack((top, bottom))

结果将是相同的.

这里是带有以下代码的结果img的示例:

Here as sample of the resulting img with this code:

不过,您的图像有些不同,您将需要轮换并且并非完全串联,而是要复制一个.一个示例如下:

However your image is a little bit different, you will need a rotation and is not exactly concatenation, but the copying one. An example of this follows:

# creates the resulting image with double the size and 3 channels 
output = np.zeros((w+h+h , w + h + h, 3), dtype="uint8")

# top img
output[0:h, h:h+w] = img 
# left img (rotated 90°)
output[h:h+w, 0:h] = np.rot90(img,1) 
# right img (rotated 270°)
output[h:h + w, h + w:h +w +h] = np.rot90(img,3)  
# bottom img (rotated 180°)
output[h+w:h+w+h, h:h+w] = np.rot90(img,2) 

结果是这样的:

如果将图像与黑色背景一起使用,则将或多或少地获得您所拥有的图像.您可能需要使用复制参数来玩,但基本上您会执行以下操作:

If you use your image with the black background you will get more or less what you have there. You would need to play maybe with the copying parameters, but basically you do something like:

imgToCopyTo[y1:y2, x1:x2] = imgToCopyFrom

其中y1和x1是要开始复制的位置的左上角坐标,而y2和x2是要复制到的位置的右​​下角坐标.另外y2-y1的高度应为imgToCopyFrom x2-x1的宽度(可以大于宽度或高度,但不能小于此宽度).

Where y1 and x1 is your top left coordinates where you want to start the copy and y2 and x2 are your bottom right coordinates of where you want to copy to. Also y2-y1 should have the height of the imgToCopyFrom x2-x1 the width (it can be bigger than the width or height but not smaller).

这篇关于如何将多个视频合并为一个视频并使用Python设置它们的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆