Ask Your Question
0

How to implement K nearest neighbour

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

Nbb gravatar image

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

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.

edit retag flag offensive close merge delete

Comments

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 -0600 )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 -0600 )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 -0600 )edit
1

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 -0600 )edit
1

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

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

1 answer

Sort by ยป oldest newest most voted
3

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

berak gravatar image

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

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));
            num.convertTo(num,CV_32F);
            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
            }
            else
            {
                test_data.push_back(num.reshape(1,1));
                test_labels.push_back(int(r/5));
            }
        }
    }
    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 = test_labels.at<int>(i);
        int p = (int)res.at<float>(0);
        cerr << e << " : " << p << " " << neighbours << endl;
    }
    return 0;
}
edit flag offensive delete link more

Comments

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 -0600 )edit

Question Tools

2 followers

Stats

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

Seen: 4,203 times

Last updated: Mar 23 '16