
Python+OpenCVでstitching パノラマ画像生成をやってみた
2018-09-065 min read
目次
概要
PythonでOpenCVのstitchingを利用して複数の写真をつなぎ合わせる方法を紹介します。
いわゆるパノラマ画像と呼ばれるものを生成します。 OpenCVにSticherClassが用意されており、これを利用しました。
複数の画像を一度に読み込んでから計算しているので、何かしらのアプリケーションとして利用するのであれば、メモリとかを気にしなければならないと思います。 sticherクラスに関しては、こちらから
High level stitching API (Stitcher class) https://docs.opencv.org/3.3.0/d8/d19/tutorial_stitcher.html ググると公式のサンプルが見つかると思うので、これを参考にして実装しました。
出力
実行するとこのような感じになります。




ソース
import argparse
import cv2
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Stitch given images.')
parser.add_argument("inputs", metavar='input', type=str, nargs='+', help='input file')
parser.add_argument("--output", type=str, default='a.png', help='output file (default=a.png)')
args = parser.parse_args()
input_images = []
for i in args.inputs:
image = cv2.imread(i)
if image is None:
print(f'Error: Unable to open file "{i}".')
exit()
input_images.append(image)
if len(input_images) == 1:
cv2.imwrite(args.output, input_images[0])
else:
stitcher = cv2.createStitcher(True)
stitched = stitcher.stitch(input_images)
cv2.imwrite(args.output, stitched[1])
呼び出す場合のコマンド例
python3 main.py --output ./dst.jpg ./src1.jpg ./src2.jpg ./src3.jpg
他の画像でも試してみた
生成に成功した画像
公園の広場の写真。明暗が別れている写真でも繋げることができました。
構造物〜公園〜海の画像。
失敗した画像
以下の画像からの生成には失敗しました。被写体が海であったので特徴量が不足していたのかもしれません。
環境
Python3.6X OpenCV3.4.X Mac OS X
参考
https://docs.opencv.org/3.3.0/d0/d33/tutorial_table_of_content_stitching.html
Recommends
New Posts
Hot posts!
Date
Tags
Author