イメージ・スティッチング

【サイト内の OpenCV 関連ページ】

【OpenCV の公式情報】

前準備

Python のインストール(Windows,Ubuntu 上)

Python 3.10 のインストール(Windows 上)

Pythonは,プログラミング言語の1つ.

手順

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
  2. 次のコマンドを実行

    次のコマンドは,Python ランチャーとPython 3.10をインストールする.

    winget install --scope machine Python.Launcher
    winget install --scope machine Python.Python.3.10
    

関連する外部ページ

サイト内の関連ページ

関連項目Python

サイト内の関連ページ

関連する外部ページPython の公式ページ: https://www.python.org/

Ubuntu のシステム Python

Ubuntu では,システム Pythonを使用できる.

Python 用 opencv-python, numpy のインストール

Windows でのインストール

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
  2. インストールの実行

    次のコマンドは,旧バージョンのものを削除し,Python 用 opencv-python のインストールを行う. 「python -c "import sys, cv2; print(f'Python version: {sys.version}\nOpenCV version: {cv2.__version__}')"」はインストールできたかの確認のため,バージョンを表示している.

    python -m pip uninstall -y opencv-python
    python -m pip uninstall -y opencv-python-headless
    python -m pip uninstall -y opencv-contrib-python
    python -m pip install -U opencv-python opencv-contrib-python numpy
    python -c "import sys, cv2; print(f'Python version: {sys.version}\nOpenCV version: {cv2.__version__}')"
    

Ubuntu でのインストール

  1. インストールの実行

    次のコマンドは,Python 用 opencv-python のインストールを行う. 「python3 -c "import sys, cv2; print(f'Python version: {sys.version}\nOpenCV version: {cv2.__version__}')"」はインストールできたかの確認のため,バージョンを表示している.

    sudo apt install -y python3-opencv python3-numpy
    python3 -c "import sys, cv2; print(f'Python version: {sys.version}\nOpenCV version: {cv2.__version__}')"
    

前準備

curl のインストール

ここで説明のためにサンプルとして使用する画像

画像のダウンロード

curl コマンドを用いてダウンロードできる.あるいは Web ブラウザでもダウンロードできる.

Ubuntu, RaspberryPi のときは,「sudo mkdir /usr/local/image; cd /usr/local/image/

cd %LOCALAPPDATA%
curl -L https://github.com/opencv/opencv_extra/raw/4.x/testdata/stitching/boat1.jpg?raw=true -o boat1.jpg
curl -L https://github.com/opencv/opencv_extra/raw/4.x/testdata/stitching/boat2.jpg?raw=true -o boat2.jpg
curl -L https://github.com/opencv/opencv_extra/raw/4.x/testdata/stitching/boat3.jpg?raw=true -o boat3.jpg
curl -L https://github.com/opencv/opencv_extra/raw/4.x/testdata/stitching/boat4.jpg?raw=true -o boat4.jpg
curl -L https://github.com/opencv/opencv_extra/raw/4.x/testdata/stitching/boat5.jpg?raw=true -o boat5.jpg
curl -L https://github.com/opencv/opencv_extra/raw/4.x/testdata/stitching/boat6.jpg?raw=true -o boat6.jpg

イメージスティッチングを行ってみる

  1. Windows, Ubuntu, RaspberryPi の場合

    Jupyter Qt Consoleを起動

    jupyter qtconsole
    
  2. Python プログラムの実行
    Ubuntu, RaspberryPi のときは,「IMROOT=os.environ['LOCALAPPDATA'] + '/'」の行を,「IMROOT="/usr/local/image/"」のように書き換える. Google Colaboratory のときは,「IMROOT=os.environ['LOCALAPPDATA'] + '/'」の行を,「IMROOT="./"」のように書き換える.
    import os
    import cv2
    %matplotlib inline
    import matplotlib.pyplot as plt
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    
    IMROOT=os.environ['LOCALAPPDATA'] + '/'
    image_names = [IMROOT + "boat1.jpg", IMROOT + "boat2.jpg", IMROOT + "boat3.jpg", IMROOT + "boat4.jpg", IMROOT + "boat5.jpg", IMROOT + "boat6.jpg"]
    images = []
    for i in image_names: 
        img = cv2.imread(i)
        images.append(img)
    
    stitcher = cv2.Stitcher.create()
    all = stitcher.stitch(images)
    
    plt.style.use('default')
    plt.imshow(cv2.cvtColor(all[1], cv2.COLOR_BGR2RGB))
    plt.show()