-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimilarity.py
More file actions
33 lines (25 loc) · 1.12 KB
/
similarity.py
File metadata and controls
33 lines (25 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import cv2
# 1枚目の画像を読み込む
img1 = cv2.imread("~/Desktop/test_11.jpeg")
# 2枚目の画像を読み込む
img2 = cv2.imread("~/Desktop/test_22.jpeg")
# 1枚目の画像をグレースケールで読み出し
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
# 2枚目の画像をグレースケールで読み出し
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# AKAZE検出器の生成
akaze = cv2.AKAZE_create()
# gray1にAKAZEを適用、特徴点を検出
kp1, des1 = akaze.detectAndCompute(gray1, None)
# gray2にAKAZEを適用、特徴点を検出
kp2, des2 = akaze.detectAndCompute(gray2, None)
# BFMatcherオブジェクトの生成
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Match descriptorsを生成
matches = bf.match(des1, des2)
# matchesをdescriptorsの似ている順にソートする
matches = sorted(matches, key = lambda x:x.distance)
# 検出結果を描画する
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, flags = cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
#検出結果を描画した画像を出力する
cv2.imwrite("~/Desktop/test_result.jpeg", img3)