-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBruteForceMatching.py
More file actions
38 lines (29 loc) · 1.14 KB
/
Copy pathBruteForceMatching.py
File metadata and controls
38 lines (29 loc) · 1.14 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
34
35
36
37
38
import numpy as np
import cv2
from matplotlib import pyplot as plt
# Code from:
# https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html
# Prepared for M1IV students, by Prof. Slimane Larabi
#===================================================
img1 = cv2.imread('im1.png',0) # queryImage
img2 = cv2.imread('im1.jpg',0) # trainImage
# Initiate SIFT detector
sift = cv2.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
print(des1[0],kp1[0].pt)
# BFMatcher with default params
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)
# Sort them in the order of their distance.
#matches = sorted(matches, key = lambda x:x.distance)
# Apply ratio test
good = []
for m,n in matches:
if m.distance < 0.1*n.distance:
print(m.queryIdx, m.trainIdx, m.distance, n.queryIdx, n.trainIdx, n.distance, n.imgIdx)
good.append([m])
# cv2.drawMatchesKnn expects list of lists as matches.
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good, None, flags=2)
plt.imshow(img3),plt.show()