#include "stdafx.h"
#include <iostream>
//#include <opencv2/core/core.hpp>
//#include <opencv2/ml/ml.hpp>
#include <opencv2/opencv.hpp>
#include <io.h>
using namespace cv;
using namespace std;
#define CLASS_NUM 10
#define MAT_WEIGHT 16
#define MAT_HEIGHT 32
#define uchar unsigned char
void getFeature(Mat &X_train, Mat temp_img, int nIndex)
{
cvtColor(temp_img, temp_img, CV_BGR2GRAY);
//判断图像是否加载成功
if (temp_img.empty())
{
cout << "图像加载失败" << endl;
return ;
}
for (size_t i = 0; i < MAT_HEIGHT; i++)
{
for (size_t j = 0; j < MAT_WEIGHT; j++)
{
X_train.at<float>(nIndex, i*MAT_WEIGHT + j) = (float)temp_img.at<uchar>(i, j)/255;
}
}
}
int main()
{
int in_layer = 512;
int out_layer = 2;
int SAMPLES = 10; // 训练样本总量
Mat X_train(SAMPLES, in_layer, CV_32FC1);
Mat Y_train(SAMPLES, out_layer, CV_32FC1);
// 构建训练数据矩阵 X_train
Mat xtrain_0 = imread("data\\0\\1.jpg");
getFeature(X_train, xtrain_0, 0);
xtrain_0 = imread("data\\0\\2.jpg");
getFeature(X_train, xtrain_0, 1);
xtrain_0 = imread("data\\0\\3.jpg");
getFeature(X_train, xtrain_0, 2);
xtrain_0 = imread("data\\0\\4.jpg");
getFeature(X_train, xtrain_0, 3);
xtrain_0 = imread("data\\0\\5.jpg");
getFeature(X_train, xtrain_0, 4);
Mat xtrain_1 = imread("data\\1\\1.png");
getFeature(X_train, xtrain_1, 5);
xtrain_1 = imread("data\\1\\2.png");
getFeature(X_train, xtrain_1, 6);
xtrain_1 = imread("data\\1\\3.png");
getFeature(X_train, xtrain_1, 7);
xtrain_1 = imread("data\\1\\4.png");
getFeature(X_train, xtrain_1, 8);
xtrain_1 = imread("data\\1\\5.png");
getFeature(X_train, xtrain_1, 9);
//string strPath = "data\\0\\";
//for (int i = 0; i < 21; i++)
//{
// string strPicPath = strPath + to_string(i) + ".jpg";
// Mat mat = imread(strPicPath);
// getFeature(X_train, mat,i);
//}
// 构建训练标签矩阵 Y_train
for (int i = 0; i < SAMPLES; i++)
{
Y_train.at<float>(i, 0) = 1;
Y_train.at<float>(i, 1) = 0;
if (i > SAMPLES / 2 || i == SAMPLES / 2)
{
Y_train.at<float>(i, 0) = 0;
Y_train.at<float>(i, 1) = 1;
}
}
CvANN_MLP_TrainParams params(
cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 300, 0.000001),
CvANN_MLP_TrainParams::BACKPROP,
0.1,
0.1);
int n_W = MAT_WEIGHT * MAT_HEIGHT;
Mat layers = (Mat_<int>(3, 1) << n_W, 24, 2);
CvANN_MLP net(layers, CvANN_MLP::SIGMOID_SYM, 0.6667f, 1.7159f);
net.train(X_train, Y_train, Mat(), Mat(), params);
net.save("net.xml");
int numTest = 3;
Mat X_test(numTest, in_layer, CV_32FC1);
Mat Y_test(numTest, out_layer, CV_32FC1);
Mat testImg0 = imread("data\\0\\6.jpg");
cvtColor(testImg0, testImg0, CV_BGR2GRAY);
Mat testImg1 = imread("data\\1\\6.png");
cvtColor(testImg1, testImg1, CV_BGR2GRAY);
Mat testImg2 = imread("data\\1\\7.png");
cvtColor(testImg2, testImg2, CV_BGR2GRAY);
for (size_t i = 0; i < MAT_HEIGHT; i++)
{
for (size_t j = 0; j < MAT_WEIGHT; j++)
{
X_test.at<float>(0, i*MAT_WEIGHT + j) = (float)testImg0.at<uchar>(i, j) / 255;
X_test.at<float>(1, i*MAT_WEIGHT + j) = (float)testImg1.at<uchar>(i, j) / 255;
X_test.at<float>(2, i*MAT_WEIGHT + j) = (float)testImg2.at<uchar>(i, j) / 255;
}
}
Mat predictions(Y_test.size(), CV_32F);
net.predict(X_test, predictions);
cout << predictions << endl;
return 0;
}
最后得到的分类结果还不错:
上面用的训练样本比较少,每类只有5张图片,下面采用21张图片的:
#include "stdafx.h"
#include <iostream>
//#include <opencv2/core/core.hpp>
//#include <opencv2/ml/ml.hpp>
#include <opencv2/opencv.hpp>
#include <io.h>
using namespace cv;
using namespace std;
#define CLASS_NUM 10
#define MAT_WEIGHT 16
#define MAT_HEIGHT 32
#define uchar unsigned char
void getFeature(Mat &X_train, Mat temp_img, int nIndex)
{
cvtColor(temp_img, temp_img, CV_BGR2GRAY);
//判断图像是否加载成功
if (temp_img.empty())
{
cout << "图像加载失败" << endl;
return ;
}
for (size_t i = 0; i < MAT_HEIGHT; i++)
{
for (size_t j = 0; j < MAT_WEIGHT; j++)
{
X_train.at<float>(nIndex, i*MAT_WEIGHT + j) = (float)temp_img.at<uchar>(i, j)/255;
}
}
}
int main()
{
int in_layer = 512;
int out_layer = 2;
int SAMPLES_0 = 21;
int SAMPLES_2 = 21;
int SAMPLES = SAMPLES_0 + SAMPLES_2; // 训练样本总量
Mat X_train(SAMPLES, in_layer, CV_32FC1);
Mat Y_train(SAMPLES, out_layer, CV_32FC1);
// 构建训练数据矩阵 X_train
string strPath = "data\\0\\";
for (int i = 0; i < 21; i++)
{
string strPicPath = strPath + to_string(i+1) + ".jpg";
Mat mat = imread(strPicPath);
getFeature(X_train, mat,i);
}
strPath = "data\\1\\";
for (int i = 0; i < SAMPLES_2; i++)
{
string strPicPath = strPath + to_string(i + 1) + ".png";
Mat mat = imread(strPicPath);
getFeature(X_train, mat, i + SAMPLES_0);
}
// 构建训练标签矩阵 Y_train
for (int i = 0; i < SAMPLES; i++)
{
if (i < SAMPLES_0)
{
Y_train.at<float>(i, 0) = 1;
Y_train.at<float>(i, 1) = 0;
}
if (i > SAMPLES_0 || SAMPLES_0 == i)
{
Y_train.at<float>(i, 0) = 0;
Y_train.at<float>(i, 1) = 1;
}
}
CvANN_MLP_TrainParams params(
cvTermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 300, 0.000001),
CvANN_MLP_TrainParams::BACKPROP,
0.1,
0.1);
int n_W = MAT_WEIGHT * MAT_HEIGHT;
Mat layers = (Mat_<int>(3, 1) << n_W, 24, 2);
CvANN_MLP net(layers, CvANN_MLP::SIGMOID_SYM, 0.6667f, 1.7159f);
net.train(X_train, Y_train, Mat(), Mat(), params);
net.save("net.xml");
int numTest = 3;
Mat X_test(numTest, in_layer, CV_32FC1);
Mat Y_test(numTest, out_layer, CV_32FC1);
Mat testImg0 = imread("data\\0\\6.jpg");
cvtColor(testImg0, testImg0, CV_BGR2GRAY);
Mat testImg1 = imread("data\\1\\6.png");
cvtColor(testImg1, testImg1, CV_BGR2GRAY);
Mat testImg2 = imread("data\\1\\7.png");
cvtColor(testImg2, testImg2, CV_BGR2GRAY);
for (size_t i = 0; i < MAT_HEIGHT; i++)
{
for (size_t j = 0; j < MAT_WEIGHT; j++)
{
X_test.at<float>(0, i*MAT_WEIGHT + j) = (float)testImg0.at<uchar>(i, j) / 255;
X_test.at<float>(1, i*MAT_WEIGHT + j) = (float)testImg1.at<uchar>(i, j) / 255;
X_test.at<float>(2, i*MAT_WEIGHT + j) = (float)testImg2.at<uchar>(i, j) / 255;
}
}
Mat predictions(Y_test.size(), CV_32F);
net.predict(X_test, predictions);
cout << predictions << endl;
return 0;
}