開発環境
- 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(演習問題)2.を取り組んでみる。
コード(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
max_distance = 100
for σ in range(0, 11, 2):
print(f'Gaussian filter(σ = {σ})')
for i in range(3):
image1_blurred = ndimage.gaussian_filter(image1, σ)
image2_blurred = ndimage.gaussian_filter(image2, σ)
harris_image = harris.compute_harris_response(image1_blurred, 5)
filtered_cords1 = harris.get_harris_points(harris_image, width + 1)
d1 = harris.get_descriptors(image1_blurred, filtered_cords1, width)
harris_image = harris.compute_harris_response(image2_blurred, 5)
filtered_cords2 = harris.get_harris_points(harris_image, width + 1)
d2 = harris.get_descriptors(image2_blurred, filtered_cords2, width)
print('starting matching')
matches = harris.match_twosided(d1, d2)
print(f'matches: {len(matches)}')
plt.figure()
plt.gray()
plt.subplots_adjust(0, 0, 1, 1, 0, 0)
plot_matches(image1_blurred, image2_blurred, filtered_cords1,
filtered_cords2, matches, show_below=True,
max_distance=max_distance)
plt.savefig(f'sample2_{σ}.jpg')
plt.close()
入出力結果(Terminal, cmd(コマンドプロンプト), Jupyter(IPython))
$ ./sample2.py 1. Gaussian filter(σ = 0) starting matching matches: 224 Gaussian filter(σ = 2) starting matching matches: 218 Gaussian filter(σ = 4) starting matching matches: 232 Gaussian filter(σ = 6) starting matching matches: 279 Gaussian filter(σ = 8) starting matching matches: 327 Gaussian filter(σ = 10) starting matching matches: 392 $
0 コメント:
コメントを投稿