The KNN classifier
K-nearest neighbors (KNN) is one of the simplest classifiers. It is a supervised classification method, which learns from available cases and classifies new cases by a minimum distance. K is the number of neighbors to be analyzed in the decision. The new data point to classify (the query) is projected to the same space as the learning points, and its class is given by the most frequent class among its KNN from the training set.
The following KNNClassifier
code is an example of using the KNN algorithm to classify each image pixel to the nearest color: black (0, 0, 0), white (255, 255, 255), blue (255, 0, 0), green (0, 255, 0), or red (0, 0, 255):
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/ml/ml.hpp> using namespace std; using namespace cv; int main(int argc, char *argv[]){ //Create Mat for the training set and classes Mat classes(5, 1, CV_32FC1); Mat colors(5, 3, CV_32FC1...