2011-04-17 81 views
3

http://opencv.willowgarage.com/documentation/cpp/features2d_common_interfaces_of_descriptor_matchers.html#flannbasedmatcher如何使用基于flann的匹配器,或者通常在opencv中使用flann?

请有人向我展示示例代码或告诉我如何使用这个类和方法。 我只想将查询图像中的SURF与通过应用Flann设置的图像匹配。我在样本中看到了很多图像匹配代码,但是我仍然没有找到量化图像与其他图像相似程度的指标。任何帮助都感激不尽。

回答

10

这里的未经测试示例代码

using namespace std; 
using namespace cv; 

Mat query; //the query image 
vector<Mat> images; //set of images in your db 

/* ... get the images from somewhere ... */ 

vector<vector<KeyPoint> > dbKeypoints; 
vector<Mat> dbDescriptors; 

vector<KeyPoint> queryKeypoints; 
Mat queryDescriptors; 

/* ... Extract the descriptors ... */ 

FlannBasedMatcher flannmatcher; 

//train with descriptors from your db 
flannmatcher.add(dbDescriptors); 
flannmatcher.train(); 

vector<DMatch > matches; 

flannmatcher.match(queryDescriptors, matches); 

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

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

*/ 

找到最“类似”的图像与查询图像取决于你的应用。也许匹配关键点的数量是足够的。或者您可能需要更复杂的相似度量度。

+0

感谢您的回复,“queryKeypoints [matches [kk] .queryIdx] .pt 的最佳匹配是dbKeypoints [matches [kk] .imgIdx] [matches [kk] .trainIdx] .pt”如何做到这一点部分,如何确定最佳匹配,要实现的算法或opencv中的方法。 – AquaAsh 2011-04-18 07:28:28

+0

函数调用flannmatcher.match(queryDescriptors,matches);做匹配。你所要做的就是使用向量匹配中的索引。 – Sammy 2011-04-18 12:21:18

+0

对不起,迟到的答复,谢谢,我终于明白了索引的事情。无论如何,我试图减少误报,你能否提出任何复杂的相似度量。 – AquaAsh 2011-04-25 13:32:54

1

为了减少误报的次数,您可以通过比较那些距离的比率来比较第一个最靠近的第二个最邻近的邻居。 distance(query,mostnearestneighbor)/ distance(query,secondnearestneighbor)< T,比值越小,第二个最近邻居与查询描述符的距离越大。这因此是高度独特性的翻译。用于设计注册的许多计算机视觉论文。

相关问题