How to implement K nearest neighbour
Hello,
I am trying to use KNN but calling Im not sure how to initialize it. CvKNearest knn; does not work. It says CvKNearest is undefined. I followed this http://docs.opencv.org/2.4/modules/ml...
Is there a documentation that teaches me how to initialize classes in opencv ? I did this
flann::Index flan;
Mat M = (Mat_<double>(3, 3) << 1, 1, 1, 2, 2, 2, 3, 3, 3);
Mat query = (Mat_<double>(1, 3) << 1, 1, 1);
Mat index, dist;
flan.knnSearch(query, index, dist, 2);
But how do I tell the function to search the K nearest neighbour of Mat query given M (the data) ? It is giving me an error also, OpenCV Error: Assertion failed (query.type() == type && indices.type() == CV_32S && dists.type() == dtype) Help thanks. OpenCV is too hard for me.
It is the explanation for the Python interface but the main idea is identical. Click here for more info!
please clarify, if you want to do a classification (use ml::KNearest) or find the closest n neighbours (use flann::Index), then we can help you further.
Hello, I am trying to find both. Find the closest n neighbours then classify it. The input to knnSearch does not have the training data. I can only input the vector I want to query. What function can i use to find the closest n neighbours ?
Something like KNN(query, data, output)
the flann::Index does not classify, the ml::KNearest only gives you nearest class labels (not data items)
again, choose ;) (then we can come up with an example)
Hello thanks. I guess I would want ml::KNearest. I have looked through the doc http://docs.opencv.org/master/dd/de1/... but I am still extremely lost. Given an a Mat trainingData and Mat labels and Mat input, how do I use those functions to find the KNN ? ml::Knearest does not take in training data as input