Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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(3) neighbours:
        knn->findNearest(test_data.row(i), 3,res);
        int e = test_labels.at<int>(i);
        int p = (int)res.at<float>(0);
        cerr << e << " : " << p << endl;
    }
    return 0;
}

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(3) k(5) neighbours:
        knn->findNearest(test_data.row(i), 3,res);
5, res, neighbours);
        int e = test_labels.at<int>(i);
        int p = (int)res.at<float>(0);
        cerr << e << " : " << p << " " << neighbours << endl;
    }
    return 0;
}