What does the distance attribute in DMatches mean?
Asked Answered
D

3

27

I have a short question: When I do feature-matching in OpenCV, what does the distance attribute mean of DMatches in MatOfMatches?

I know that I have to filter matches with bigger distance because they aren't as good as them with lower distance. But what is the meaning of this attribute? Is it a kind of deviation?

Dryasdust answered 8/6, 2013 at 6:35 Comment(0)
P
32

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.

Provo answered 8/6, 2013 at 7:21 Comment(6)
One short question left, why do the matching algorithems gives me matches with big distance when they obviously not be identically?Dryasdust
They give the best match w.r.t the descriptors you have. Some are correct, some aren't. Different techniques exist to filter out the outliers, but the best one depends on the situation and this is not the job of the matcher itself...Provo
Can you please elaborate on the part about "A descriptor is a multidimensional vector" - it is indeed a matrix but what exactly do the single elements in it represent?Cuevas
I've read that a descriptor basically "captures" the intensity around a keypoint, which means that the descriptor matrix basically stores for each keypoint all the intensities where this keypoint has been detected. If we have keypoint A that is found in our image 10 times, then the row for this keypoint in the descriptor matrix will contain 10 intensity values. Is this correct or am I completely misunderstanding the whole thing?Cuevas
Distance is actually a measure of dissimilarity.Dex
I have a question. When you say "score", has that number limits?. I mean that score is number between 0 and 1, or something like that?.Merras
G
12

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.

Gee answered 20/12, 2017 at 7:35 Comment(0)
H
6

Usually when you are matching two features, you are actually comparing two vectors under certain distance metrics. Now let's assume your feature is SIFT with 128 dimensions, and you compare two SIFT features a and b using Euclidean distance, then DMatch.distance is equal to

formula

Halftimbered answered 8/6, 2013 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.