-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFeatureExtractor.cpp
More file actions
279 lines (253 loc) · 6.47 KB
/
FeatureExtractor.cpp
File metadata and controls
279 lines (253 loc) · 6.47 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "FeatureExtractor.h"
#include<iostream>
#include<fstream>
using namespace visint_ocr;
/*
* 参数:需要用通过实验找到最优参数
* theta:四个方向是确定的
* lambda:波长(尺度)支持1-3个尺度【根据实验做具体设置】
* psi:0
* gamma: 0.5 usually
*/
void FeatureExtractor::Init()
{
//应该是读配置文件
param.kenelsize = 11;
param.theta[0] = 0.0, param.theta[1] = CV_PI / 4, param.theta[2] = CV_PI / 2, param.theta[3] = CV_PI*3.0 / 4.0;
param.sigma = CV_PI/3;
param.waves[0] = 2 * CV_PI / 3, param.waves[1] = 2.5* CV_PI / 3, param.waves[2] = 3 * CV_PI / 3;
//param.sigma = CV_PI;
//param.waves[0] = 2 * CV_PI / 3, param.waves[1] = 2.5* CV_PI / 6, param.waves[2] = 3 * CV_PI / 12;
param.gamma = 0.5;
param.psi = 0;
this->thetanum = 4;
this->wavenum = 1;
this->level = 3;
}
FeatureExtractor::FeatureExtractor()
{
Init();
BuildKernelFamily();
}
FeatureExtractor::FeatureExtractor(int thetanum, int wavenum)
{
this->Init();
this->thetanum = thetanum;
this->wavenum = wavenum;
BuildKernelFamily();
}
FeatureExtractor::FeatureExtractor(const GaborParam& param, int thetanum, int wavenum)
{
this->param.kenelsize = param.kenelsize;
this->param.gamma = param.gamma;
this->param.psi = param.psi;
this->thetanum = thetanum;
this->wavenum = wavenum;
for (int i = 0; i < wavenum; i++)
{
this->param.waves[i] = param.waves[i];
}
for (int i = 0; i < thetanum; i++)
{
this->param.theta[i] = param.theta[i];
}
this->level = 3;
BuildKernelFamily();
}
FeatureExtractor::~FeatureExtractor()
{
}
void FeatureExtractor::BuildKernelFamily()
{
kernels.clear();
for (int i = 0; i < wavenum; i++)
{
for (int j = 0; j < thetanum; j++)
{
cv::Mat kernel = cv::getGaborKernel(cv::Size(param.kenelsize, param.kenelsize), param.sigma, param.theta[j], param.waves[i], param.gamma,param.psi);//, param.psi
this->kernels.push_back(kernel);
}
}
}
void FeatureExtractor::Pyramid(const cv::Mat& dst, int level, std::vector<float>& features)
{
int H = dst.rows / (int)pow(2, level);
int W = dst.cols / (int)pow(2, level);
for (int i = H; i <= dst.rows; i += H)
{
for (int j = W; j <= dst.cols; j += W)
{
cv::Mat tempdst = dst.rowRange(i - H, i).colRange(j - W, j);
cv::Scalar meanv, stdDev;
cv::meanStdDev(tempdst, meanv, stdDev);
features.push_back(meanv[0]);
features.push_back(stdDev[0]);
}
}
}
void FeatureExtractor::setPyramidLevel(int level)
{
if (level >= 3||level<=0)
this->level = 3;
else
this->level = level;
}
void FeatureExtractor::Normalize(std::vector<float>& features)
{
float meanmin = features[0], meanmax = features[0], stdmin = features[1], stdmax = features[1];
for (size_t i = 0; i < features.size(); i += 2)
{
//mean
meanmin = std::min(meanmin, features[i]);
meanmax = std::max(meanmax, features[i]);
stdmin = std::min(stdmin, features[i + 1]);
stdmax = std::max(stdmax, features[i + 1]);
}
for (size_t i = 0; i < features.size(); i += 2)
{
features[i] = (features[i] - meanmin) / (meanmax - meanmin + 0.00001);
features[i + 1] = (features[i + 1] - stdmin) / (stdmax - stdmin + 0.00001);
}
}
void FeatureExtractor::Extract(const cv::Mat& img, std::vector<float>& features)
{
//features.clear();
cv::Mat inv = img;// 浅copy
if (inv.channels() == 3)
{
cvtColor(inv, inv, cv::COLOR_BGR2GRAY);
}
int height = 16;
int width = (int)(height*1.0*inv.rows / inv.cols);
cv::resize(inv, inv, cv::Size(height, width));
inv = 255 - inv;
cv::Mat text_f;
inv.convertTo(text_f, CV_32F, 1.0 / 255.0, 0);
for (size_t i = 0; i < kernels.size(); i++)
{
cv::Mat dst;
cv::filter2D(text_f, dst, -1, kernels[i]);
//第 0 层金字塔
//Pyramid(dst, 0, features);
//第 1 层金字塔
//Pyramid(dst, 1, features);
//第 2 层金字塔
//Pyramid(dst, 2, features);
for (int i = 0; i < this->level; i++)
{
Pyramid(dst, i, features);
}
}
//归一化操作
Normalize(features);
}
void visint_ocr::ExtractGaborFeatureWithLabel(std::string basepath, std::string imagespath, std::string featurespath)
{
std::vector<float>feature;
FeatureExtractor fex(4,3);
fex.setPyramidLevel(3);
std::ifstream input(imagespath);
std::ofstream output(featurespath,std::ios::out);
if (input.fail() || output.fail()) //
{
std::cout << "paths error";
system("pause");
return;
}
//读取imagespath 中图片的路径 、 label,并把抽取的特征依次保存在featuresMat
//while(1)
//std::string base = "F:\\WYL\\AllFonts\\";
//int index = 1;
while (1)
{
feature.clear();
int label = 0;
std::string imagepath;
input >> imagepath >> label;
if (input.eof()) break;
output << label ;
cv::Mat image = cv::imread(basepath+imagepath, 0);
fex.Extract(image, feature);
for (size_t i = 0; i < feature.size(); i++)
{
output << " "<<(i+1)<<":" << feature[i];
}
output << std::endl;
std::cout << ".";
//std::cout << index++<<std::endl;
// write featurespath
}
output.close();
input.close();
}
void visint_ocr::ExtractGaborFeatureWithLabel(std::string basepath,std ::string imagespath, cv::Mat& featuresMat)
{
std::vector<float>feature;
FeatureExtractor fex(4,3);
fex.setPyramidLevel(2);
//读取imagespath 中图片的路径 、 label,并把抽取的特征依次保存在featuresMat
//while(1)
std::ifstream input(imagespath);
if (input.fail())
{
std::cout << "error";
return;
}
while (1)
{
feature.clear();
int label = 0;;
std::string imagepath;
input >> imagepath >> label;
if (input.eof())
{
break;
}
cv::Mat image = cv::imread(basepath + imagepath, 0);
fex.Extract(image, feature);
feature.insert(feature.begin(), label);
cv::Mat featuremat(feature);
featuremat = featuremat.t();
featuresMat.push_back(featuremat);
//featuresMat.
}
}
void visint_ocr::ExtractGaborFeature(const cv::Mat& image, cv::Mat& featureMat)
{
std::vector<float>feature;
FeatureExtractor fex(4,3);
fex.setPyramidLevel(2);
fex.Extract(image, feature);
cv::Mat featuremat(feature);
featuremat = featuremat.t();
featureMat.push_back(featuremat);
}
void visint_ocr::ExtractGaborFeature(std::string basepath, std::string imagespath, cv::Mat& featuresMat)
{
std::vector<float>feature;
FeatureExtractor fex;
fex.setPyramidLevel(2);
//读取imagespath 中图片的路径 、 label,并把抽取的特征依次保存在featuresMat
//while(1)
std::ifstream input(imagespath);
if (input.fail())
{
std::cout << "error";
return;
}
while (1)
{
feature.clear();
std::string imagepath;
input >> imagepath;
if (input.eof())
{
break;
}
cv::Mat image = cv::imread(imagepath, 0);
fex.Extract(image, feature);
cv::Mat featuremat(feature);
featuremat = featuremat.t();
featuresMat.push_back(featuremat);
}
}