1 | initial version |
assuming, you've done your PCA, and projected your data to PCA space:
PCA pca(data, Mat(), CV_PCA_DATA_AS_ROW, _num_components);
Mat projected = pca.project(data);
you can now setup KNearest for training:
CvKNearest knn; // assumes opencv2.4
Mat labels = ... // nimages rows, 1 column, containing an integer per image with the resp. age group.
knn.train(projected, labels);
once this has finished, you can predict with new data. (you'll need to keep the PCA and the KNN objects):
Mat img = ... // convert to float, reshape to a single row.
Mat query = pca.project(img);
int K = 3;
int predicted = (int) knn.find_nearest(query, K);
// now, 'predicted' holds the age-group-id, you fed into the labels when training.
2 | No.2 Revision |
assuming, you've done your PCA, and projected your data to PCA space:
PCA pca(data, Mat(), CV_PCA_DATA_AS_ROW, _num_components);
Mat projected = pca.project(data);
you can now setup KNearest for training:
CvKNearest knn; // assumes opencv2.4
Mat labels = ... // nimages rows, 1 column, containing an integer per image with the resp. age group.
knn.train(projected, labels);
once this has finished, you can predict with new data. (you'll need to keep the PCA and the KNN objects):
Mat img = ... // convert to float, reshape to a single row.
Mat query = pca.project(img);
int K = 3; // majority vote of K neighbours
int predicted = (int) knn.find_nearest(query, K);
// now, 'predicted' holds the age-group-id, you fed into the labels when training.