简介:OpenCV 利用特征提取、特征匹配、齐次估计、图像配准和图像融合等技术,将一系列图像合成为全景图。OpenCV 和 Pillow 是两个功能强大的 Python 图像处理库,但它们在处理全景图拼接方面有一些明显的区别。而Pillow 的图像融合功能较为简单,可能无法消除全景图中的接缝和阴影。在创建全景图方面,OpenCV 是一个更强大和灵活的解决方案。
历史攻略:
案例源码:
# -*- coding: utf-8 -*-
# time: 2023/5/7 11:33
# file: stitch_panorama.py
# 公众号: 玩转测试开发
import cv2
import sys
import os
def stitch_images(images):
stitcher = cv2.Stitcher_create()
status, panorama = stitcher.stitch(images)
if status == cv2.Stitcher_OK:
return panorama
else:
print("Error during stitching:", status)
return None
def load_images(image_paths):
images = []
for path in image_paths:
image = cv2.imread(path)
if image is None:
print(f"Error loading image {path}")
sys.exit(1)
images.append(image)
return images
def main(image_folder):
image_paths = [os.path.join(image_folder, img) for img in os.listdir(image_folder)]
images = load_images(image_paths)
panorama = stitch_images(images)
if panorama is not None:
cv2.imwrite("panorama.jpg", panorama)
print("Panorama saved as panorama.jpg")
else:
print("Panorama stitching failed")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python stitch_panorama.py <image_folder>")
sys.exit(1)
image_folder = sys.argv[1]
main(image_folder)
运行:
python stitch_panorama.py <path_to_image_folder>
合成前:
合成结果:效果上看,凑合能用。