July 2017
Intermediate to advanced
382 pages
9h 13m
English
Now it's time to train the classifier.
As with all other machine learning functions, the k-NN classifier is part of OpenCV 3.1's ml module. We can create a new classifier using the following command:
In [15]: knn = cv2.ml.KNearest_create()
We then pass our training data to the train method:
In [16]: knn.train(train_data, cv2.ml.ROW_SAMPLE, labels)Out[16]: True
Here, we have to tell knn that our data is an N x 2 array (that is, every row is a data point). Upon success, the function returns True.
Read now
Unlock full access