我正在研究基于 SIFT 流从一组图像中计算密集 SIFT 特征的代码:http: //people.csail.mit.edu/celiu/SIFTflow/
我想通过比较 SIFT 流表示中每个图像之间的“能量”来尝试在这些图像上建立一个 FLANN 索引。
我有从这里计算能量的代码:http ://richardt.name/publications/video-deanaglyph/
有没有办法为索引创建我自己的距离函数?
相关说明:
我终于能够获得与 flann::Index 一起使用的备用(但不是自定义)距离函数。诀窍是你需要像这样使用 flann::GenericIndex :
flann::GenericIndex<cvflann::ChiSquareDistance<int>> flannIndex(descriptors, cvflann::KDTreeIndexParams());
但是你需要给它CV_32S描述符。
如果您使用带有自定义距离函数的 knnSearch,则必须提供CV_32S结果 Mat 和CV_32F距离 Mat。
这是我的完整代码,以防万一有用(那里没有很多文档):
Mat samples;
loadDescriptors(samples); // loading descriptors from .yml file
samples *= 100000; // scaling up my descriptors to be int
samples.convertTo(samples, CV_32S); // convert float to int
// create flann index
flann::GenericIndex<cvflann::ChiSquareDistance<int>> flannIndex(samples, cvflann::KDTreeIndexParams());
// NOTE lack of distance type in constructor parameters
// (unlike flann::index)
// now try knnSearch
int k=10; // find 10 nearest neighbors
Mat results(1,10,CV_32S), dists(1,10,CV_32F);
// (1,10) Mats for the output, types CV_32S and CV_32F
Mat responseHistogram;
responseHistogram = samples.row(60);
// choose a random row from the descriptors Mat
// to find nearest neighbors
flannIndex.knnSearch(responseHist, results, dists, k, cvflann::SearchParams(200) );
cout << results << endl;
cout << dists << endl;
flannIndex.save(ofToDataPath("indexChi2.txt"));
实际上,使用卡方对我来说似乎比 L2 距离更有效。在这种情况下,我的特征向量是 BoW 直方图。