stitching:用于图像拼接的python第三方库
原文链接:https://github.com/OpenStitching/stitching
1. 安装第三方库:
pip install stitching
2. 在命令行终端使用:
(1)查看帮助:
stitch -h
(2)拼接指定的图片:
stitch img1.jpg img2.jpg img3.jpg
(3)拼接文件夹下所有图片:
stitch photo/*.png
2. 在python编辑器中使用:
(1)对指定路径的图片拼接并保存:
from stitching import Stitcher
import cv2
# 创建拼接器
stitcher = Stitcher()
# stitcher = Stitcher(detector="sift", confidence_threshold=0.2)
# 拼接图像
stitched_image = stitcher.stitch(["photo/*.png"])
# 保存结果
cv2.imwrite("stitched_image.png", stitched_image)
(2)对指定路径的视频拼接:
# 处理视频的拼接
from stitching import Stitcher
import cv2
# 读取两个视频文件
video1 = cv2.VideoCapture('device1_video.avi')
video2 = cv2.VideoCapture('device2_video.avi')
# 获取视频的帧数
frames1 = int(video1.get(cv2.CAP_PROP_FRAME_COUNT))
frames2 = int(video2.get(cv2.CAP_PROP_FRAME_COUNT))
# 获取视频的帧率和分辨率
fps = int(video1.get(cv2.CAP_PROP_FPS))
width = int(video1.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video1.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 设置拼接器
settings = {"detector": "orb", "confidence_threshold": 0.02}
stitcher = Stitcher(**settings)
# 创建 VideoWriter 对象用于保存拼接后的视频
output_size = (width * 2, height)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
result_video_writer = cv2.VideoWriter('stitched_video.avi', fourcc, fps, output_size)
# 逐帧读取并拼接视频
for frame_number in range(min(frames1, frames2)):
# 读取两个视频的当前帧
ret1, frame1 = video1.read()
ret2, frame2 = video2.read()
cv2.imshow('frame1', frame1) # 在窗口中显示接收到的图像
cv2.imshow('frame2', frame2) # 在窗口中显示接收到的图像
if not ret1 or not ret2:
break
# 拼接两个帧
stitched_frame = stitcher.stitch([frame1, frame2])
cv2.imshow('stitched_frame', stitched_frame) # 在窗口中显示接收到的图像
cv2.waitKey(1)
result_video_writer.write(stitched_frame)
# 释放资源
video1.release()
video2.release()
result_video_writer.release()
cv2.destroyAllWindows()