What is the distance used by K-nearest neighbour in opencv

asked 2015-07-14 15:44:37 -0600

studentstudent gravatar image

Hello,

I am using the machine learning k-nearest neighbour algrithm c++. I have a dataset with a high dimensional features. So, the euclidian distance will be not the best. I try to find which distance used in knearest.cpp to change it and used different distances but I could not because the code lacks comments. I find this block in the 189 but It seems that it not used in the find_nearest function. Any help?

> for( ; t < d; t++ )
>                 {
>                 
>                    double t0 = u[t] - v[t];
>                     sum += t0*t0;
>                     
>                 }
edit retag flag offensive close merge delete

Comments

yes, that's the euclidean distance. (why do you think, the code is not used ?) (why do you think, L2SQR norm is not adequate ?)

(oh, and please, hacking the library sounds like a bad idea, if you don't know, what you're doing, no ?)

berak gravatar imageberak ( 2015-07-14 23:28:20 -0600 )edit

you can find a long discussion here distance euclidian problems in high dimensions

studentstudent gravatar imagestudentstudent ( 2015-07-15 11:07:39 -0600 )edit

So, what is the solution to use other distances than euclidian distance? I think it was a bad idea to use just euclidian distance!! They have to make the distance used as an input parameters!

studentstudent gravatar imagestudentstudent ( 2015-07-15 11:12:51 -0600 )edit

hmm, the idea of having "means" in the 1st place is closely related to some kind of L2 distance, don't you think ?

i.e, if you need to do a BOW model from ORB features, and thus want hamming distance instead, another algorithm, like kmajority would be more appropriate.

berak gravatar imageberak ( 2015-07-15 11:18:32 -0600 )edit

In fact I need to use the chi-2 square distance because I have implemented it on mathematica and it gives a good results in my case. I need to use it with opencv knearest neighbours.

studentstudent gravatar imagestudentstudent ( 2015-07-15 11:26:20 -0600 )edit

ah, i see now.

berak gravatar imageberak ( 2015-07-15 11:29:22 -0600 )edit

The implementation of k nearest neighbour in opencv was not professional! In addition, we can not get the indices of the k-nearest neighbours and we can note use an other distance.

studentstudent gravatar imagestudentstudent ( 2015-07-15 11:33:06 -0600 )edit