图像拼接库stitching

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()

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值