Ask Your Question

Why does the BestOf2NearestMatcher's result depend on the image features input order?

asked 2013-05-16 07:49:47 -0600

Ben gravatar image

updated 2013-05-16 07:51:44 -0600

I get different matcher results with the BestOf2NearestMatcher from the stitching module when I swap the input order of the image features:

detail::BestOf2NearestMatcher matcher(false, 0.2);

matcher(imgFeat1, imgFeat2, matchesInfo); // 1095 matches, 811 inliers
matcher(imgFeat2, imgFeat1, matchesInfo); // 1107 matches, 787 inliers

Looking at the code of modules/stitching/src/matchers.cpp, line 179, I can't see a reason for this:

// Find 1->2 matches
matcher.knnMatch(features1.descriptors, features2.descriptors, pair_matches, 2);
for (size_t i = 0; i < pair_matches.size(); ++i)
    if (pair_matches[i].size() < 2)
    const DMatch& m0 = pair_matches[i][0];
    const DMatch& m1 = pair_matches[i][1];
    if (m0.distance < (1.f - match_conf_) * m1.distance)
        matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
LOG("\n1->2 matches: " << matches_info.matches.size() << endl);

// Find 2->1 matches
matcher.knnMatch(features2.descriptors, features1.descriptors, pair_matches, 2);
for (size_t i = 0; i < pair_matches.size(); ++i)
    if (pair_matches[i].size() < 2)
    const DMatch& m0 = pair_matches[i][0];
    const DMatch& m1 = pair_matches[i][1];
    if (m0.distance < (1.f - match_conf_) * m1.distance)
        if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
            matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));

It seems to me like it should make no difference, as 1->2 matches and 2->1 matches are both added to the match list. Can anyone tell me where the different results come from? In my opinion the input order should make no difference, so this might be a bug...

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-05-16 09:09:14 -0600

Nyenna gravatar image

updated 2013-05-17 07:46:48 -0600

When you match, say, image A to image B, you actually find the best correspondence in B for each feature in A.

And this is definitely not symmetric:

  • Your first image (corresponding to imgFeat1) has 1095 features. For each of those features, the algorithm gives you the closest feature in imgFeat2. It might happen that two features in imgFeat1 are matched to the same feature in imgFeat2.
  • Inversely, your second image has 1107 features that are matched to the features of the first image.
edit flag offensive delete link more


That would be true, if it was just a one-way match. But as you can see in the OpenCV code I posted in my question, the matching is done both ways and the results are merged.

Ben gravatar imageBen ( 2013-05-16 09:24:51 -0600 )edit

My mistake. But is it true that image1 has 1095 features and image2 has 1107?

Nyenna gravatar imageNyenna ( 2013-05-16 09:46:53 -0600 )edit

Why would I post it here otherwise? :D You can test it yourself.

Ben gravatar imageBen ( 2013-05-16 10:00:49 -0600 )edit

You posted the number of matches. My reasoning is that if it is the number of features as well, then your code is computing the matches only in one way...

Nyenna gravatar imageNyenna ( 2013-05-17 07:51:18 -0600 )edit

Do you always get the same number of matches? Actually, it is apparently using the FLANN library, which approximates the best match. Depending on the algorithm which is used, maybe it is simply not deterministic? More precisely, maybe it does not always find two matches for a feature and, in this case, it continue; while ignoring the feature. Is it possible?

Nyenna gravatar imageNyenna ( 2013-05-17 08:08:35 -0600 )edit

It is not my code. It's OpenCV code from the stitching module. And I don't think there is nondeterministic behavior in the knnMatch. Otherwise I probably wouldn't get the same amount of matches every time.

Ben gravatar imageBen ( 2013-05-21 09:04:41 -0600 )edit

That's exactly why I asked.

Nyenna gravatar imageNyenna ( 2013-05-22 02:08:47 -0600 )edit

Question Tools

1 follower


Asked: 2013-05-16 07:49:47 -0600

Seen: 811 times

Last updated: May 17 '13