1

I have a follow-up question regarding this previously asked question:

How to use flann based matcher, or generally flann in opencv?

specifically regarding this portion of the answer:

/* for kk=1 to matches.size()

       the best match for queryKeypoints[matches[kk].queryIdx].pt 
       is dbKeypoints[matches[kk].imgIdx][matches[kk].trainIdx].pt
 */

My question is the following: is it possible to access the image from which the best matching keypoint (shortest distance) has been found using opencv's flannbasedmatcher match() function and dmatch structure, and if so, how might one go about doing this?

Specifically, how does one do this when comparing a query image to a set of images? In this case, is the flann k-d tree built with all keypoints mixed together (then the corresponding image label is not attached with the keypoint in the tree)? Or is there a way to access the image to which a given keypoint belongs?

Any other pointers as to how the flannbasedmatcher match() and train() functions work would also be great, as I have scoured the source code and still am somewhat mystified -- thank you.

Any guidance is much appreciated -- thank you!

Community
  • 1
  • 1
user1325426
  • 11
  • 1
  • 5

1 Answers1

2

The Dmatch attribute imgIdx reports the index of the "train" image to which the matched keypoint belongs to. Using DescriptorMatcher.:add you append a vector of descriptors to the vector of the "training" images descriptors. It's up to you to know that image id X corresponds to a specific image file.

Marco
  • 2,389
  • 2
  • 16
  • 19
  • 1
    Thank you very much for your response. I have created a vector of trainDescriptors, adding a a cv::Mat descriptor into this vector for each of my training images. What surprises me is that if I have 3 entries (3 descriptor sets for 3 training images) in my vector of train descriptors, I sometimes still get an imgIdx value of 5 (or some such number greater than my number of training images). Is this expected? I would expect an imgIdx no greater than 2 (0, 1, or 2) for a 3-image trainDescriptor size. I appreciate any suggestions. – user1325426 May 19 '12 at 00:10
  • To be more specific, I am using the following function: 'void DescriptorMatcher::match( const Mat& queryDescriptors, vector& matches, const vector& masks )' for the matching between one query image and an image set. I then retrieve 'matches.at(0).imgIdx' (matches.at(0) should be the best (shortest distance) matching keypoint, so the imgIdx should tell me the the best cv::Mat trainDescriptor index from which this best keypoint came, equal to the best image index from my original vector of images, right? But I'm getting an imgIdx greater than my original number of trainDescriptors. – user1325426 May 20 '12 at 07:07