forked from viccwq/ANN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchar_ann.cpp
More file actions
224 lines (195 loc) · 4.49 KB
/
char_ann.cpp
File metadata and controls
224 lines (195 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "comm_def_col.h"
#define HORIZONTAL 1
#define VERTICAL 0
const string g_path = "./training_05/";
const string g_exten = "*.bmp";
static Mat ProjectedHistogram(Mat img, int t)
{
int sz=(t)?img.rows:img.cols;
Mat mhist=Mat::zeros(1,sz,CV_32F);
for(int j=0; j<sz; j++){
Mat data=(t)?img.row(j):img.col(j);
mhist.at<float>(j)= countNonZero(data);
}
//Normalize histogram
double min, max;
minMaxLoc(mhist, &min, &max);
if(max>0)
mhist.convertTo(mhist,-1 , 1.0f/max, 0);
return mhist;
}
static Mat features(Mat in, int sizeData){
//Histogram features
Mat vhist=ProjectedHistogram(in,VERTICAL);
Mat hhist=ProjectedHistogram(in,HORIZONTAL);
//Low data feature
Mat lowData;
resize(in, lowData, Size(sizeData, sizeData) );
//Last 10 is the number of moments components
int numCols=vhist.cols+hhist.cols+lowData.cols*lowData.cols;
Mat out=Mat::zeros(1,numCols,CV_32F);
//Asign values to feature
int j=0;
for(int i=0; i<vhist.cols; i++)
{
out.at<float>(j)=vhist.at<float>(i);
j++;
}
for(int i=0; i<hhist.cols; i++)
{
out.at<float>(j)=hhist.at<float>(i);
j++;
}
for(int x=0; x<lowData.cols; x++)
{
for(int y=0; y<lowData.rows; y++){
out.at<float>(j)=(float)lowData.at<unsigned char>(x,y);
j++;
}
}
return out;
}
static int save_xml()
{
cv::Directory dir;
FileStorage fs_train("cny_chars_train.xml", FileStorage::WRITE);
FileStorage fs_test("cny_chars_test.xml", FileStorage::WRITE);
Mat train;
Mat label_1;
Mat test;
Mat label_2;
for (int k = 0; k < 10; k++)
{
char buf[8] = {0};
sprintf(buf, "%02d", k);
string sub_path = buf;
vector<string> file_name = dir.GetListFiles(g_path + sub_path, g_exten);
for (int i = 0; i < 40 && i < file_name.size(); i++)
{
cout<<file_name[i]<<endl;
Mat image = imread(file_name[i], IMREAD_GRAYSCALE);
Mat feat = features(image, 5);
train.push_back(feat);
label_1.push_back(k);
}
for (int i = 40; i < 50 && i < file_name.size(); i++)
{
cout<<file_name[i]<<endl;
Mat image = imread(file_name[i], IMREAD_GRAYSCALE);
Mat feat = features(image, 5);
test.push_back(feat);
label_2.push_back(k);
}
}
train.convertTo(train, CV_32FC1);
test.convertTo(test, CV_32FC1);
fs_train << "train" << train;
fs_train << "label" << label_1;
fs_test << "test" << test;
fs_test << "label" << label_2;
fs_train.release();
fs_test.release();
return 0;
}
class OCR
{
public:
OCR();
OCR(string train_file);
bool trained;
int classify(Mat f);
void train(Mat train, Mat label, int nlayers);
void test();
protected:
private:
CvANN_MLP ann;
};
int OCR::classify(Mat f)
{
int result=-1;
Mat output(1, 10, CV_32FC1);
ann.predict(f, output);
Point maxLoc;
double maxVal;
minMaxLoc(output, 0, &maxVal, 0, &maxLoc);
//We need know where in output is the max val, the x (cols) is the class.
return maxLoc.x;
}
void OCR::train(Mat TrainData, Mat label, int nlayers)
{
Mat layers(1,3,CV_32SC1);
layers.at<int>(0)= TrainData.cols;
layers.at<int>(1)= nlayers;
layers.at<int>(2)= 10;
ann.create(layers, CvANN_MLP::SIGMOID_SYM, 1, 1);
//Prepare trainClases
//Create a mat with n trained data by m classes
Mat trainClasses;
trainClasses.create( TrainData.rows, 10, CV_32FC1 );
for( int i = 0; i < trainClasses.rows; i++ )
{
for( int k = 0; k < trainClasses.cols; k++ )
{
//If class of data i is same than a k class
if( k == label.at<int>(i) )
trainClasses.at<float>(i,k) = 1;
else
trainClasses.at<float>(i,k) = 0;
}
}
Mat weights( 1, TrainData.rows, CV_32FC1, Scalar::all(1) );
//Learn classifier
ann.train( TrainData, trainClasses, weights );
trained=true;
}
OCR::OCR()
{
trained=false;
}
OCR::OCR(string train_file)
{
trained=false;
//Read file storage.
FileStorage fs;
fs.open("cny_chars_train.xml", FileStorage::READ);
Mat TrainingData;
Mat Classes;
fs["train"] >> TrainingData;
fs["label"] >> Classes;
train(TrainingData, Classes, 30);
}
void OCR::test()
{
if (trained == false)
return;
//Read file storage.
FileStorage fs;
fs.open("cny_chars_test.xml", FileStorage::READ);
Mat TestData;
Mat Classes;
fs["test"] >> TestData;
fs["label"] >> Classes;
for (int i = 0; i < TestData.rows; i++)
{
Mat data = TestData.row(i);
int result = classify(data);
bool ret = (Classes.at<int>(i) == result);
cout<<"result is:"<<result<<"("<<(ret?"TURN":"FALSE")<<")"<<endl;
}
}
int char_ann()
{
char input[32];
cout<<"input the command[g|r]:";
cin>>input;
if ('g' == input[0])
save_xml();
else if ('r' == input[0])
{
OCR ocr("aaa");
ocr.test();
}
else
;
return 0;
}