def match_descriptors(descriptors1, descriptors2, metric=None, p=2, max_distance=np.inf, cross_check=True):
 if (descriptors1.shape[1] != descriptors2.shape[1]):
  raise ValueError('Descriptor length must equal.')
 if (metric is None):
  if np.issubdtype(descriptors1.dtype, np.bool):
   metric = 'hamming'
  else:
   metric = 'euclidean'
 distances = cdist(descriptors1, descriptors2, metric=metric, p=p)
 indices1 = np.arange(descriptors1.shape[0])
 indices2 = np.argmin(distances, axis=1)
 if cross_check:
  matches1 = np.argmin(distances, axis=0)
  mask = (indices1 == matches1[indices2])
  indices1 = indices1[mask]
  indices2 = indices2[mask]
 matches = np.column_stack((indices1, indices2))
 if (max_distance < np.inf):
  matches = matches[(distances[(indices1, indices2)] < max_distance)]
 return matches

def match_descriptors ( descriptors1 , descriptors2 , metric = None , p = 2 , max_distance = np . inf , cross_check = True ) : if ( descriptors1 . shape [ 1 ] != descriptors2 . shape [ 1 ] ) : raise ValueError ( 'Descriptor length must equal.' ) if ( metric is None ) : if np . issubdtype ( descriptors1 . dtype , np . bool ) : metric = 'hamming' else : metric = 'euclidean' distances = cdist ( descriptors1 , descriptors2 , metric = metric , p = p ) indices1 = np . arange ( descriptors1 . shape [ 0 ] ) indices2 = np . argmin ( distances , axis = 1 ) if cross_check : matches1 = np . argmin ( distances , axis = 0 ) mask = ( indices1 == matches1 [ indices2 ] ) indices1 = indices1 [ mask ] indices2 = indices2 [ mask ] matches = np . column_stack ( ( indices1 , indices2 ) ) if ( max_distance < np . inf ) : matches = matches [ ( distances [ ( indices1 , indices2 ) ] < max_distance ) ] return matches
brute-force matching of descriptors .