How to use DescriptorMatcher to get matching images? [closed]
I have some images, and I want to use DescriptorMatcher to get matching images, such as load 1.png, 2.png, 3..png, and then match to 1,2,3.
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
std::vector<std::vector<DMatch> > knn_matches;
for (int i = 0; i < trainingImages.size(); i++) {
std::vector<KeyPoint> keypoints;
Mat descriptors;
detector->detectAndCompute(trainingImages.at(i), noArray(), keypoints,
descriptors);
if (descriptors.type() != CV_32F) {
descriptors.convertTo(descriptors, CV_32F);
}
matcher->add(descriptors);
}
matcher->train();
Mat re;
matcher->knnMatch(descriptors1, knn_matches, 2, re);
the result re
is []
---------------------------update-------------------------------------
code:
//-- Step 1: Detect the keypoints using SURF Detector, compute the descriptors
Ptr<ORB> detector = ORB::create();
std::vector<KeyPoint> keypoints1;
Mat descriptors1;
Mat img1 = imread("3.jpg", IMREAD_GRAYSCALE);
detector->detectAndCompute(img1, noArray(), keypoints1, descriptors1);
//-- Step 2: Matching descriptor vectors with a FLANN based matcher
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(
DescriptorMatcher::BRUTEFORCE_HAMMING);
std::vector<std::vector<DMatch> > knn_matches;
for (int i = 0; i < trainingImages.size(); i++) {
std::vector<KeyPoint> keypoints;
Mat descriptors;
detector->detectAndCompute(trainingImages.at(i), noArray(), keypoints,
descriptors);
matcher->add(descriptors);
}
matcher->train();
// Mat reponse;
matcher->knnMatch(descriptors1, knn_matches, 2);
// cout << reponse << endl;
//-- Filter matches using the Lowe's ratio test
const float ratio_thresh = 0.7f;
std::vector<DMatch> good_matches;
// 过滤
for (size_t i = 0; i < knn_matches.size(); i++) {
if (knn_matches[i][0].distance
< ratio_thresh * knn_matches[i][1].distance) {
good_matches.push_back(knn_matches[i][0]);
}
}
cout << "ORB Matching Results" << endl;
cout << "*******************************" << endl;
cout << "# Matches: \t" << good_matches.size()
<< endl;
cout << endl;
output:
[]
ORB Matching Results
*******************************
# Matches: 102
sidenote: you MUST NOT convert descriptors to float,ever.
if they turn out to be CV_8U (ORB, FREAK or such), you need a different matcher (BFMatcher with hamming distance)
what is your detector ? SURF ? ORB ?
thank you , i update my question
I want to know which image the matcher matched
maybe you're on the wrong bus, then. it''s not the purpose of feature matching to find "similarity", but to give you a homography for 2 images, that are "known to match"
I don't know which method should be used to achieve my goal. I have multiple pictures and multiple tags. Because tags have different values, machine learning (ML) is not helpful here. Can I create multiple matchers and find the best match, or something like this:
again, feature matchng asyou do now, will never work for dissimilar images, it will only ret rieve bogus matches
maybe read up on "content based retrieval" (CBIR) and BagOfWords (BOW), VLAD, Fisher vectors
and imho you will need machinelearning, albeit in a more "unsupervised" fashion
If you want to write something like this it will not take 5 minutes
Why not phash? @berak@LBerger