1

I need to do BOW (bag of words) but I only have the described keypoints of the images. For the moment, I have obtained the vocabulary using:

cv::BOWKMeansTrainer bowtrainerCN(numCenters); //num clusters
bowtrainerCN.add(allDescriptors);
cv::Mat vocabularyCN = bowtrainerCN.cluster();

So now I need to do the assignment but I can't use the compute function because it calculates the descriptors of the images and I already have that. Is there any function to do the assignment or have I to compute it manually?

LizyCV
  • 23
  • 1
  • 4

1 Answers1

1

Once you have built the vocabulary (codebook) using cv::BOWKMeansTrainer::cluster() method, you can then match a descriptor (with suitable size and type) to the codebook. You first have to choose the type of matcher you need with a norm to use. (see opencv doc)

For example, with cv::BFMatcher and L2 norm

// init the matcher with you pre-trained codebook
cv::Ptr<cv::DescriptorMatcher > matcher = new cv::BFMatcher(cv::NORM_L2);
matcher->add(std::vector<cv::Mat>(1, vocabulary));
// matches
std::vector<cv::DMatch> matches;
matcher->match(new_descriptors,matches);

Then the index of the closest codeword in your codebook for the new_descriptors[i] will be

matches[i].trainIdx; 
Eric
  • 2,301
  • 3
  • 23
  • 30