-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbinarizewolfjolion.cpp
412 lines (330 loc) · 10.4 KB
/
binarizewolfjolion.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**************************************************************
* Binarization with several methods
* (0) Niblacks method
* (1) Sauvola & Co.
* ICDAR 1997, pp 147-152
* (2) by myself - Christian Wolf
* Research notebook 19.4.2001, page 129
* (3) by myself - Christian Wolf
* 20.4.2007
*
* See also:
* Research notebook 24.4.2001, page 132 (Calculation of s)
**************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <iostream>
// #include <cv>
// #include <highgui>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
enum NiblackVersion
{
NIBLACK=0,
SAUVOLA,
WOLFJOLION,
};
#define BINARIZEWOLF_VERSION "2.4 (August 1st, 2014)"
#define uget(x,y) at<unsigned char>(y,x)
#define uset(x,y,v) at<unsigned char>(y,x)=v;
#define fget(x,y) at<float>(y,x)
#define fset(x,y,v) at<float>(y,x)=v;
/**********************************************************
* Usage
**********************************************************/
static void usage (char *com) {
cerr << "usage: " << com << " [ -x <winx> -y <winy> -k <parameter> ] [ version ] <inputimage> <outputimage>\n\n"
<< "version: n Niblack (1986) needs white text on black background\n"
<< " s Sauvola et al. (1997) needs black text on white background\n"
<< " w Wolf et al. (2001) needs black text on white background\n"
<< "\n"
<< "Default version: w (Wolf et al. 2001)\n"
<< "\n"
<< "example:\n"
<< " " << com << " w in.pgm out.pgm\n"
<< " " << com << " in.pgm out.pgm\n"
<< " " << com << " s -x 50 -y 50 -k 0.6 in.pgm out.pgm\n";
}
// *************************************************************
// glide a window across the image and
// *************************************************************
// create two maps: mean and standard deviation.
//
// Version patched by Thibault Yohan (using opencv integral images)
double calcLocalStats (Mat &im, Mat &map_m, Mat &map_s, int winx, int winy) {
Mat im_sum, im_sum_sq;
cv::integral(im,im_sum,im_sum_sq,CV_64F);
double m,s,max_s,sum,sum_sq;
int wxh = winx/2;
int wyh = winy/2;
int x_firstth= wxh;
int y_firstth= wyh;
int y_lastth = im.rows-wyh-1;
double winarea = winx*winy;
max_s = 0;
for (int j = y_firstth ; j<=y_lastth; j++){
sum = sum_sq = 0;
// for sum array iterator pointer
double *sum_top_left = im_sum.ptr<double>(j - wyh);
double *sum_top_right = sum_top_left + winx;
double *sum_bottom_left = im_sum.ptr<double>(j - wyh + winy);
double *sum_bottom_right = sum_bottom_left + winx;
// for sum_sq array iterator pointer
double *sum_eq_top_left = im_sum_sq.ptr<double>(j - wyh);
double *sum_eq_top_right = sum_eq_top_left + winx;
double *sum_eq_bottom_left = im_sum_sq.ptr<double>(j - wyh + winy);
double *sum_eq_bottom_right = sum_eq_bottom_left + winx;
sum = (*sum_bottom_right + *sum_top_left) - (*sum_top_right + *sum_bottom_left);
sum_sq = (*sum_eq_bottom_right + *sum_eq_top_left) - (*sum_eq_top_right + *sum_eq_bottom_left);
m = sum / winarea;
s = sqrt ((sum_sq - m*sum)/winarea);
if (s > max_s) max_s = s;
float *map_m_data = map_m.ptr<float>(j) + x_firstth;
float *map_s_data = map_s.ptr<float>(j) + x_firstth;
*map_m_data++ = m;
*map_s_data++ = s;
// Shift the window, add and remove new/old values to the histogram
for (int i=1 ; i <= im.cols-winx; i++) {
sum_top_left++, sum_top_right++, sum_bottom_left++, sum_bottom_right++;
sum_eq_top_left++, sum_eq_top_right++, sum_eq_bottom_left++, sum_eq_bottom_right++;
sum = (*sum_bottom_right + *sum_top_left) - (*sum_top_right + *sum_bottom_left);
sum_sq = (*sum_eq_bottom_right + *sum_eq_top_left) - (*sum_eq_top_right + *sum_eq_bottom_left);
m = sum / winarea;
s = sqrt ((sum_sq - m*sum)/winarea);
if (s > max_s) max_s = s;
*map_m_data++ = m;
*map_s_data++ = s;
}
}
return max_s;
}
/**********************************************************
* The binarization routine
**********************************************************/
void NiblackSauvolaWolfJolion (Mat im, Mat output, NiblackVersion version,
int winx, int winy, double k, double dR) {
double m, s, max_s;
double th=0;
double min_I, max_I;
int wxh = winx/2;
int wyh = winy/2;
int x_firstth= wxh;
int x_lastth = im.cols-wxh-1;
int y_lastth = im.rows-wyh-1;
int y_firstth= wyh;
// int mx, my;
// Create local statistics and store them in a double matrices
Mat map_m = Mat::zeros (im.rows, im.cols, CV_32F);
Mat map_s = Mat::zeros (im.rows, im.cols, CV_32F);
max_s = calcLocalStats (im, map_m, map_s, winx, winy);
minMaxLoc(im, &min_I, &max_I);
Mat thsurf (im.rows, im.cols, CV_32F);
// Create the threshold surface, including border processing
// ----------------------------------------------------
for (int j = y_firstth ; j<=y_lastth; j++) {
float *th_surf_data = thsurf.ptr<float>(j) + wxh;
float *map_m_data = map_m.ptr<float>(j) + wxh;
float *map_s_data = map_s.ptr<float>(j) + wxh;
// NORMAL, NON-BORDER AREA IN THE MIDDLE OF THE WINDOW:
for (int i=0 ; i <= im.cols-winx; i++) {
m = *map_m_data++;
s = *map_s_data++;
// Calculate the threshold
switch (version) {
case NIBLACK:
th = m + k*s;
break;
case SAUVOLA:
th = m * (1 + k*(s/dR-1));
break;
case WOLFJOLION:
th = m + k * (s/max_s-1) * (m-min_I);
break;
default:
cerr << "Unknown threshold type in ImageThresholder::surfaceNiblackImproved()\n";
exit (1);
}
// thsurf.fset(i+wxh,j,th);
*th_surf_data++ = th;
if (i==0) {
// LEFT BORDER
float *th_surf_ptr = thsurf.ptr<float>(j);
for (int i=0; i<=x_firstth; ++i)
*th_surf_ptr++ = th;
// LEFT-UPPER CORNER
if (j==y_firstth)
{
for (int u=0; u<y_firstth; ++u)
{
float *th_surf_ptr = thsurf.ptr<float>(u);
for (int i=0; i<=x_firstth; ++i)
*th_surf_ptr++ = th;
}
}
// LEFT-LOWER CORNER
if (j==y_lastth)
{
for (int u=y_lastth+1; u<im.rows; ++u)
{
float *th_surf_ptr = thsurf.ptr<float>(u);
for (int i=0; i<=x_firstth; ++i)
*th_surf_ptr++ = th;
}
}
}
// UPPER BORDER
if (j==y_firstth)
for (int u=0; u<y_firstth; ++u)
thsurf.fset(i+wxh,u,th);
// LOWER BORDER
if (j==y_lastth)
for (int u=y_lastth+1; u<im.rows; ++u)
thsurf.fset(i+wxh,u,th);
}
// RIGHT BORDER
float *th_surf_ptr = thsurf.ptr<float>(j) + x_lastth;
for (int i=x_lastth; i<im.cols; ++i)
// thsurf.fset(i,j,th);
*th_surf_ptr++ = th;
// RIGHT-UPPER CORNER
if (j==y_firstth)
{
for (int u=0; u<y_firstth; ++u)
{
float *th_surf_ptr = thsurf.ptr<float>(u) + x_lastth;
for (int i=x_lastth; i<im.cols; ++i)
*th_surf_ptr++ = th;
}
}
// RIGHT-LOWER CORNER
if (j==y_lastth)
{
for (int u=y_lastth+1; u<im.rows; ++u)
{
float *th_surf_ptr = thsurf.ptr<float>(u) + x_lastth;
for (int i=x_lastth; i<im.cols; ++i)
*th_surf_ptr++ = th;
}
}
}
cerr << "surface created" << endl;
for (int y=0; y<im.rows; ++y)
{
unsigned char *im_data = im.ptr<unsigned char>(y);
float *th_surf_data = thsurf.ptr<float>(y);
unsigned char *output_data = output.ptr<unsigned char>(y);
for (int x=0; x<im.cols; ++x)
{
*output_data = *im_data >= *th_surf_data ? 255 : 0;
im_data++;
th_surf_data++;
output_data++;
}
}
}
/**********************************************************
* The main function
**********************************************************/
int main (int argc, char **argv)
{
char version;
int c;
int winx=0, winy=0;
float optK=0.5;
bool didSpecifyK=false;
NiblackVersion versionCode;
char *inputname, *outputname, *versionstring;
cerr << "===========================================================\n"
<< "Christian Wolf, LIRIS Laboratory, Lyon, France.\n"
<< "[email protected]\n"
<< "Version " << BINARIZEWOLF_VERSION << endl
<< "===========================================================\n";
// Argument processing
while ((c = getopt (argc, argv, "x:y:k:")) != EOF) {
switch (c) {
case 'x':
winx = atof(optarg);
break;
case 'y':
winy = atof(optarg);
break;
case 'k':
optK = atof(optarg);
didSpecifyK = true;
break;
case '?':
usage (*argv);
cerr << "\nProblem parsing the options!\n\n";
exit (1);
}
}
switch(argc-optind)
{
case 3:
versionstring=argv[optind];
inputname=argv[optind+1];
outputname=argv[optind+2];
break;
case 2:
versionstring=(char *) "w";
inputname=argv[optind];
outputname=argv[optind+1];
break;
default:
usage (*argv);
exit (1);
}
cerr << "Adaptive binarization\n"
<< "Threshold calculation: ";
// Determine the method
version = versionstring[0];
switch (version)
{
case 'n':
versionCode = NIBLACK;
cerr << "Niblack (1986)\n";
break;
case 's':
versionCode = SAUVOLA;
cerr << "Sauvola et al. (1997)\n";
break;
case 'w':
versionCode = WOLFJOLION;
cerr << "Wolf and Jolion (2001)\n";
break;
default:
usage (*argv);
cerr << "\nInvalid version: '" << version << "'!";
}
cerr << "parameter k=" << optK << endl;
if (!didSpecifyK)
cerr << "Setting k to default value " << optK << endl;
// Load the image in grayscale mode
Mat input = imread(inputname,CV_LOAD_IMAGE_GRAYSCALE);
if ((input.rows<=0) || (input.cols<=0)) {
cerr << "*** ERROR: Couldn't read input image " << inputname << endl;
exit(1);
}
// Treat the window size
if (winx==0||winy==0) {
cerr << "Input size: " << input.cols << "x" << input.rows << endl;
winy = (int) (2.0 * input.rows-1)/3;
winx = (int) input.cols-1 < winy ? input.cols-1 : winy;
// if the window is too big, than we asume that the image
// is not a single text box, but a document page: set
// the window size to a fixed constant.
if (winx > 100)
winx = winy = 40;
cerr << "Setting window size to [" << winx
<< "," << winy << "].\n";
}
// Threshold
Mat output (input.rows, input.cols, CV_8U);
NiblackSauvolaWolfJolion (input, output, versionCode, winx, winy, optK, 128);
// Write the tresholded file
cerr << "Writing binarized image to file '" << outputname << "'.\n";
imwrite (outputname, output);
return 0;
}