/////*-----------------------------------------------------------------------------
//// * Project.....................: Visual Computing at MICA
//// * Copyright...................: (C) 2009 MICA. All rights reserved.
//// * This project developed by...: MICA, HUT, Viet Nam http://mica.edu.vn
//// * File........................:
//// * Authors.....................: Tran Thi Thanh Hai
//// * Description.................: Skin Detection Under Changing illumination using Neural Network
//// * Creation....................: 22.01.2010
//// * Revision....................:
//// -------------------------------------------------------------------------------*/
////
////standard includes
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include "ml.h"
#include <stdio.h>
// This function reads data and responses from the file <filename>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <fstream>
//open CV includes
#include <ml.h>
using namespace std;
ofstream outPre ("OutPre.txt");
CvANN_MLP machineBrain;
static int build_mlp_classifier()
{
int var_count;
CvMat* datas;
CvMat* responses;
// ofstream outFile ("OutTrain.txt");
// if (outFile.is_open())
//{
// ifstream inFile;
// inFile.open("Train.txt");
//
// if (!inFile) {
// cerr << "Unable to open file Train.txt";
// exit(1); // call system to stop
// }
//
// inFile >> var_count;
// cout << "number of data pairs = " << var_count << endl;
// cout << "Processing...";
// outFile << "number of data pairs = " << var_count << "\n";
// datas = cvCreateMat(var_count, 3, CV_32FC1);
// responses = cvCreateMat(var_count, 3, CV_32FC1);
//
// int id = 0;
// while (!inFile.eof())
// {
// for (int i = 0; i < 3; i++)
// {
// int x ;
// inFile >> x;
// //cout << x << " " ;
// //outFile << x << " ";
// cvmSet(datas,id, i, x);
// }
// for (int i = 0; i < 3; i++)
// {
// int x ;
// inFile >> x;
// //cout << x << " " ;
// //outFile << x << " ";
// cvmSet(responses,id, i, x);
// }
// //cout << endl;
// //outFile << "\n";
// id++;
// }
// inFile.close();
// }
//
ifstream inFile;
inFile.open("Train.txt");
if (!inFile) {
cerr << "Unable to open file Train.txt";
exit(1); // call system to stop
}
inFile >> var_count;
cout << "number of data pairs = " << var_count << endl;
cout << "Processing...";
datas = cvCreateMat(var_count, 3, CV_32FC1);
responses = cvCreateMat(var_count, 3, CV_32FC1);
int id = 0;
while (!inFile.eof())
{
for (int i = 0; i < 3; i++)
{
int x ;
inFile >> x;
//cout << x;
cvmSet(datas,id, i, x);
}
for (int i = 0; i < 3; i++)
{
int x ;
inFile >> x;
cvmSet(responses,id, i, x);
}
id++;
}
inFile.close();
//The weight of each training data sample. We'll later set all to equal weights.
CvMat* sampleWts = cvCreateMat(var_count, 1, CV_32FC1);
//The matrix representation of our ANN. We'll have four layers.
CvMat* neuralLayers = cvCreateMat(3, 1, CV_32SC1);
CvMat neuralLayers1, sampleWts1;
cvGetRows(sampleWts, &sampleWts1, 0, var_count);
cvGetRows(neuralLayers, &neuralLayers1, 0, 3); // 0,3
//Setting the number of neurons on each layer of the ANN
/*
We have in Layer 1: 3 neurons (3 inputs)
Layer 2: 20 neurons (hidden layer)
Layer 3: 3 neurons (3 output)
*/
cvSet1D(&neuralLayers1, 0, cvScalar(3));
cvSet1D(&neuralLayers1, 1, cvScalar(20));
cvSet1D(&neuralLayers1, 2, cvScalar(3));
for (int i=0; i<var_count; i++)
{
cvSet1D(&sampleWts1, i, cvScalar(1));
}
//Create our ANN.
machineBrain.create(neuralLayers);
//Train it with our data.
//See the Machine learning reference at http://www.seas.upenn.edu/~bensapp/opencvdocs/ref/opencvref_ml.htm#ch_ann
int iters = machineBrain.train(
datas,
responses,
sampleWts,
0,
CvANN_MLP_TrainParams(
cvTermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 100000, 1.0 ),//
CvANN_MLP_TrainParams::BACKPROP,
0.01,
0.05
)
);
/*outFile << "number of trainning iterations = " << iters << "\n";
outFile << "end of trainning" << "\n";
outFile.close();*/
return iters;
return 0;
}
// Predict the output with the trained ANN given the three inputs.
void Predict(float data1, float data2, float data3)
{
float _sample[3];
CvMat sample = cvMat(1, 3, CV_32FC1, _sample);
float _predout[3];
CvMat predout = cvMat(1, 3, CV_32FC1, _predout);
sample.data.fl[0] = data1;
sample.data.fl[1] = data2;
sample.data.fl[2] = data3;
machineBrain.predict(&sample, &predout);
//cout<<min((int)predout.data.fl[0],255)<<" "<<min((int)predout.data.fl[1],255)<<" "<<min((int)predout.data.fl[2],255)<<endl;
outPre<<min((int)predout.data.fl[0],255)<<" "<<min((int)predout.data.fl[1],255)<<" "<<min((int)predout.data.fl[2],255)<<" ";
}
int main( int argc, char *argv[] )
{
int iters = build_mlp_classifier();
//for (int i=0; i<5; i++) build_mlp_classifier();
//cout << "number of trainning iterations = " << iters << endl;
//cout << "end of trainning, please enter a key to exit" << endl;
//cout << endl;
ifstream inFile;
inFile.open("TestImage.txt");
if (outPre.is_open())
{
while (!inFile.eof())
{
float data1, data2, data3 ;
inFile >> data1;
inFile >> data2;
inFile >> data3;
//cout <<(int)data1<<" "<<(int)data2<<" "<<(int) data3<< endl;
//if ((data1 + data2 + data3)/3 > 10)
Predict(data1,data2,data3);
}
inFile.close();
}
outPre.close();
cout << "Complete!";
int wait;
scanf("%d",&wait);
return 0;
}
ANN.zip_ANN opencv _ANN_MLP_MLP_opencv ann_opencv mlp
版权申诉
138 浏览量
2022-09-24
02:05:14
上传
评论
收藏 2KB ZIP 举报
JaniceLu
- 粉丝: 78
- 资源: 1万+
最新资源
- pta题库答案c语言之树结构2ListLeaves.zip
- pta题库答案c语言之树结构1树的同构.zip
- 基于C++实现民航飞行与地图简易管理系统可执行程序+说明+详细注释.zip
- pta题库答案c语言之复杂度1最大子列和问题.zip
- 三维装箱问题(Three-Dimensional Bin Packing Problem,3D-BPP)是一个经典的组合优化问题
- 以下是一些关于Linux线程同步的基本概念和方法.txt
- 以下是一个简化的示例,它使用pygame库来模拟烟花动画的框架.txt
- Linux线程同步机制深度解析与实用指南.zip
- PTA题库C语言解题策略与实战.rar
- SVPWM控制技术的simulink建模与仿真【包括simulink模型,参考文献,操作步骤】
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈