Skip to content

Instantly share code, notes, and snippets.

@ankit-maverick
Created September 20, 2013 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankit-maverick/6634072 to your computer and use it in GitHub Desktop.
Save ankit-maverick/6634072 to your computer and use it in GitHub Desktop.
def match_keypoints_brief(keypoints1, descriptors1, keypoints2,
descriptors2, threshold=0.40):
"""Match keypoints described using BRIEF descriptors in one image to
those in second image.
Parameters
----------
keypoints1 : (M, 2) ndarray
M Keypoints from the first image described using skimage.feature.brief
descriptors1 : (M, P) ndarray
BRIEF descriptors of size P about M keypoints in the first image.
keypoints2 : (N, 2) ndarray
N Keypoints from the second image described using skimage.feature.brief
descriptors2 : (N, P) ndarray
BRIEF descriptors of size P about N keypoints in the second image.
threshold : float in range [0, 1]
Maximum allowable hamming distance between descriptors of two keypoints
in separate images to be regarded as a match.
Returns
-------
match_keypoints_brief : (Q, 2, 2) ndarray
Location of Q matched keypoint pairs from two images.
"""
if (keypoints1.shape[0] != descriptors1.shape[0]
or keypoints2.shape[0] != descriptors2.shape[0]):
raise ValueError("The number of keypoints and number of described "
"keypoints do not match.")
if descriptors1.shape[1] != descriptors2.shape[1]:
raise ValueError("Descriptor sizes for matching keypoints in both "
"the images should be equal.")
# Get hamming distances between keypoints1 and keypoints2
distance = pairwise_hamming_distance(descriptors1, descriptors2)
matched_keypoints1_index = np.argmin(distance, axis=0)
matched_keypoints2_index = np.argmin(distance, axis=1)
matched_index = []
for i in range(len(matched_keypoints1_index)):
if (matched_keypoints2_index[matched_keypoints1_index[i]] == i and
distance[i, matched_keypoints1_index[i]] < threshold):
matched_index.append([i, matched_keypoints1_index[i]])
matched_index = np.asarray(matched_index)
matched_keypoint_pairs = np.zeros((matched_index.shape[0], 2, 2), dtype=np.intp)
matched_keypoint_pairs[:, 0, :] = keypoints1[matched_index[:, 0]]
matched_keypoint_pairs[:, 1, :] = keypoints2[matched_index[:, 1]]
return matched_keypoint_pairs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment