BoW in OpenCV using precomputed features

半城伤御伤魂 提交于 2019-12-02 15:28:44

问题


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?


回答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; 


来源:https://stackoverflow.com/questions/15611872/bow-in-opencv-using-precomputed-features

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!