Python图像处理(三)--从图像拼接到视频拼接与整合处理
作者:互联网
图像拼接
python中关于图像拼接的方法比较多,个人推荐使用Numpy中的原生方法(concatenate、hstack、vstack),如下所示:
# -*- coding:utf-8 -*-
'''
图像(水平、垂直)拼接
'''
import cv2
import numpy as np
# 读取图像并重置图像大小,使两图像宽高保持一致
img1 = cv2.resize(cv2.imread("./images/5.jpg"), (640, 480))
img2 = cv2.resize(cv2.imread("./images/2.jpg"), (640, 480))
# 方式一:
image = np.hstack([img1, img2]) # 水平拼接
image2 = np.vstack([img1, img2]) # 垂直拼接
# 方式二:
image3 = np.concatenate([img1, img2], axis=0) # axis=0时为垂直拼接;axis=1时为水平拼接
cv2.imshow("", image3)
cv2.waitKey(5000)
cv2.destroyAllWindows()
原始图像:
拼接后效果如下:
视频拼接
视频拼接其实就是对视频帧(图像)做处理:将视频处理成帧,然后对其做相应的处理。示例如下:
# -*- coding:utf-8 -*-
'''
视频拼接
'''
import cv2
import numpy as np
cam1 = cv2.VideoCapture("C:/Users/Administrator/Desktop/csdn/001.mp4")
cam2 = cv2.VideoCapture("C:/Users/Administrator/Desktop/csdn/002.mp4")
# 获取视频1的宽度
ww = int(cam1.get(3))
# 获取视频1的高度
hh = int(cam1.get(4))
print(ww, hh)
print(cam1.get(cv2.CAP_PROP_FPS))
# 获取视频的帧频
CAMERA_FPS = cam1.get(cv2.CAP_PROP_FPS) # 25帧/秒
# 定义视频编码格式
fourcc = cv2.VideoWriter_fourcc(*'WMV1')
# 创建视频保存对象
videoWriter = cv2.VideoWriter('C:/Users/Administrator/Desktop/out.wmv', fourcc, CAMERA_FPS,(ww * 2, hh))
# videoWriter = cv2.VideoWriter('C:/Users/Administrator/Desktop/out.wmv', fourcc, CAMERA_FPS,(ww, hh*2))
while True:
# 读取视频
(ok1, frame1) = cam1.read()
(ok2, frame2) = cam2.read()
if ok1 and ok2:
# 重置视频大小,使两视频大小一致
frame1 = cv2.resize(frame1, (ww, hh))
frame2 = cv2.resize(frame2, (ww, hh))
# 在视频中添加文字
cv2.putText(frame1, "Video 1", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 255, 0), 2)
cv2.putText(frame2, "Video 2", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 255, 0), 2)
# 拼接处理
image = np.concatenate([frame1, frame2], axis=1) # axis=0时为垂直拼接;axis=1时为水平拼接
# 视频展示
cv2.imshow("camera1", image)
# 视频保存
videoWriter.write(image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
videoWriter.release()
cam1.release()
cam2.release()
cv2.destroyAllWindows()
效果:
<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" id="GSt9LElZ-1584065979490" src="https://player.bilibili.com/player.html?aid=95610668"></iframe>视频拼接
视频整合
# -*- coding:utf-8 -*-
'''
视频整合
'''
import cv2
cam1 = cv2.VideoCapture("./videos/001.mp4")
cam2 = cv2.VideoCapture("./videos/002.mp4")
# 获取视频001的宽度与高度
ww, hh = int(cam1.get(3)), int(cam1.get(4))
print(cam1.get(cv2.CAP_PROP_FPS))
print(cam2.get(cv2.CAP_PROP_FPS))
# 获取视频的帧频
CAMERA_FPS = (cam1.get(cv2.CAP_PROP_FPS) + cam2.get(cv2.CAP_PROP_FPS)) // 2 # 25帧/秒
print(CAMERA_FPS)
# 定义视频编码格式
fourcc = cv2.VideoWriter_fourcc(*'WMV1')
# 创建视频保存对象
videoWriter = cv2.VideoWriter('./out2.wmv', fourcc, CAMERA_FPS,(ww, hh))
while True:
# 读取视频
(ok1, frame1) = cam1.read()
if not ok1:
(ok2, frame1) = cam2.read()
if ok1 or ok2:
# 重置视频大小,使两视频大小一致
frame1 = cv2.resize(frame1, (ww, hh))
# 视频展示
cv2.imshow("camera1", frame1)
# 视频保存
videoWriter.write(frame1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
videoWriter.release()
cam1.release()
cam2.release()
cv2.destroyAllWindows()
效果见上面的视频!
标签:视频,FPS,Python,cam1,cv2,拼接,图像处理,frame1 来源: https://blog.csdn.net/Thomson617/article/details/104801943