Skip to content

Instantly share code, notes, and snippets.

@mihaidusmanu
Created July 22, 2019 14:41
Show Gist options
  • Save mihaidusmanu/20fd0904b2102acc1330bad9b4badab8 to your computer and use it in GitHub Desktop.
Save mihaidusmanu/20fd0904b2102acc1330bad9b4badab8 to your computer and use it in GitHub Desktop.
Mutual NN Matcher (numpy)
import numpy as np
def mutual_nn_matcher(descriptors1, descriptors2):
descriptors1 = descriptors1 / np.linalg.norm(descriptors1, axis=1)[:, np.newaxis]
descriptors2 = descriptors2 / np.linalg.norm(descriptors2, axis=1)[:, np.newaxis]
similarity = descriptors1 @ descriptors2.transpose()
nn12 = np.argmax(similarity, axis=1)
nn21 = np.argmax(similarity, axis=0)
ids1 = np.arange(similarity.shape[0])
valid_matches = (ids1 == nn21[nn12])
return np.vstack([
ids1[valid_matches], nn12[valid_matches]
]).transpose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment