開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
- 
    パッケージ
    - PIL(Python Image Library, Pillow)
- NumPy
- SciPy
- matplotlib
 
実践 コンピュータビジョン (Jan Erik Solem (著)、相川 愛三 (翻訳)、オライリージャパン)の2章(画像の局所記述子)、2.4(演習問題)1.を取り組んでみる。
コード(Emacs)
Python 3
#!/usr/bin/env python3
from PIL import Image
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
import harris
print('1.')
def plot_matches(im1, im2, locs1, locs2, matchscores,
                 show_below=True, max_distance=1000):
    """ 対応点を線で結んで画像を表示する
      入力: im1,im2(配列形式の画像)、locs1,locs2(特徴点座標)
         machescores(match()の出力)、
         show_below(対応の下に画像を表示するならTrue)"""
    im3 = harris.appendimages(im1, im2)
    if show_below:
        im3 = np.vstack((im3, im3))
    plt.imshow(im3)
    cols1 = im1.shape[1]
    for i, m in enumerate(matchscores):
        distance = np.sqrt((locs1[i][1] - locs2[m][1]) ** 2 +
                           (locs1[i][0] - locs2[m][0]) ** 2)
        if m > 0 and distance <= max_distance:
            plt.plot([locs1[i][1], locs2[m][1]+cols1],
                     [locs1[i][0], locs2[m][0]], 'r')
    plt.axis('off')
if __name__ == '__main__':
    filename1 = 'building1.jpg'
    filename2 = 'building2.jpg'
    image1 = np.array(Image.open(filename1).convert('L'))
    image2 = np.array(Image.open(filename2).convert('L'))
    width = 5
    harris_image = harris.compute_harris_response(image1, 5)
    filtered_cords1 = harris.get_harris_points(harris_image, width + 1)
    d1 = harris.get_descriptors(image1, filtered_cords1, width)
    harris_image = harris.compute_harris_response(image2, 5)
    filtered_cords2 = harris.get_harris_points(harris_image, width + 1)
    d2 = harris.get_descriptors(image1, filtered_cords2, width)
    print('starting matching')
    matches = harris.match_twosided(d1, d2)
    n = 3
    for m in range(0, n):
        distance = 10 ** (n - m)
        plt.figure()
        plt.gray()
        plt.subplots_adjust(0, 0, 1, 1, 0, 0)
        plot_matches(image1, image2, filtered_cords1,
                     filtered_cords2, matches, max_distance=distance)
        plt.savefig(f'sample1_{m}.jpg')
        plt.close()
入出力結果(Terminal, cmd(コマンドプロンプト), Jupyter(IPython))
$ ./sample1.py 1. starting matching $
 
0 コメント:
コメントを投稿