This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
sh_oh.h
285 lines (259 loc) · 8.69 KB
/
sh_oh.h
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
280
281
282
283
284
285
#ifndef SH_OH_H
#define SH_OH_H
// other handle
#include "prefix.h"
#include "sh_zm.h"
#include "sh_dk.h"
#include "sh_rl.h"
#include "sh_tn.h"
#include "sh_fx.h"
// mean smooth filter
inline image_type sh_oh_mean_smooth(image_type const& old_img, int tn = 3)
{
if (old_img.is_empty()) return old_img;
image_type new_img(old_img);
for (int px = 0; px < (int)old_img.width() - tn; ++px)
{
for (int py = 0; py < (int)old_img.height() - tn; ++py)
{
int avg = 0;
for (int k = 0; k < tn * tn; ++k)
avg += old_img.at(px + k % tn, py + k / tn).gray();
avg /= tn * tn;
new_img.at(px + (tn >> 1), py + (tn >> 1)).gray(avg);
}
}
return new_img;
}
// mean smooth filter for binary image
inline image_type sh_oh_binary_mean_smooth(image_type const& old_img, int tn = 2)
{
if (old_img.is_empty()) return old_img;
int w = (int)old_img.width();
int h = (int)old_img.height();
int n = 0;
int px, py;
for (px = 0; px < w; ++px)
for (py = 0; py < h; ++py)
n += old_img.at(px, py).is_black()? 1 : 0;
// not smooth it if image is too rough
if ((double(n) / double(w * h)) > 0.6) return old_img;
image_type new_img(old_img);
for (px = 0; px < w - tn; ++px)
{
for (py = 0; py < h - tn; ++py)
{
int avg = 0;
for (int k = 0; k < tn * tn; ++k)
avg += old_img.at(px + k % tn, py + k / tn).gray();
avg /= tn * tn;
new_img.at(px + (tn >> 1), py + (tn >> 1)).is_black(avg < 128);
}
}
return new_img;
}
// trim margin
inline image_type sh_oh_trim(image_type const& old_img)
{
basic_rect<int> r = sh_dk_digital_region(old_img);
// save trimed image
image_type new_img(r.width(), r.height());
for (int py = r.top(); py <= r.bottom(); ++py)
for (int px = r.left(); px <= r.right(); ++px)
new_img.at(px - r.left(), py - r.top()).gray(old_img.at(px, py).gray());
return new_img;
}
inline void sh_oh_trim_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row)
{
// normalize
for (int i = 0; i < (int)pimgs_info_row.size(); ++i)
for (int j = 0; j < (int)pimgs_info_row[i].size(); ++j)
pimgs_info_row[i][j]->image = sh_oh_trim(pimgs_info_row[i][j]->image);
}
// fill margin by factor = w / h
inline image_type sh_oh_fill_margin(image_type const& old_img, double factor)
{
int w = old_img.width();
int h = old_img.height();
double f = double(w) / double(h);
// fill
if (f > factor) // fill height
{
int need_h = int(w / factor);
int margin_h = (need_h - h) >> 1;
image_type new_img(w, need_h);
for (int py = 0; py < new_img.height(); ++py)
{
for (int px = 0; px < new_img.width(); ++px)
{
if (py < margin_h || py >= new_img.height() - margin_h)
new_img.at(px, py).is_black(false);
else new_img.at(px, py).is_black(old_img.at(px, py - margin_h).is_black());
}
}
return new_img;
}
else if (f < factor)// fill width
{
int need_w = int(h * factor);
int margin_w = (need_w - w) >> 1;
image_type new_img(need_w, h);
for (int py = 0; py < new_img.height(); ++py)
{
for (int px = 0; px < new_img.width(); ++px)
{
if (px < margin_w || px >= new_img.width() - margin_w)
new_img.at(px, py).is_black(false);
else new_img.at(px, py).is_black(old_img.at(px - margin_w, py).is_black());
}
}
return new_img;
}
return old_img;
}
// fill margin for 1 by factor = w / h
inline image_type sh_oh_fill_margin_for_1(image_type const& old_img, double factor)
{
int w = old_img.width();
int h = old_img.height();
double f = double(w) / double(h);
if (f < factor) // fill width
{
int need_w = int(h * factor);
int margin_lw = (need_w - w) >> 1;
int margin_rw = (need_w - w) - margin_lw;
int px, py;
image_type new_img(need_w, h);
for (py = 0; py < new_img.height(); ++py)
{
for (px = 0; px < new_img.width(); ++px)
{
if (px < margin_lw || px >= new_img.width() - margin_rw)
new_img.at(px, py).is_black(false);
else new_img.at(px, py).is_black(old_img.at(px - margin_lw, py).is_black());
}
}
return new_img;
}
return old_img;
}
inline void sh_oh_fill_margin_for_1_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row, int w = IMAGE_W, int h = IMAGE_H)
{
// fill margin for 1
for (int i = 0; i < (int)pimgs_info_row.size(); ++i)
for (int j = 0; j < (int)pimgs_info_row[i].size(); ++j)
pimgs_info_row[i][j]->image = sh_oh_fill_margin_for_1(pimgs_info_row[i][j]->image, double(w) / double(h));
}
inline void sh_oh_fill_margin_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row, int w = IMAGE_W, int h = IMAGE_H)
{
// fill margin
for (int i = 0; i < (int)pimgs_info_row.size(); ++i)
for (int j = 0; j < (int)pimgs_info_row[i].size(); ++j)
pimgs_info_row[i][j]->image = sh_oh_fill_margin(pimgs_info_row[i][j]->image, double(w) / double(h));
}
inline void sh_oh_normalize_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row, int w = IMAGE_W, int h = IMAGE_H)
{
// normalize
for (int i = 0; i < (int)pimgs_info_row.size(); ++i)
for (int j = 0; j < (int)pimgs_info_row[i].size(); ++j)
pimgs_info_row[i][j]->image = sh_zm_bilinear_zoom(pimgs_info_row[i][j]->image, w, h, true);
}
inline void sh_oh_normalize2_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row, int w = IMAGE_W, int h = IMAGE_H)
{
// normalize
for (int i = 0; i < (int)pimgs_info_row.size(); ++i)
for (int j = 0; j < (int)pimgs_info_row[i].size(); ++j)
{
image_type& pimg = pimgs_info_row[i][j]->image;
if (pimg.width() < 16 || pimg.height() < 32)
{
pimg = sh_zm_nearest_neighbor_zoom(pimg, 16, 32);
//pimg = sh_fx_roughening(pimg);
pimg = sh_oh_binary_mean_smooth(pimg);
pimg = sh_zm_nearest_neighbor_zoom(pimg, w, h);
}
else pimg = sh_zm_nearest_neighbor_zoom(pimg, w, h);
}
}
inline void sh_oh_binary_smooth_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row)
{
// binary smooth
for (int i = 0; i < (int)pimgs_info_row.size(); ++i)
for (int j = 0; j < (int)pimgs_info_row[i].size(); ++j)
//pimgs_info_row[i][j]->image = sh_oh_mean_smooth(pimgs_info_row[i][j]->image, 2, true);
pimgs_info_row[i][j]->image = sh_oh_binary_mean_smooth(pimgs_info_row[i][j]->image);
}
inline void sh_oh_fixes_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row)
{
// deskew
for (int i = 0; i < (int)pimgs_info_row.size(); ++i)
for (int j = 0; j < (int)pimgs_info_row[i].size(); ++j)
pimgs_info_row[i][j]->image = sh_fx_fixes(pimgs_info_row[i][j]->image);
}
inline void sh_oh_deskew_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row)
{
// deskew
for (int i = 0; i < (int)pimgs_info_row.size(); ++i)
for (int j = 0; j < (int)pimgs_info_row[i].size(); ++j)
pimgs_info_row[i][j]->image = sh_dk_horizontal_side_slip_deskew(pimgs_info_row[i][j]->image);
}
inline void sh_oh_thinning_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row)
{
// thinning
for (int i = 0; i < (int)pimgs_info_row.size(); ++i)
for (int j = 0; j < (int)pimgs_info_row[i].size(); ++j)
pimgs_info_row[i][j]->image = sh_tn_thinning(pimgs_info_row[i][j]->image);
}
inline image_type sh_oh_merge_from_pimgs_info_row(pimages_info_row_type const& pimgs_info_row, int margin = 20)
{
int i, j;
int px, py;
// get width & height
int max_w = 0, max_h = 0;
for (i = 0; i < (int)pimgs_info_row.size(); ++i)
{
int w = 0;
int h = 0;
for (j = 0; j < (int)pimgs_info_row[i].size(); ++j)
{
w += pimgs_info_row[i][j]->image.width();
w += margin;
h = pimgs_info_row[i][j]->image.height() > h? pimgs_info_row[i][j]->image.height() : h;
}
max_w = w > max_w? w : max_w;
max_h += h + margin;
}
if (max_w == 0 || max_h == 0) return image_type();
// clear new image
image_type new_img(max_w, max_h);
for (px = 0; px < (int)new_img.width(); ++px)
for (py = 0; py < (int)new_img.height(); ++py)
new_img.at(px, py).is_black(false);
int base_y = 0;
for (i = 0; i < (int)pimgs_info_row.size(); ++i)
{
int h = 0;
int base_x = 0;
for (j = 0; j < (int)pimgs_info_row[i].size(); ++j)
{
image_type& img = pimgs_info_row[i][j]->image;
for (px = 0; px < (int)img.width(); ++px)
{
for (py = 0; py < (int)img.height(); ++py)
{
if (img.at(px, py).is_black())
new_img.at(base_x + px, base_y + py) = img.at(px, py);
//else new_img.at(base_x + px, base_y + py).gray(200);
//if (py == 0 || px == 0 || px == (int)img.width() - 1 || py == (int)img.height() - 1)
// new_img.at(base_x + px, base_y + py).gray(200);
}
}
base_x += img.width();
base_x += margin;
h = img.height() > h? img.height() : h;
}
base_y += h + margin;
}
return new_img;
}
#endif // SH_OH_H