How to use DescriptorMatcher to get matching images? [closed]

asked 2019-12-12 07:32:09 -0600

HelloWorld gravatar image

updated 2019-12-12 20:59:20 -0600

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
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-21 17:03:20.334248

Comments

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)

berak gravatar imageberak ( 2019-12-12 07:34:36 -0600 )edit

what is your detector ? SURF ? ORB ?

berak gravatar imageberak ( 2019-12-12 07:39:29 -0600 )edit

thank you , i update my question

HelloWorld gravatar imageHelloWorld ( 2019-12-12 08:30:38 -0600 )edit

I want to know which image the matcher matched

HelloWorld gravatar imageHelloWorld ( 2019-12-12 08:32:05 -0600 )edit

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"

berak gravatar imageberak ( 2019-12-13 02:00:58 -0600 )edit

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:

            traingImages.push_back(trainImage);// add towice
    traingImages.push_back(trainImage);
    labels.push_back(count);
    labels.push_back(count);
HelloWorld gravatar imageHelloWorld ( 2019-12-13 02:58:03 -0600 )edit

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

berak gravatar imageberak ( 2019-12-13 03:08:47 -0600 )edit

If you want to write something like this it will not take 5 minutes

LBerger gravatar imageLBerger ( 2019-12-13 03:10:50 -0600 )edit
1

Why not phash? @berak@LBerger

LiorA gravatar imageLiorA ( 2019-12-15 02:19:35 -0600 )edit