Ask Your Question

Revision history [back]

As said here, it's the cache of all training samples, ie: you need an initial set. After that, you could request for all new vectors which are the closest in the training set. kNN is a classifier (that's why it inherits from CvStatModel. A typical used is: you have a training set of features computed from what you want, says ORB features from a dataset of images (but not limited to keypoints!) and you want to know for an unknown image, what is it. You train your kNN with the training set (ie: the features computed on each images you know the class), you compute the features on your unknown image, and you find the k nearest neighbors in the training set (with find_nearest). You look at the label of each of the nearest neighbors, and the majority label is probably the class for your image => you have recognize and image. This is just a typical used of kNN and there is many others!

If you are looking for a solution to cluster a group of data into K class (ie: for a set of vector, you want to make a partition into k groups), look at [kmeans](http://docs.opencv.org/modules/core/doc/clustering.html?highlight=kmeans#double kmeans(InputArray data, int K, InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers).