1/原文连接 矩阵匹配
knn 行列数组操作
#include "opencv2\opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
using namespace cv::ml;
int main()
{
Mat img = imread("digitals.png");
Mat gray;
cvtColor(img, gray, CV_BGR2GRAY);
int b = 20;
int m = gray.rows / b; //原图为1000*2000
int n = gray.cols / b; //裁剪为5000个20*20的小图块
Mat data, labels; //特征矩阵
for (int i = 0; i < n; i++)
{
int offsetCol = i*b; //列上的偏移量
for (int j = 0; j < m; j++)
{
int offsetRow = j*b; //行上的偏移量
//截取20*20的小块
Mat tmp;
gray(Range(offsetRow, offsetRow + b), Range(offsetCol, offsetCol + b)).copyTo(tmp);
data.push_back(tmp.reshape(0, 1)); //序列化后放入特征矩阵
labels.push_back((int)j / 5); //对应的标注
}
}
data.convertTo(data, CV_32F); //uchar型转换为cv_32f
int samplesNum = data.rows;
int trainNum = 3000;
Mat trainData, trainLabels;
trainData = data(Range(0, trainNum), Range::all()); //前3000个样本为训练数据
trainLabels = labels(Range(0, trainNum), Range::all());
//使用KNN算法
int K = 5;
Ptr<TrainData> tData = TrainData::create(trainData, ROW_SAMPLE, trainLabels);
Ptr<KNearest> model = KNearest::create();
model->setDefaultK(K);
model->setIsClassifier(true);
model->train(tData);
//svm分类
Ptr<SVM> svm = SVM::create();//SVM分类器
svm->setType(SVM::C_SVC);
svm->setC(0.01);
svm->setKernel(SVM::LINEAR);
svm->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 3000, 1e-6));
std::cout << "Starting training..." << endl;
svm->train(trainData, ROW_SAMPLE, trainLabels);
//训练分类器
std::cout << "Finishing training..." << endl;
//将训练好的SVM模型保存为xml文件
svm->SVM::save("SVM_HOG.xml");
//Ann分类
Ptr<ANN_MLP> ann = ANN_MLP::create();
Mat layerSizes = (Mat_<int>(1, 5) << 400, 128, 128, 128, 10);
ann->setLayerSizes(layerSizes);
ann->setTrainMethod(ANN_MLP::BACKPROP, 0.001, 0.1);
ann->setActivationFunction(ANN_MLP::SIGMOID_SYM, 1.0, 1.0);
ann->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER | TermCriteria::EPS, 10000, 0.0001));
Ptr<TrainData> trainDatas = TrainData::create(trainData, ROW_SAMPLE, trainLabels);
ann->train(trainDatas);
//保存训练结果
ann->save("MLPModel.xml");
//预测分类
double train_hr = 0, test_hr = 0;
Mat response;
// compute prediction error on train and test data
for (int i = 0; i < samplesNum; i++)
{
Mat sample = data.row(i);
float r = model->predict(sample); //对所有行进行预测
//预测结果与原结果相比,相等为1,不等为0
r = std::abs(r - labels.at<int>(i)) <= FLT_EPSILON ? 1.f : 0.f;
if (i < trainNum)
train_hr += r; //累积正确数
else
test_hr += r;
}
test_hr /= samplesNum - trainNum;
train_hr = trainNum > 0 ? train_hr / trainNum : 1.;
printf("accuracy: train = %.1f%%, test = %.1f%%\n",
train_hr*100., test_hr*100.);
//svm分类
double svmtrain_hr = 0, svmtest_hr = 0;
//Mat svmresponse;
// compute prediction error on train and test data
for (int i = 0; i < samplesNum; i++)
{
Mat sample = data.row(i);
float r =svm->predict(sample); //对所有行进行预测
//预测结果与原结果相比,相等为1,不等为0
r = std::abs(r - labels.at<int>(i)) <= FLT_EPSILON ? 1.f : 0.f;
if (i < trainNum)
svmtrain_hr += r; //累积正确数
else
svmtest_hr += r;
}
svmtest_hr /= samplesNum - trainNum;
train_hr = trainNum > 0 ? svmtrain_hr / trainNum : 1.;
printf("accuracy: train = %.1f%%, test = %.1f%%\n",
svmtrain_hr*100., svmtest_hr*100.);
//ann分类
//预测分类
double anntrain_hr = 0, anntest_hr = 0;
//Mat response;
// compute prediction error on train and test data
for (int i = 0; i < samplesNum; i++)
{
Mat sample = data.row(i);
float r = model->predict(sample); //对所有行进行预测
//预测结果与原结果相比,相等为1,不等为0
r = std::abs(r - labels.at<int>(i)) <= FLT_EPSILON ? 1.f : 0.f;
if (i < trainNum)
anntrain_hr += r; //累积正确数
else
anntest_hr += r;
}
anntest_hr /= samplesNum - trainNum;
anntrain_hr = trainNum > 0 ? anntrain_hr / trainNum : 1.;
printf("accuracy: train = %.1f%%, test = %.1f%%\n",
anntrain_hr*100., anntest_hr*100.);
waitKey(0);
getchar();
return 0;
}