What does the distance attribute in DMatches mean?

In this context, a feature is a point of interest on the image. In order to compare features, you "describe" them using a feature detector. Each feature is then associated to a descriptor. When you match features, you actually match their descriptors.

A descriptor is a multidimensional vector. It can be real-valued (e.g. SIFT) or binary (e.g. BRIEF).

A matching is a pair of descriptors, one from each image, which are the most similar among all of the descriptors. And of course, to find the descriptor in image B that is the most similar to a descriptor in image A, you need a measure of this similarity.

There are multiple ways to compute a "score of similarity" between two vectors. For real-valued descriptors, the Euclidean distance is often used, when the Hamming distance is common for binary descriptors.

As a conclusion, we can now understand the distance attribute: it is the score of similarity between the two descriptors of a match.


Distance attribute in DMatch is a measure of similarity between the two descriptors(feature vectors). If the distance is less, then the images are more similar and vice versa.

A lesson learnt from my experience when I started out:

Do not confuse the DMatch.distance with the normal spatial distance between two points. Both are different. The distance in the DMatch represents the distance between two descriptors(vectors with 128 values in case of SIFT)

In case of SIFT (local feature descriptor):

1) First, you detect key points(interesting points) for the two images that you want to compare.

2) Then you compute sift descriptors for a defined area (16 X 16 neighbourhood around each key point) around all the key points. Each descriptor stores the histogram of oriented gradients for the area around each key point.

3) Finally, the descriptors of both the images are matched to find matching key points between the images. This is done by using BFMatcher -> match(), knnMatch() or FlannBasedMatcher -> knnMatch().

4) If you are using BFMatcher.match(), you will get a list of DMatch objects. The number of DMatch objects is equal to the number of matches. Each DMatch object contains following four attributes for each matched key point pair.

DMatch.distance - Distance between descriptors. The lower, the better it is.
DMatch.trainIdx - Index of the descriptor in train descriptors(1st image)
DMatch.queryIdx - Index of the descriptor in query descriptors(2nd image)
DMatch.imgIdx - Index of the train image.

5) DMatch.Distance can be one of many distance measures -> Norm_L1, Norm_L2(Euclidean distance), Hamming distance, Hamming2 distance,... which can be mentioned as a parameter in BFMatcher. The Default distance is Euclidean.

6) Difference between Spatial Euclidean distance and DMatch Euclidean distance:

SIFT descriptor 1 -> [a1,a2,....a128]

SIFT descriptor 2 -> [b1,b2,....b128]

(DMatch) -> Euclidean distance = sqrt[(a1-b1)^2 + (a2-b2)^2 +...+(a128-b128)^2]

Point 1 -> (x1, y1)

Point 2 -> (x2, y2)

(Spatial) -> Euclidean distance = sqrt[(x2-x1)^2 + (y2-y1)^2]

Thus, this distance from DMatch is the distance between descriptors and it represents the degree of similarity between two descriptors and is different from the normal spatial euclidean distance between two points.

If the distance between the descriptors is less, then their similarity is high. If the distance between the descriptors is more, then their similarity is low.

Hope this helps in understanding the meaning of distance attribute in DMatch objects. If you are clear on this, then you can work with any feature descriptors like HOG, SIFT, SURF, ORB, BRISK, FREAK,... All of them are similar when it comes to matching their respective feature descriptors.