Ask Your Question
0

OpenCV KNearest input

asked 2013-08-06 23:29:49 -0600

HiteshVaghani gravatar image

What is the exact meaning of the first parameter that takes the KNearest class in

OpenCV, CvMat* trainData?

Do I have to pass a CvMat* with the location of the centroids of the classes, or another info?

edit retag flag offensive close merge delete

Comments

You are right, the documentation (http://docs.opencv.org/modules/ml/doc/k_nearest_neighbors.html) is missing the first three parameters.

Alexander Pacha gravatar imageAlexander Pacha ( 2013-08-06 23:53:03 -0600 )edit
1

That is your features.use c++ & pass Mat instead of CvMat*

Mostafa Sataki gravatar imageMostafa Sataki ( 2013-08-07 01:39:31 -0600 )edit

Like @Mostafa Sataki mentioned, the second 'C++' directive is actually just a C++ wrap around the original C - style API, which requires these crazy structures like CvMat pointers. Like him I suggest taking the first C++ directive:

  C++: CvKNearest::CvKNearest(const Mat& trainData, const Mat& responses, const Mat& sampleIdx=Mat(), bool isRegression=false, int max_k=32 )
StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-07 01:43:58 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-08-06 23:54:21 -0600

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.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-08-06 23:29:49 -0600

Seen: 254 times

Last updated: Aug 06 '13