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_dk.h
290 lines (257 loc) · 8.13 KB
/
sh_dk.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
286
287
288
289
290
#ifndef SH_DK_H
#define SH_DK_H
#include "prefix.h"
// gets the digital region in the image
inline basic_rect<int> sh_dk_digital_region(image_type const& old_img, int margin = 1)
{
basic_rect<int> region;
// stats the number of pixels at every rows
int py, px;
scoped_buffer<int> hstats(old_img.height());
for (py = 0; py < old_img.height(); ++py)
{
hstats[py] = 0;
for (px = 0; px < old_img.width(); ++px)
if (old_img.at(px, py).is_black()) ++hstats[py];
}
// split by height
for (py = 0; py < old_img.height(); ++py)
{
if (hstats[py] > 0)
{
region.top(py);
break;
}
}
if (py == old_img.height()) region.top(old_img.height() - 1);
if (region.top() - margin >= 0) region.top(region.top() - margin);
for (py = old_img.height(); py > 0; --py)
{
if (hstats[py - 1] > 0)
{
region.bottom(py - 1);
break;
}
}
if (py == 0) region.bottom(0);
if (region.bottom() + margin < old_img.height()) region.bottom(region.bottom() + margin);
EXTL_ASSERT(region.bottom() > region.top());
// stats the number of pixels at every columns
scoped_buffer<int> wstats(old_img.width());
for (px = 0; px < old_img.width(); ++px)
{
wstats[px] = 0;
for (py = 0; py < old_img.height(); ++py)
if (old_img.at(px, py).is_black()) ++wstats[px];
}
// split by width
for (px = 0; px < old_img.width(); ++px)
{
if (wstats[px] > 0)
{
region.left(px);
break;
}
}
if (px == old_img.width()) region.left(old_img.width() - 1);
if (region.left() - margin >= 0) region.left(region.left() - margin);
for (px = old_img.width(); px > 0; --px)
{
if (wstats[px - 1] > 0)
{
region.right(px - 1);
break;
}
}
if (px == 0) region.right(0);
if (region.right() + margin < old_img.width()) region.right(region.right() + margin);
EXTL_ASSERT(region.right() > region.left());
return region;
}
// horizontal_side_slip_deskew
/*inline image_type __sh_dk_horizontal_side_slip_deskew(image_type const& old_img)
{
int py, px;
double angle = EXTL_PI / 4;
image_type mid_tmp(old_img.width() + 10, old_img.height() + 10);
for (py = 0; py < mid_tmp.height(); ++py)
{
for (px = 0; px < mid_tmp.width(); ++px)
{
if (py < 5 || px < 5 || py >= mid_tmp.height() - 5 || px >= mid_tmp.width() - 5)
mid_tmp.at(px, py).is_black(false);
else mid_tmp.at(px, py).is_black(old_img.at(px - 5, py - 5).is_black());
}
}
while (angle > EXTL_PI / 180)
{
// calculate the real position of image included pixels
basic_rect<int> mid_pos = sh_dk_digital_region(mid_tmp);
// left transform;
image_type left_tmp(mid_tmp.width(), mid_tmp.height());
for (py = 0; py < left_tmp.height(); ++py)
for (px = 0; px < left_tmp.width(); ++px)
left_tmp.at(px, py).is_black(false);
for (py = 0; py < left_tmp.height(); ++py)
{
for (px = 0; px < left_tmp.width(); ++px)
{
if (mid_tmp.at(px, py).is_black())
{
double slope = tan(EXTL_PI / 2 + angle);
int new_x = xtl_round45(px - (py - ((double)left_tmp.height() / 2.0)) / slope);
if (new_x >= 0 && new_x < left_tmp.width())
left_tmp.at(new_x, py).is_black(true);
}
}
}
// calculate the real position of image included pixels
basic_rect<int> left_pos = sh_dk_digital_region(left_tmp);
// right transform;
image_type right_tmp(mid_tmp.width(), mid_tmp.height());
for (py = 0; py < right_tmp.height(); ++py)
for (px = 0; px < right_tmp.width(); ++px)
right_tmp.at(px, py).is_black(false);
for (py = 0; py < right_tmp.height(); ++py)
{
for (px = 0; px < right_tmp.width(); ++px)
{
if (mid_tmp.at(px, py).is_black())
{
double slope = tan(EXTL_PI / 2 - angle);
int new_x = xtl_round45(px - (py - ((double)right_tmp.height() / 2.0)) / slope);
if (new_x >= 0 && new_x < right_tmp.width())
right_tmp.at(new_x, py).is_black(true);
}
}
}
// calculate the real position of image included pixels
basic_rect<int> right_pos = sh_dk_digital_region(right_tmp);
// select better image
if (right_pos.width() < left_pos.width())
{
if (right_pos.width() < mid_pos.width())
mid_tmp = right_tmp;
}
else
{
if (left_pos.width() < mid_pos.width())
mid_tmp = left_tmp;
}
angle /= 2;
}
// update
basic_rect<int> mid_pos = sh_dk_digital_region(mid_tmp);
image_type new_img(mid_pos.width(), mid_pos.height());
for (py = mid_pos.top(); py <= mid_pos.bottom(); ++py)
for (px = mid_pos.left(); px <= mid_pos.right(); ++px)
new_img.at(px - mid_pos.left(), py - mid_pos.top()).is_black(mid_tmp.at(px, py).is_black());
return new_img;
}*/
// horizontal_side_slip_deskew
inline image_type sh_dk_horizontal_side_slip_deskew(image_type const& old_img)
{
int py, px;
double angle = EXTL_PI / 4;
image_type mid_tmp(old_img.width() + 10, old_img.height() + 10);
for (py = 0; py < mid_tmp.height(); ++py)
{
for (px = 0; px < mid_tmp.width(); ++px)
{
if (py < 5 || px < 5 || py >= mid_tmp.height() - 5 || px >= mid_tmp.width() - 5)
mid_tmp.at(px, py).is_black(false);
else mid_tmp.at(px, py).is_black(old_img.at(px - 5, py - 5).is_black());
}
}
// calculate the real position of image included pixels
basic_rect<int> mid_pos = sh_dk_digital_region(mid_tmp);
double best_angle = 0;
int min_w = mid_pos.width();
while (angle > EXTL_PI / 180)
{
// left transform;
image_type left_tmp(mid_tmp.width(), mid_tmp.height());
for (py = 0; py < left_tmp.height(); ++py)
for (px = 0; px < left_tmp.width(); ++px)
left_tmp.at(px, py).is_black(false);
for (py = 0; py < left_tmp.height(); ++py)
{
for (px = 0; px < left_tmp.width(); ++px)
{
if (mid_tmp.at(px, py).is_black())
{
double slope = tan(EXTL_PI / 2 + best_angle + angle);
int new_x = xtl_round45(px - (py - ((double)left_tmp.height() / 2.0)) / slope);
if (new_x >= 0 && new_x < left_tmp.width())
left_tmp.at(new_x, py).is_black(true);
}
}
}
// calculate the real position of image included pixels
basic_rect<int> left_pos = sh_dk_digital_region(left_tmp);
// right transform
image_type right_tmp(mid_tmp.width(), mid_tmp.height());
for (py = 0; py < right_tmp.height(); ++py)
for (px = 0; px < right_tmp.width(); ++px)
right_tmp.at(px, py).is_black(false);
for (py = 0; py < right_tmp.height(); ++py)
{
for (px = 0; px < right_tmp.width(); ++px)
{
if (mid_tmp.at(px, py).is_black())
{
double slope = tan(EXTL_PI / 2 + best_angle - angle);
int new_x = xtl_round45(px - (py - ((double)right_tmp.height() / 2.0)) / slope);
if (new_x >= 0 && new_x < right_tmp.width())
right_tmp.at(new_x, py).is_black(true);
}
}
}
// calculate the real position of image included pixels
basic_rect<int> right_pos = sh_dk_digital_region(right_tmp);
// select better image
if (right_pos.width() < left_pos.width())
{
if (right_pos.width() < min_w)
{
min_w = right_pos.width();
best_angle -= angle;
}
}
else
{
if (left_pos.width() < min_w)
{
min_w = left_pos.width();
best_angle += angle;
}
}
angle /= 2;
}
// best transform
image_type best_tmp(mid_tmp.width(), mid_tmp.height());
for (py = 0; py < best_tmp.height(); ++py)
for (px = 0; px < best_tmp.width(); ++px)
best_tmp.at(px, py).is_black(false);
for (py = 0; py < best_tmp.height(); ++py)
{
for (px = 0; px < best_tmp.width(); ++px)
{
if (mid_tmp.at(px, py).is_black())
{
double slope = tan(EXTL_PI / 2 + best_angle);
int new_x = xtl_round45(px - (py - ((double)best_tmp.height() / 2.0)) / slope);
if (new_x >= 0 && new_x < best_tmp.width())
best_tmp.at(new_x, py).is_black(true);
}
}
}
// update
basic_rect<int> best_pos = sh_dk_digital_region(best_tmp);
image_type new_img(best_pos.width(), best_pos.height());
for (py = best_pos.top(); py <= best_pos.bottom(); ++py)
for (px = best_pos.left(); px <= best_pos.right(); ++px)
new_img.at(px - best_pos.left(), py - best_pos.top()).is_black(best_tmp.at(px, py).is_black());
return new_img;
}
#endif // SH_DK_H