#include <iostream>
#include <math.h>
#include <string>
#include "cv.h"
#include "ml.h"
#include "highgui.h" using namespace cv;
using namespace std; bool plotSupportVectors=true;
int numTrainingPoints=;
int numTestPoints=;
int size=;
int eq=; // accuracy
float evaluate(cv::Mat& predicted, cv::Mat& actual) {
assert(predicted.rows == actual.rows);
int t = ;
int f = ;
for(int i = ; i < actual.rows; i++) {
float p = predicted.at<float>(i,);
float a = actual.at<float>(i,);
if((p >= 0.0 && a >= 0.0) || (p <= 0.0 && a <= 0.0)) {
t++;
} else {
f++;
}
}
return (t * 1.0) / (t + f);
} // plot data and class
void plot_binary(cv::Mat& data, cv::Mat& classes, string name) {
cv::Mat plot(size, size, CV_8UC3);
plot.setTo(cv::Scalar(255.0,255.0,255.0));
for(int i = ; i < data.rows; i++) { float x = data.at<float>(i,) * size;
float y = data.at<float>(i,) * size; if(classes.at<float>(i, ) > ) {
cv::circle(plot, Point(x,y), , CV_RGB(,,),);
} else {
cv::circle(plot, Point(x,y), , CV_RGB(,,),);
}
}
cv::imshow(name, plot);
} // function to learn
int f(float x, float y, int equation) {
switch(equation) {
case :
return y > sin(x*) ? - : ;
break;
case :
return y > cos(x * ) ? - : ;
break;
case :
return y > *x ? - : ;
break;
case :
return y > tan(x*) ? - : ;
break;
default:
return y > cos(x*) ? - : ;
}
} // label data with equation
cv::Mat labelData(cv::Mat points, int equation) {
cv::Mat labels(points.rows, , CV_32FC1);
for(int i = ; i < points.rows; i++) {
float x = points.at<float>(i,);
float y = points.at<float>(i,);
labels.at<float>(i, ) = f(x, y, equation);
}
return labels;
} void svm(cv::Mat& trainingData, cv::Mat& trainingClasses, cv::Mat& testData, cv::Mat& testClasses) {
CvSVMParams param = CvSVMParams(); param.svm_type = CvSVM::C_SVC;
param.kernel_type = CvSVM::RBF; //CvSVM::RBF, CvSVM::LINEAR ...
param.degree = ; // for poly
param.gamma = ; // for poly/rbf/sigmoid
param.coef0 = ; // for poly/sigmoid param.C = ; // for CV_SVM_C_SVC, CV_SVM_EPS_SVR and CV_SVM_NU_SVR
param.nu = 0.0; // for CV_SVM_NU_SVC, CV_SVM_ONE_CLASS, and CV_SVM_NU_SVR
param.p = 0.0; // for CV_SVM_EPS_SVR param.class_weights = NULL; // for CV_SVM_C_SVC
param.term_crit.type = CV_TERMCRIT_ITER +CV_TERMCRIT_EPS;
param.term_crit.max_iter = ;
param.term_crit.epsilon = 1e-; // SVM training (use train auto for OpenCV>=2.0)
CvSVM svm(trainingData, trainingClasses, cv::Mat(), cv::Mat(), param); cv::Mat predicted(testClasses.rows, , CV_32F); for(int i = ; i < testData.rows; i++) {
cv::Mat sample = testData.row(i); float x = sample.at<float>(,);
float y = sample.at<float>(,); predicted.at<float>(i, ) = svm.predict(sample);
} cout << "Accuracy_{SVM} = " << evaluate(predicted, testClasses) << endl;
plot_binary(testData, predicted, "Predictions SVM"); // plot support vectors
if(plotSupportVectors) {
cv::Mat plot_sv(size, size, CV_8UC3);
plot_sv.setTo(cv::Scalar(255.0,255.0,255.0)); int svec_count = svm.get_support_vector_count();
for(int vecNum = ; vecNum < svec_count; vecNum++) {
const float* vec = svm.get_support_vector(vecNum);
cv::circle(plot_sv, Point(vec[]*size, vec[]*size), , CV_RGB(, , ));
}
cv::imshow("Support Vectors", plot_sv);
}
} void mlp(cv::Mat& trainingData, cv::Mat& trainingClasses, cv::Mat& testData, cv::Mat& testClasses) { cv::Mat layers = cv::Mat(, , CV_32SC1); layers.row() = cv::Scalar();
layers.row() = cv::Scalar();
layers.row() = cv::Scalar();
layers.row() = cv::Scalar(); CvANN_MLP mlp;
CvANN_MLP_TrainParams params;
CvTermCriteria criteria;
criteria.max_iter = ;
criteria.epsilon = 0.00001f;
criteria.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;
params.train_method = CvANN_MLP_TrainParams::BACKPROP;
params.bp_dw_scale = 0.05f;
params.bp_moment_scale = 0.05f;
params.term_crit = criteria; mlp.create(layers); // train
mlp.train(trainingData, trainingClasses, cv::Mat(), cv::Mat(), params); cv::Mat response(, , CV_32FC1);
cv::Mat predicted(testClasses.rows, , CV_32F);
for(int i = ; i < testData.rows; i++) {
cv::Mat response(, , CV_32FC1);
cv::Mat sample = testData.row(i); mlp.predict(sample, response);
predicted.at<float>(i,) = response.at<float>(,); } cout << "Accuracy_{MLP} = " << evaluate(predicted, testClasses) << endl;
plot_binary(testData, predicted, "Predictions Backpropagation");
} void knn(cv::Mat& trainingData, cv::Mat& trainingClasses, cv::Mat& testData, cv::Mat& testClasses, int K) { CvKNearest knn(trainingData, trainingClasses, cv::Mat(), false, K);
cv::Mat predicted(testClasses.rows, , CV_32F);
for(int i = ; i < testData.rows; i++) {
const cv::Mat sample = testData.row(i);
predicted.at<float>(i,) = knn.find_nearest(sample, K);
} cout << "Accuracy_{KNN} = " << evaluate(predicted, testClasses) << endl;
plot_binary(testData, predicted, "Predictions KNN"); } void bayes(cv::Mat& trainingData, cv::Mat& trainingClasses, cv::Mat& testData, cv::Mat& testClasses) { CvNormalBayesClassifier bayes(trainingData, trainingClasses);
cv::Mat predicted(testClasses.rows, , CV_32F);
for (int i = ; i < testData.rows; i++) {
const cv::Mat sample = testData.row(i);
predicted.at<float> (i, ) = bayes.predict(sample);
} cout << "Accuracy_{BAYES} = " << evaluate(predicted, testClasses) << endl;
plot_binary(testData, predicted, "Predictions Bayes"); } void decisiontree(cv::Mat& trainingData, cv::Mat& trainingClasses, cv::Mat& testData, cv::Mat& testClasses) { CvDTree dtree;
cv::Mat var_type(, , CV_8U); // define attributes as numerical
var_type.at<unsigned int>(,) = CV_VAR_NUMERICAL;
var_type.at<unsigned int>(,) = CV_VAR_NUMERICAL;
// define output node as numerical
var_type.at<unsigned int>(,) = CV_VAR_NUMERICAL; dtree.train(trainingData,CV_ROW_SAMPLE, trainingClasses, cv::Mat(), cv::Mat(), var_type, cv::Mat(), CvDTreeParams());
cv::Mat predicted(testClasses.rows, , CV_32F);
for (int i = ; i < testData.rows; i++) {
const cv::Mat sample = testData.row(i);
CvDTreeNode* prediction = dtree.predict(sample);
predicted.at<float> (i, ) = prediction->value;
} cout << "Accuracy_{TREE} = " << evaluate(predicted, testClasses) << endl;
plot_binary(testData, predicted, "Predictions tree"); } int main() { cv::Mat trainingData(numTrainingPoints, , CV_32FC1);
cv::Mat testData(numTestPoints, , CV_32FC1); cv::randu(trainingData,,);
cv::randu(testData,,); cv::Mat trainingClasses = labelData(trainingData, eq);
cv::Mat testClasses = labelData(testData, eq); plot_binary(trainingData, trainingClasses, "Training Data");
plot_binary(testData, testClasses, "Test Data"); svm(trainingData, trainingClasses, testData, testClasses);
mlp(trainingData, trainingClasses, testData, testClasses);
knn(trainingData, trainingClasses, testData, testClasses, );
bayes(trainingData, trainingClasses, testData, testClasses);
decisiontree(trainingData, trainingClasses, testData, testClasses); cv::waitKey(); return ;
}
图像分类结果: