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_th.h
277 lines (240 loc) · 7.68 KB
/
sh_th.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
#ifndef SH_TH_H
#define SH_TH_H
// threshold
#include "prefix.h"
// search a threshold from the given gray histogram using basic method
// and return -1 if we don't find threshold.
// \note only for gray image background of which is white
inline int sh_th_search_basic_threshold(int gh[256])
{
double threshold = -1.0;
// calculate global average of gray
double n = 0, sum = 0;
for (int i = 0; i < 256; ++i)
{
n += gh[i];
sum += i * gh[i];
}
if (0 == n) return xtl_round45(threshold);
// initial threshold
threshold = sum / n;
// search threshold
double dt = 255;
while (fabs(dt) > 1e-10)
{
// calculate the average of gray: u1 & u2
double n1 = 0, sum1 = 0;
for (int i = 0; double(i) < threshold; ++i)
{
n1 += gh[i];
sum1 += i * gh[i];
}
double n2 = n - n1;
if (0 == n1 || 0 == n2) break;
double sum2 = sum - sum1;
// u1: the average of gray which is less than threshold
double u1 = sum1 / n1;
// u2: the average of gray which is greater c than threshold
double u2 = sum2 / n2;
//dt = ((u1 + u2) / 2) - threshold;
dt = 0.3 * u1 + 0.7 * u2 - threshold; // seems to be better
threshold += dt;
}
return xtl_round45(threshold);
}
// split image using global basic threshold
// \note only for gray image background of which is white
inline image_type sh_th_global_basic_threshold(image_type const& old_img)
{
if (old_img.is_empty()) return old_img;
// stats the gray histogram
int gh[256] = {0};
int px, py;
for (px = 0; px < (int)old_img.width(); ++px)
for (py = 0; py < (int)old_img.height(); ++py)
++gh[old_img.at(px, py).gray()];
// search hreshold
int t = sh_th_search_basic_threshold(gh);
// splits image using the global threshold
image_type new_img(old_img.width(), old_img.height());
for (px = 0; px < (int)old_img.width(); ++px)
{
for (py = 0; py < (int)old_img.height(); ++py)
{
if (-1 != t && old_img.at(px, py).gray() < t)
new_img.at(px, py).is_black(true);
else new_img.at(px, py).is_black(false);
}
}
return new_img;
}
// split image using partial basic threshold
// \note only for gray image background of which is white
inline image_type sh_th_partial_basic_threshold(image_type const& old_img, int part_n = 50)
{
if (old_img.is_empty()) return old_img;
// the image is too small
if (old_img.width() < part_n || old_img.height() < part_n)
part_n = xtl_min(old_img.width(), old_img.height()) / 2;
// traverse per-column
int i, px, py;
int pixel_n = part_n * part_n;
int part_n_2 = part_n >> 1;
image_type new_img(old_img.width(), old_img.height());
for (py = -part_n_2; py < (int)old_img.height() - part_n_2; ++py)
{
// stats the gray of partial block at the start position of per-line
int gh[256] = {0};
for (i = 0; i < pixel_n; ++i)
{
if ((py + i / part_n) >= 0 && (py + i / part_n) < old_img.height())
{
EXTL_ASSERT(old_img.at(i % part_n_2, py + i / part_n).gray() < 256);
++gh[old_img.at(i % part_n_2, py + i / part_n).gray()];
}
}
// stats partial histogram
for (px = -part_n_2; px < (int)old_img.width() - part_n_2; ++px)
{
// search partial threshold
int t = sh_th_search_basic_threshold(gh);
// split using threshold
if (-1 != t && old_img.at(px + part_n_2 , py + part_n_2).gray() < t)
new_img.at(px + part_n_2 , py + part_n_2).is_black(true);
else new_img.at(px + part_n_2 , py + part_n_2).is_black(false);
// update gray histogram
for (i = 0; i < part_n; ++i)
{
if ((py + i) >= 0 && (py + i) < old_img.height())
{
// remove previous partial column
if (px >= 0)
--gh[old_img.at(px, py + i).gray()];
// add next partial column
if (px + part_n < old_img.width())
++gh[old_img.at(px + part_n, py + i).gray()];
}
}
}
}
return new_img;
}
// search a threshold from the given gray histogram using otsu method
// and return -1 if we don't find threshold.
// \note only for gray image background of which is white
inline int sh_th_search_otsu_threshold(int gh[256])
{
// stats the number of gray
int i, n = 0, sum = 0;
for (i = 0; i < 256; ++i)
{
n += gh[i];
sum += i * gh[i];
}
double bc; // the between-class variance
double bc_max = -1.0; // the maximum between-class variance
double threshold = -1; // initial threshold
double n1 = 0, n2 = 0; // counters
double sum1 = 0, sum2 = 0; // counters
double u1 = 0, u2 = 0; // the gray average
for (int t = 0; t < 256; ++t)
{
n1 += gh[t]; // left counter
if (0 == n1) continue;
n2 = n - n1; // right counter
if (0 == n2) break;
sum1 += t * gh[t];
sum2 = sum - sum1;
u1 = sum1 / n1; // left average
u2 = sum2 / n2; // right average
// between-class variance
//bc = n1 * n2 * (u1 - u2) * (u1 - u2) / n;
bc = n1 * n2 * (u1 - u2) * (u1 - u2);
//bc = n1 * n2 * (u2 - u1);
if (bc > bc_max)
{
bc_max = bc;
//threshold = t + 1;
threshold = (u1 + 3 * u2) / 4;
}
}
return xtl_round45(threshold);
}
// global otsu adaptive threhold split
// note: only for gray image and foreground is black & background is white
inline image_type sh_th_global_otsu_threshold(image_type const& old_img)
{
if (old_img.is_empty()) return old_img;
// stats the gray histogram
int gh[256] = {0};
int px, py;
for (px = 0; px < (int)old_img.width(); ++px)
for (py = 0; py < (int)old_img.height(); ++py)
++gh[old_img.at(px, py).gray()];
// search hreshold
int t = sh_th_search_otsu_threshold(gh);
// splits image using the global threshold
image_type new_img(old_img.width(), old_img.height());
for (px = 0; px < (int)old_img.width(); ++px)
{
for (py = 0; py < (int)old_img.height(); ++py)
{
if (-1 != t && old_img.at(px, py).gray() < t)
new_img.at(px, py).is_black(true);
else new_img.at(px, py).is_black(false);
}
}
return new_img;
}
// partial otsu adaptive threhold split
// note: only for gray image and foreground is black & background is white
inline image_type sh_th_partial_otsu_threshold(image_type const& old_img, int part_n = 50)
{
if (old_img.is_empty()) return old_img;
// the image is too small
if (old_img.width() < part_n || old_img.height() < part_n)
part_n = xtl_min(old_img.width(), old_img.height()) / 2;
// traverse per-column
int i, px, py;
int pixel_n = part_n * part_n;
int part_n_2 = part_n >> 1;
image_type new_img(old_img.width(), old_img.height());
for (py = -part_n_2; py < (int)old_img.height() - part_n_2; ++py)
{
// stats the gray of partial block at the start position of per-line
int gh[256] = {0};
for (i = 0; i < pixel_n; ++i)
{
if ((py + i / part_n) >= 0 && (py + i / part_n) < old_img.height())
{
EXTL_ASSERT(old_img.at(i % part_n_2, py + i / part_n).gray() < 256);
++gh[old_img.at(i % part_n_2, py + i / part_n).gray()];
}
}
// stats partial histogram
for (px = -part_n_2; px < (int)old_img.width() - part_n_2; ++px)
{
// search threshold
int t = sh_th_search_otsu_threshold(gh);
// split using threshold
if (-1 != t && old_img.at(px + part_n_2 , py + part_n_2).gray() < t)
new_img.at(px + part_n_2 , py + part_n_2).is_black(true);
else new_img.at(px + part_n_2 , py + part_n_2).is_black(false);
// update gray histogram
for (i = 0; i < part_n; ++i)
{
if ((py + i) >= 0 && (py + i) < old_img.height())
{
// remove previous partial column
if (px >= 0)
--gh[old_img.at(px, py + i).gray()];
// add next partial column
if (px + part_n < old_img.width())
++gh[old_img.at(px + part_n, py + i).gray()];
}
}
}
}
return new_img;
}
#endif // SH_TH_H