Using custom distance function with FLANN or Knn in OpenCv?

时间:2016-04-04 17:16:59

标签: c++ opencv distance knn flann

I'm working on code that computes dense SIFT features from a set of images, based on SIFT flow: http://people.csail.mit.edu/celiu/SIFTflow/

I'd like to try building a FLANN index on these images by comparing the "energy" between each image in SIFT flow representation.

I have the code to compute the energy from here: http://richardt.name/publications/video-deanaglyph/

Is there a way to create my own distance function for the indexing?


RELATED NOTE:

I was finally able to get an alternate (but not custom) distance function working with flann::Index. The trick is you need to use flann::GenericIndex like so:

flann::GenericIndex<cvflann::ChiSquareDistance<int>> flannIndex(descriptors, cvflann::KDTreeIndexParams());

But you need to give it CV_32S descriptors.

And if you use knnSearch with custom distance function, you have to provide a CV_32S results Mat and CV_32F distances Mat.

Here's my full code in case it's helpful (not a lot of documentation out there):

    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"));

Using Chi Squared actually seems to work better for me than L2 distance. My feature vectors are BoW histograms in this case.

0 个答案:

没有答案