Ask Your Question

How to implement K nearest neighbour

asked 2016-03-22 07:50:56 -0500

Nbb gravatar image

updated 2016-03-22 08:10:05 -0500


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

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.

edit retag flag offensive close merge delete


It is the explanation for the Python interface but the main idea is identical. Click here for more info!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-03-22 08:17:33 -0500 )edit

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.

berak gravatar imageberak ( 2016-03-22 09:07:17 -0500 )edit

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)

Nbb gravatar imageNbb ( 2016-03-23 05:02:51 -0500 )edit

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)

berak gravatar imageberak ( 2016-03-23 05:24:59 -0500 )edit

Hello thanks. I guess I would want ml::KNearest. I have looked through the doc 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

Nbb gravatar imageNbb ( 2016-03-23 05:39:13 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-03-23 05:49:23 -0500

berak gravatar image

updated 2016-03-23 05:53:27 -0500

so, here's a KNearest example for opencv3:

#include <iostream>
using namespace std;

#include <opencv2\opencv.hpp>
using namespace cv;
using namespace cv::ml;

int main(int argc, char **argv)
    Mat train_data, train_labels;
    Mat test_data, test_labels;
    Mat digits = imread("samples/data/digits.png",0); // have a look at it !
    for (int r=0; r<50; r++) // 10 digits a 5 rows
        for (int c=0; c<100; c++)  // 100 digits per row
            Mat num = digits(Rect(c*20,r*20,20,20));
            if (c%2==0) // 50/50 split
                train_data.push_back(num.reshape(1,1)); // each digit goes on a single row
                train_labels.push_back(int(r/5));  // we need 1 (integer) label per feature row
    Ptr<ml::KNearest> knn = ml::KNearest::create();
    knn->train(train_data, ml::ROW_DATA, train_labels);

    for (int i=0; i<test_data.rows; i++)
        Mat res;
        // predict on majority of k(5) neighbours:
        knn->findNearest(test_data.row(i), 5, res, neighbours);
        int e =<int>(i);
        int p = (int)<float>(0);
        cerr << e << " : " << p << " " << neighbours << endl;
    return 0;
edit flag offensive delete link more


Amazing thanks berak ! knn->train(train_data, ml::ROW_DATA, train_labels); so that was how you got the function to store the samples :) Thanks again

Nbb gravatar imageNbb ( 2016-03-23 09:08:25 -0500 )edit

Question Tools



Asked: 2016-03-22 07:50:56 -0500

Seen: 3,009 times

Last updated: Mar 23 '16