Match keyframes and keep the good matches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Match keypoints in both images | |
# Based on: https://docs.opencv.org/master/dc/dc3/tutorial_py_matcher.html | |
FLANN_INDEX_KDTREE = 1 | |
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) | |
search_params = dict(checks=50) # or pass empty dictionary | |
flann = cv.FlannBasedMatcher(index_params, search_params) | |
matches = flann.knnMatch(des1, des2, k=2) | |
# Keep good matches: calculate distinctive image features | |
# Lowe, D.G. Distinctive Image Features from Scale-Invariant Keypoints. International Journal of Computer Vision 60, 91–110 (2004). https://doi.org/10.1023/B:VISI.0000029664.99615.94 | |
# https://www.cs.ubc.ca/~lowe/papers/ijcv04.pdf | |
matchesMask = [[0, 0] for i in range(len(matches))] | |
good = [] | |
pts1 = [] | |
pts2 = [] | |
for i, (m, n) in enumerate(matches): | |
if m.distance < 0.7*n.distance: | |
# Keep this keypoint pair | |
matchesMask[i] = [1, 0] | |
good.append(m) | |
pts2.append(kp2[m.trainIdx].pt) | |
pts1.append(kp1[m.queryIdx].pt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment