// Perform simple feature matching. This just uses the SSD // distance between two feature vectors, and matches a feature in the // first image with the closest feature in the second image. It can // match multiple features in the first image to the same feature in // the second image. void ssdMatchFeatures(const FeatureSet &f1, const FeatureSet &f2, vector<FeatureMatch> &matches, double &totalScore) { int m = f1.size(); int n = f2.size(); matches.resize(m); totalScore = 0; double d; double dBest; int idBest; for (int i=0; i<m; i++) { dBest = 1e100; idBest = 0; for (int j=0; j<n; j++) { d = distanceSSD(f1[i].data, f2[j].data); if (d < dBest) { dBest = d; idBest = f2[j].id; } } matches[i].id1 = f1[i].id; matches[i].id2 = idBest; matches[i].score = dBest; totalScore += matches[i].score; } }
// Perform ratio feature matching. This just uses the ratio of the SSD distance of the // two best matches and matches a feature in the first image with the closest feature // in the second image. It can match multiple features in the first image to the same // feature in the second image. void ratioMatchFeatures(const FeatureSet &f1, const FeatureSet &f2, vector<FeatureMatch> &matches) { int m = f1.size(); int n = f2.size(); matches.resize(m); double d; double dBest, dBest2; int idBest; for (int i=0; i<m; i++) { dBest = 1e100; dBest2 = 1e100; idBest = 0; for (int j=0; j<n; j++) { d = distanceSSD(f1[i].data, f2[j].data); if (d < dBest) { dBest2 = dBest; dBest = d; idBest = f2[j].id; } else if (d < dBest2) { dBest2 = d; } } matches[i].id1 = f1[i].id; matches[i].id2 = idBest; matches[i].distance = dBest/dBest2; } }