-
-
Save michaelgruner/15a4fe72a0c32a23fe31bfff8cccfc7d to your computer and use it in GitHub Desktop.
LightGlue: Local Feature Matching at Light Speed
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
#!/usr/bin/env python3 | |
import numpy as np | |
import cv2 as cv | |
from matplotlib import pyplot as plt | |
MIN_MATCH_COUNT = 10 | |
img1 = cv.imread('./sensor4_00230.jpeg') | |
img2 = cv.imread('./sensor3_00230.jpeg') | |
# Initiate SIFT detector | |
sift = cv.SIFT_create() | |
# find the keypoints and descriptors with SIFT | |
kp1, des1 = sift.detectAndCompute(img1,None) | |
kp2, des2 = sift.detectAndCompute(img2,None) | |
FLANN_INDEX_KDTREE = 1 | |
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) | |
search_params = dict(checks = 50) | |
flann = cv.FlannBasedMatcher(index_params, search_params) | |
matches = flann.knnMatch(des1,des2,k=2) | |
# store all the good matches as per Lowe's ratio test. | |
good = [] | |
for m,n in matches: | |
if m.distance < 0.7*n.distance: | |
good.append(m) | |
if len(good)>MIN_MATCH_COUNT: | |
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2) | |
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2) | |
M, mask = cv.findHomography(dst_pts, src_pts, cv.RANSAC,5.0) | |
h,w,_ = img1.shape | |
T = np.float32([[1, 0, 0], [0, 1, h/2], [0, 0, 1]]) | |
M = T @ M | |
matchesMask = mask.ravel().tolist() | |
dst = cv.warpPerspective(img2, M, (w*2, h*2)) | |
dst[h//2:h//2+h ,0:w,:] = img1 | |
else: | |
print( "Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT) ) | |
matchesMask = None | |
plt.imshow(dst[...,::-1]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment