how to use opencv flann, especially set distance algorithms?
knn, opencv
Solution
I notice few people are familiar with openCV, and there are few documents. I finally avoid namespace conflict by using opencvflann instead of flann. And after checking the opencv source code, I found "dist.h" is a helpful file to find how to set different KNN distance types.
In my code, I use manhattan distance, referring to dist.h in 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));
Problem
I'm using opencv 2.4.4 flann. and I refer to: http://docs.opencv.org/2.4.4/modules/flann/doc/flann_fast_approximate_nearest_neighbor_search.html to do KNN. I have a matrix(8000*32) flann_m. There are 8000 data and each with 32 features. I wrote code like this: ``` flann::Index flann_index(flann_m, flann::LinearIndexParams()); flann_index.save("flann_index.fln"); Mat resps(ROW,K,CV_32F); Mat nresps(ROW,K,CV_16S); Mat dist(ROW,K,CV_32F); flann_index.knnSearch(flann_m,nresps,dist,K,flann::SearchParams(64)); ``` And I could get the KNN results in nresps and dist, with nresps the indexes of N neighbors, and dist the distances. But I don't know how to set different distance algorithm (ChiSquare, Euclidean, etc.) in opencv flann. I checked flann.cpp, and it seems the set_distance() function is deperecated.