I notice that few people are familiar with openCV, and there are several documents. I finally avoid namespace conflicts by using opencvflann instead of flann. And after checking the opencv source code, I found that "dist.h" is a useful file to find how to set different types of KNN distances.
In my code, I use manhattan distance, referencing dist.h in the opencv source code.
//// do indexing Matrix<float> samplesMatrix((float*)flann_m.data, flann_m.rows, flann_m.cols); //Index<cvflann::ChiSquareDistance<float>> flann_index(samplesMatrix, cvflann::LinearIndexParams()); Index<cvflann::L1<float>> flann_index(samplesMatrix, cvflann::LinearIndexParams()); flann_index.buildIndex(); cv::Mat1i ind(flann_m.rows, K); CV_Assert(ind.isContinuous()); cvflann::Matrix<int> nresps((int*) ind.data, ind.rows, ind.cols); cvflann::Matrix<float> dist(new float[flann_m.rows*K], flann_m.rows, K); flann_index.knnSearch(samplesMatrix,nresps,dist,K,SearchParams(64));
source share