title: C++/OpenCV: How to use BOWImgDescriptorExtractor to determine which clusters relate to which images in the vocabulary?
body: <p>My goal is to take an image as a query and find its best match in a library of images. I am using SURF features, in openCV 3.0.0, and the Bag of Words approach to find a match. I need a way to find out if the query image has a match in the library. If it does, I want to know the index of the image that is the nearest match.</p>
  
  <p>Here is my code for reading in all the images (300 total in the library of images) and extracting and clustering the features:</p>
  
  <pre><code>Mat training_descriptors(1, extractor-&gt;descriptorSize(), extractor-&gt;descriptorType());
  //read in all images and set to binary
  char filepath[1000];
  for (int i = 1; i &lt; trainingSetSize; i++){
      cout &lt;&lt; "in for loop, iteration: " &lt;&lt; i &lt;&lt; endl;
      _snprintf_s(filepath, 100, "C:/Users/Randal/Desktop/TestCase1Training/%d.bmp", i);
      Mat temp = imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);
      Mat tempBW;
      adaptiveThreshold(temp, tempBW, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 2);
      detector-&gt;detect(tempBW, keypoints1);
      extractor-&gt;compute(tempBW, keypoints1, descriptors1);
      training_descriptors.push_back(descriptors1);
      cout &lt;&lt; "descriptors added" &lt;&lt; endl;
  
  }
  cout &lt;&lt; "Total descriptors: " &lt;&lt; training_descriptors.rows &lt;&lt; endl;
  trainer.add(training_descriptors);
  
  Ptr&lt;DescriptorMatcher&gt; matcher = DescriptorMatcher::create("FlannBased");
  BOWImgDescriptorExtractor BOW(extractor, matcher);
  Mat library = trainer.cluster();
  BOW.setVocabulary(library);
  </code></pre>
  
  <p>I wrote the following code in an attempt to find a match. The problem is that BOW.compute only returns the indices of clusters (words) that exist in both the image and the library of images. imgQ is the query image.</p>
  
  <pre><code>Mat output;
  Mat imgQBW;
  adaptiveThreshold(imgQ, imgQBW, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 11, 2);
  imshow("query image", imgQBW);
  detector-&gt;detect(imgQBW, keypoints2);
  extractor-&gt;compute(imgQBW, keypoints2, descriptors2);
  
  BOW.compute(imgQBW, keypoints1, output);
  cout &lt;&lt; output.row(0) &lt;&lt; endl;
  </code></pre>
  
  <p>I need to know which clusters in the BoW correspond to which images. My output right now--output.row(0)--is just an array with all the the indices of clusters found in the library. Am I misunderstanding this output? Is there a way to determine which image has the most matching clusters? </p>