Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV drawMatches -- queryIdx and trainIdx

This is OpenCV's drawMatches() function:

void drawMatches(Mat img1, vector<KeyPoint> keypoints1,
                 Mat img2, vector<KeyPoint> keypoints2,
                 vector<DMatch> matches, 
                 Mat outImg) //want keypoints1[i] = keypoints2[matches[i]]

Notice that matches is of type vector<DMatch>. Here is the DMatch constructor:

DMatch(int queryIdx, int trainIdx, float distance)

Presumably, queryIdx is an index into one set of keypoints, and trainIdx is an index into the other set of keypoints.

The question: Is it true that queryIdx indexes into keypoints1, and trainIdx indexes into keypoints2? Or, is it the other way around?

like image 226
solvingPuzzles Avatar asked Nov 10 '12 03:11

solvingPuzzles


People also ask

What is queryIdx and trainIdx?

Presumably, queryIdx is an index into one set of keypoints, and trainIdx is an index into the other set of keypoints.

What is DMatch?

DMatch. distance - Distance between descriptors.


2 Answers

That depends on how you get matches.

If you call match function in the order:

match(descriptor_for_keypoints1, descriptor_for_keypoints2, matches)

then queryIdx refers to keypoints1 and trainIdx refers to keypoints2, or vice versa.

like image 128
luhb Avatar answered Oct 11 '22 09:10

luhb


the variable "matches" is a list of DMatch objects.

If we are iterating over this list of DMatch objects, then each item will have the following attributes:

  1. item.distance: This attribute gives us the distance between the descriptors. A lower distance indicates a better match.
  2. item.trainIdx: This attribute gives us the index of the descriptor in the list of train descriptors (in our case, it’s the list of descriptors in the img2).
  3. item.queryIdx: This attribute gives us the index of the descriptor in the list of query descriptors (in our case, it’s the list of descriptors in the img1).
  4. item.imgIdx: This attribute gives us the index of the train image.
like image 21
gsdf Avatar answered Oct 11 '22 10:10

gsdf