Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

so, here is a simple nearest neighbour classification.

(you'll find though, that it is less accurate, than an SVM or ANN_MLP):

void nearest(const cv::Mat &testData, const vector<cv::Mat> &trainData, int &best, double &mind)
{
    mind=DBL_MAX;
    best = -1;
    for (int r=0; r<trainData.rows; r++)
    {
        double d = norm(testData, trainData[r]);
        if (d < mind)
        {
            mind = d;
            best = r;
        }
    }
}

vector<Mat> trainData = ... 
vector<int> trainLabels = ...

Mat testData = ...
int best;
double dist;

nearest(testData, trainData, best, dist);

int predicted = trainLabels[best];
Mat closest = trainData[best];

so, here is a simple nearest neighbour classification.

(you'll find though, that it is less accurate, than an SVM or ANN_MLP):

void nearest(const cv::Mat &testData, const vector<cv::Mat> &trainData, int &best, double &mind)
{
    mind=DBL_MAX;
    best = -1;
    for (int (size_t r=0; r<trainData.rows; r<trainData.size(); r++)
    {
        double d = norm(testData, trainData[r]);
        if (d < mind)
        {
            mind = d;
            best = r;
        }
    }
}

vector<Mat> trainData = ... 
vector<int> trainLabels = ...

Mat testData = ...
int best;
double dist;

nearest(testData, trainData, best, dist);

int predicted = trainLabels[best];
Mat closest = trainData[best];