How to use opencv flann, especially given distance algorithms?

I am 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

do knn.

I have a matrix (8000 * 32) flann_m. There are 8000 data, each of which has 32 functions.

I wrote the code as follows:

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 results of KNN in nresps and dist, with nresps indices N neighbors and dist dist.

But I do not know how to install another distance algorithm (ChiSquare, Euclidean, etc.) in opencv flann.

I checked flann.cpp and it seems that the set_distance () function is deformed.

+4
source share
1 answer

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)); 
+3
source

All Articles