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
/
Copy pathsample_helper.h
1960 lines (1734 loc) · 46 KB
/
sample_helper.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef SAMPLE_HELPER_H
#define SAMPLE_HELPER_H
#include "prefix.h"
#include "math.h"
inline void nearest_neighbor_zoom(image_type const& bmp_img, image_type& new_img)
{
double fh = (double)new_img.height() / bmp_img.height();
double fw = (double)new_img.width() / bmp_img.width();
int part_w = int(fw + 1);
int part_h = int(fh + 1);
int px, py;
// clear
for (px = 0; px < new_img.width(); ++px)
for (py = 0; py < new_img.height(); ++py)
new_img.at(px, py).is_black(false);
// interpolation
for (px = 0; px < bmp_img.width(); ++px)
{
for (py = 0; py < bmp_img.height(); ++py)
{
for (int k = 0; k < part_w * part_h; ++k)
{
int new_y = (int)(py * fh + k / part_w);
new_y = new_y < new_img.height()? new_y : (new_img.height() - 1);
int new_x = (int)(px * fw + k % part_w);
new_x = new_x < new_img.width()? new_x : (new_img.width() - 1);
// fill nearest neighbor pixel
new_img.at(new_x, new_y) = bmp_img.at(px, py);
}
}
}
}
inline void bilinear_zoom(image_type const& bmp_img, image_type& new_img)
{
double fh = (double)new_img.height() / bmp_img.height();
double fw = (double)new_img.width() / bmp_img.width();
int part_w = int(fw + 1);
int part_h = int(fh + 1);
int px, py;
// clear
for (px = 0; px < new_img.width(); ++px)
for (py = 0; py < new_img.height(); ++py)
new_img.at(px, py).is_black(false);
// interpolation
for (px = 0; px < bmp_img.width() - 1; ++px)
{
for (py = 0; py < bmp_img.height() - 1; ++py)
{
int f00 = bmp_img.at(px, py).gray();
int f01 = bmp_img.at(px + 1, py).gray();
int f10 = bmp_img.at(px, py + 1).gray();
int f11 = bmp_img.at(px + 1, py + 1).gray();
for (int k = 0; k < part_w * part_h; ++k)
{
int new_y = int(py * fh + k / part_w);
new_y = new_y < new_img.height()? new_y : (new_img.height() - 1);
int new_x = int(px * fw + k % part_w);
new_x = new_x < new_img.width()? new_x : (new_img.width() - 1);
// fill
double x = (double)(k / part_h) / part_w; // (0, 1)
double y = (double)(k % part_w) / part_h; // (0, 1)
int gray = int((f10 - f00) * x + (f01 - f00) * y + (f11 + f00 - f10 - f01) * x * y + f00);
new_img.at(new_x, new_y).gray(gray);
}
}
}
}
inline void mean_filter_smooth(image_type& bmp_img, int n = 3)
{
EXTL_ASSERT((n % 2)); // must be odd
int mid = n >> 1;
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
int avg = 0;
for (int k = 0; k < n * n; ++k)
{
avg += bmp_img.at(px + k / n, py + k % n).gray();
}
avg /= n * n;
bmp_img.at(px + mid, py + mid).gray(avg);
}
}
}
inline void xxxx_edge_detect(image_type& bmp_img, int hreshold)
{
int grad;
image_type tmp(bmp_img);
for (int px = 1; px < bmp_img.width(); ++px)
{
for (int py = 1; py < bmp_img.height(); ++py)
{
// horizonal gradient
grad = abs(bmp_img.at(px, py).gray() - bmp_img.at(px - 1, py).gray());
tmp.at(px - 1, py).is_black(grad > hreshold);
// vertical gradient
grad = abs(bmp_img.at(px, py).gray() - bmp_img.at(px, py - 1).gray());
tmp.at(px, py - 1).is_black(grad > hreshold? true : tmp.at(px, py - 1).is_black()); // merge
}
}
bmp_img = tmp;
}
inline void edge_detect_impl(double* o1, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k % n, py + k / n).gray() * o1[k];
}
double g = fabs(g1);
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void edge_detect_impl(double* o1, double* o2, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
double g2 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k / n, py + k % n).gray() * o1[k];
g2 += tmp.at(px + k / n, py + k % n).gray() * o2[k];
}
double g = (fabs(g1) + fabs(g2)) / 2;
//double g = sqrt(g1 * g1 + g2 * g2) / 2;
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void edge_detect_impl(double* o1, double* o2, double* o3, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
double g2 = 0;
double g3 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k / n, py + k % n).gray() * o1[k];
g2 += tmp.at(px + k / n, py + k % n).gray() * o2[k];
g3 += tmp.at(px + k / n, py + k % n).gray() * o3[k];
}
double g = (fabs(g1) + fabs(g2) + fabs(g3)) / 3;
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void edge_detect_impl(double* o1, double* o2, double* o3, double* o4, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
double g2 = 0;
double g3 = 0;
double g4 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k / n, py + k % n).gray() * o1[k];
g2 += tmp.at(px + k / n, py + k % n).gray() * o2[k];
g3 += tmp.at(px + k / n, py + k % n).gray() * o3[k];
g4 += tmp.at(px + k / n, py + k % n).gray() * o4[k];
}
double g = (fabs(g1) + fabs(g2) + fabs(g3) + fabs(g4)) / 4;
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void edge_detect_impl(double* o1, double* o2, double* o3, double* o4, double* o5, double* o6, double* o7, double* o8, int n, image_type& bmp_img, int hreshold)
{
int mid = n >> 1;
image_type tmp(bmp_img);
for (int px = 0; px < bmp_img.width() - n; ++px)
{
for (int py = 0; py < bmp_img.height() - n; ++py)
{
double g1 = 0;
double g2 = 0;
double g3 = 0;
double g4 = 0;
double g5 = 0;
double g6 = 0;
double g7 = 0;
double g8 = 0;
for (int k = 0; k < n * n; ++k)
{
g1 += tmp.at(px + k / n, py + k % n).gray() * o1[k];
g2 += tmp.at(px + k / n, py + k % n).gray() * o2[k];
g3 += tmp.at(px + k / n, py + k % n).gray() * o3[k];
g4 += tmp.at(px + k / n, py + k % n).gray() * o4[k];
g5 += tmp.at(px + k / n, py + k % n).gray() * o5[k];
g6 += tmp.at(px + k / n, py + k % n).gray() * o6[k];
g7 += tmp.at(px + k / n, py + k % n).gray() * o7[k];
g8 += tmp.at(px + k / n, py + k % n).gray() * o8[k];
}
double g = (fabs(g1) + fabs(g2) + fabs(g3) + fabs(g4) + fabs(g5) + fabs(g6) + fabs(g7) + fabs(g8)) / 8;
if (g / (n * n) > hreshold) bmp_img.at(px + mid, py + mid).is_black(true);
else bmp_img.at(px + mid, py + mid).is_black(false);
}
}
}
inline void roberts_edge_detect(image_type& bmp_img, int hreshold = 3)
{
double rx[4] = { 1, 0
, 0, -1
};
double ry[4] = { 0, 1
, -1, 0
};
edge_detect_impl(rx, ry, 2, bmp_img, hreshold);
}
inline void sobel_2_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double s1[9] = { -1, -2, -1
, 0, 0, 0
, 1, 2, 1
};
double s2[9] = { -1, 0, 1
, -2, 0, 2
, -1, 0, 1
};
edge_detect_impl(s1, s2, 3, bmp_img, hreshold);
}
inline void sobel_4_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double s1[9] = { -1, -2, -1
, 0, 0, 0
, 1, 2, 1
};
double s2[9] = { -1, 0, 1
, -2, 0, 2
, -1, 0, 1
};
double s3[9] = { 0, -1, -2
, 1, 0, -1
, 2, 1, 0
};
double s4[9] = { 0, 1, 2
, -1, 0, 1
, -2, -1, 0
};
edge_detect_impl(s1, s2, s3, s4, 3, bmp_img, hreshold);
}
inline void prewitt_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double px[9] = { -1, -1, -1
, 0, 0, 0
, 1, 1, 1
};
double py[9] = { -1, 0, 1
, -1, 0, 1
, -1, 0, 1
};
edge_detect_impl(px, py, 3, bmp_img, hreshold);
}
inline void prewitt_7_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double hp[49] ={ 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
-1, -1, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
};
double vp[49] ={ 0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
0, 0, -1, 0, 1, 0, 0,
};
edge_detect_impl(hp, vp, 7, bmp_img, hreshold);
}
inline void kirsch_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { 5, 5, 5
, -3, 0, -3
, -3, -3, -3
};
double k2[9] = { -3, 5, 5
, -3, 0, 5
, -3, -3, -3
};
double k3[9] = { -3, -3, 5
, -3, 0, 5
, -3, -3, 5
};
double k4[9] = { -3, -3, -3
, -3, 0, 5
, -3, 5, 5
};
double k5[9] = { -3, -3, -3
, -3, 0, -3
, 5, 5, 5
};
double k6[9] = { -3, -3, -3
, 5, 0, -3
, 5, 5, -3
};
double k7[9] = { 5, -3, -3
, 5, 0, -3
, 5, -3, -3
};
double k8[9] = { 5, 5, -3
, 5, 0, -3
, -3, -3, -3
};
edge_detect_impl(k1, k2, k3, k4, k5, k6, k7, k8, 3, bmp_img, hreshold);
}
// horizontal
inline void kirsch_edge_detect_h(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { 5, 5, 5
, -3, 0, -3
, -3, -3, -3
};
double k2[9] = { -3, -3, -3
, -3, 0, -3
, 5, 5, 5
};
edge_detect_impl(k1, k2, 3, bmp_img, hreshold);
}
// vertical
inline void kirsch_edge_detect_v(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { -3, -3, 5
, -3, 0, 5
, -3, -3, 5
};
double k2[9] = { 5, -3, -3
, 5, 0, -3
, 5, -3, -3
};
edge_detect_impl(k1, k2, 3, bmp_img, hreshold);
}
// right-diagonal
inline void kirsch_edge_detect_r(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { -3, 5, 5
, -3, 0, 5
, -3, -3, -3
};
double k2[9] = { -3, -3, -3
, 5, 0, -3
, 5, 5, -3
};
edge_detect_impl(k1, k2, 3, bmp_img, hreshold);
}
// left-diagonal
inline void kirsch_edge_detect_l(image_type& bmp_img, int hreshold = 5)
{
double k1[9] = { 5, 5, -3
, 5, 0, -3
, -3, -3, -3
};
double k2[9] = { -3, -3, -3
, -3, 0, 5
, -3, 5, 5
};
edge_detect_impl(k1, k2, 3, bmp_img, hreshold);
}
inline void isotropic_sobel_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double sx[9] = { -1, -1.414213, -1
, 0, 0, 0
, 1, 1.414213, 1
};
double sy[9] = { -1, 0, 1
, -1.414213, 0, 1.414213
, -1, 0, 1
};
edge_detect_impl(sx, sy, 3, bmp_img, hreshold);
}
inline void gauss_laplacian_edge_detect(image_type& bmp_img, int hreshold = 5)
{
double op[25] ={ -2, -4, -4, -4, -2,
-4, 0, 8, 0, -4,
-4, 8, 24, 8, -4,
-4, 0, 8, 0, -4,
-2, -4, -4, -4, -2
};
edge_detect_impl(op, 5, bmp_img, hreshold);
}
inline double marr_operator(int x, int y, double dr)
{
double xy2 = double(x * x + y * y);
double a = (xy2 - 2 * dr * dr) / (2 * 3.1415926 * dr * dr * dr * dr * dr * dr);
double b = exp(-xy2 / (2 * dr * dr));
return (a * b);
}
inline void marr_edge_detect(image_type& bmp_img, int hreshold = 5, int n = 5, double dr = 0.6)
{
int marr_n = n >> 1;
scoped_buffer<double> buf((2 * marr_n + 1) * (2 * marr_n + 1));
for (int y = 0; y < n; ++y)
{
for (int x = 0; x < n; ++x)
{
buf[y * n + x] = marr_operator(x - marr_n, y - marr_n, dr);
}
}
edge_detect_impl(buf.data(), n, bmp_img, hreshold);
}
inline void make_binarized_img(image_type& bmp_img, int hreshold)
{
for (int px = 0; px < bmp_img.width(); ++px)
{
for (int py = 0; py < bmp_img.height(); ++py)
{
bmp_img.at(px, py).is_black(bmp_img.at(px, py).gray() < hreshold);
}
}
}
inline void overall_tilt_correction(image_type& bmp_img)
{
int py, px;
// calcualte left average height
int left_h = 0;
int left_n = 0;
for (py = 0; py < bmp_img.height(); ++py)
{
for (px = 0; px < bmp_img.width() >> 1; ++px)
{
if (bmp_img.at(px, py).is_black())
{
left_h += py;
++left_n;
}
}
}
left_h /= left_n;
// calcualte right average height
int right_h = 0;
int right_n = 0;
for (py = 0; py < bmp_img.height(); ++py)
{
for (px = bmp_img.width() >> 1; px < bmp_img.width(); ++px)
{
if (bmp_img.at(px, py).is_black())
{
right_h += py;
++right_n;
}
}
}
right_h /= right_n;
// calculate slope
double slope = double(left_h - right_h) / (bmp_img.width() / 2);
// clear
image_type tmp(bmp_img);
for (py = 0; py < bmp_img.height(); ++py)
for (px = 0; px < bmp_img.width(); ++px)
bmp_img.at(px, py).is_black(false);
// correction
for (py = 0; py < tmp.height(); ++py)
{
for (px = 0; px < tmp.width(); ++px)
{
if (tmp.at(px, py).is_black())
{
int new_y = int(py + (px - ((double)tmp.width() / 2)) * slope);
if (new_y >= 0 && new_y < tmp.height())
{
bmp_img.at(px, new_y).is_black(true);
}
}
}
}
}
// rh: the hreshold of row pixel size
// hh: the hreshold of digital height
inline void split_row(image_type const& bmp_img, scoped_buffer<image_type>& row_imgs, int hh = 10)
{
// simple binarized image
image_type tmp(bmp_img);
mean_filter_smooth(tmp);
//sobel_4_edge_detect(tmp, 5);
kirsch_edge_detect(tmp, 5); // bold samples and binarized image for stats
overall_tilt_correction(tmp);
// correct raw image
image_type raw_data(bmp_img);
make_binarized_img(raw_data, 200);
overall_tilt_correction(raw_data);
// stats the number of pixels every rows
int py, px;
scoped_buffer<int> stats(tmp.height());
for (py = 0; py < tmp.height(); ++py)
{
stats[py] = 0;
for (px = 0; px < tmp.width(); ++px)
{
if (tmp.at(px, py).is_black())
++stats[py];
}
}
// stats minmum value
int min_n = stats[0];
for (py = 0; py < tmp.height(); ++py)
min_n = stats[py] < min_n? stats[py] : min_n;
// split rows
basic_pair<int, int> sp;
scoped_buffer<basic_pair<int, int> > sps;
// the adaptive hreshold of row pixel size
for (int rh = 5; rh < 100; ++rh)
{
sps.clear();
int start_n = 0, end_n = 0;
for (py = 0; py < tmp.height(); ++py)
{
if (stats[py] > min_n + rh)
{
// save start row
if (++start_n == 1)
sp.first(py);
end_n = 0;
}
else
{
// save end row
if (++end_n == 1 && start_n > hh)
{
sp.second(py);
sps.push_back(sp);
// draw seperator
/*for (px = 0; px < tmp.width(); ++px)
{
tmp.at(px, sp.first()).is_black(true);
tmp.at(px, sp.second()).is_black(true);
}*/
}
start_n = 0;
}
}
if (sps.size() == 5)
{
//EXTL_TRACE(_T("split row:%d"), rh);
break;
}
}
for (py = 0; py < (int)sps.size(); ++py)
{
// save images after spliting
image_type row_img(raw_data.width(), sps[py].second() - sps[py].first());
EXTL_ASSERT(sps[py].second() > sps[py].first());
for (int k = sps[py].first(); k < sps[py].second(); ++k)
{
for (px = 0; px < raw_data.width(); ++px)
{
row_img.at(px, k - sps[py].first()).is_black(raw_data.at(px, k).is_black());
}
}
row_imgs.push_back(row_img);
}
}
inline void split_col(scoped_buffer<image_type>& row_imgs, scoped_buffer<image_type>& rowcol_imgs, int wh = 3)
{
for (int col_i = 0; col_i < (int)row_imgs.size(); ++col_i)
{
// correct row_imgs
overall_tilt_correction(row_imgs[col_i]);
// split pre-handle
image_type tmp(row_imgs[col_i]);
mean_filter_smooth(tmp);
kirsch_edge_detect(tmp, 5); // bold samples and binarized image for stats
overall_tilt_correction(tmp);
// stats the number of pixels every columns
int py, px;
scoped_buffer<int> stats(tmp.width());
for (px = 0; px < tmp.width(); ++px)
{
stats[px] = 0;
for (py = 0; py < tmp.height(); ++py)
{
if (tmp.at(px, py).is_black())
++stats[px];
}
}
// stats minmum value
int min_n = stats[0];
for (px = 0; px < tmp.width(); ++px)
min_n = stats[px] < min_n? stats[px] : min_n;
// split columns
basic_pair<int, int> sp;
scoped_buffer<basic_pair<int, int> > sps;
// the adaptive hreshold of column pixel size
for (int ch = 1; ch < 50; ++ch)
{
sps.clear();
int start_n = 0, end_n = 0;
for (px = 0; px < tmp.width(); ++px)
{
if (stats[px] > min_n + ch)
{
// save start row
if (++start_n == 1)
sp.first(px);
end_n = 0;
}
else
{
// save end row
if (++end_n == 1 && start_n > wh)
{
sp.second(px);
sps.push_back(sp);
// draw seperator
/*for (py = 0; py < tmp.height(); ++py)
{
tmp.at(py, sp.first()).is_black(true);
tmp.at(py, sp.second()).is_black(true);
}*/
}
start_n = 0;
}
}
if (sps.size() == 10)
{
//EXTL_TRACE(_T("split col:%d"), ch);
break;
}
}
for (py = 0; py < (int)sps.size(); ++py)
{
// save images after spliting
image_type img(sps[py].second() - sps[py].first(), row_imgs[col_i].height());
EXTL_ASSERT(sps[py].second() > sps[py].first());
for (int pi = 0; pi < row_imgs[col_i].height(); ++pi)
{
for (int pj = sps[py].first(); pj < sps[py].second(); ++pj)
{
img.at(pj - sps[py].first(), pi).is_black(row_imgs[col_i].at(pj, pi).is_black());
}
}
rowcol_imgs.push_back(img);
}
//show_img(tmp);
}
}
// calculate the real position of image included pixels
inline void calc_real_pos(image_type const& bmp_img, basic_rect<int>& pos)
{
int margin = 3;
pos.clear();
// stats the number of pixels every rows
int py, px;
scoped_buffer<int> hstats(bmp_img.height());
for (py = 0; py < bmp_img.height(); ++py)
{
hstats[py] = 0;
for (px = 0; px < bmp_img.width(); ++px)
{
if (bmp_img.at(px, py).is_black())
++hstats[py];
}
}
// split by height
for (py = 0; py < bmp_img.height(); ++py)
{
if (hstats[py] > 0)
{
pos.top(py);
break;
}
}
if (py == bmp_img.height()) pos.top(bmp_img.height() - 1);
if (pos.top() - margin >= 0) pos.top(pos.top() - margin);
for (py = bmp_img.height(); py > 0; --py)
{
if (hstats[py - 1] > 0)
{
pos.bottom(py - 1);
break;
}
}
if (py == 0) pos.bottom(0);
if (pos.bottom() + margin < bmp_img.height()) pos.bottom(pos.bottom() + margin);
EXTL_ASSERT(pos.bottom() > pos.top());
// stats the number of pixels every columns
scoped_buffer<int> wstats(bmp_img.width());
for (px = 0; px < bmp_img.width(); ++px)
{
wstats[px] = 0;
for (py = 0; py < bmp_img.height(); ++py)
{
if (bmp_img.at(px, py).is_black())
++wstats[px];
}
}
// split by width
for (px = 0; px < bmp_img.width(); ++px)
{
if (wstats[px] > 0)
{
pos.left(px);
break;
}
}
if (px == bmp_img.width()) pos.left(bmp_img.width() - 1);
if (pos.left() - margin >= 0) pos.left(pos.left() - margin);
for (px = bmp_img.width(); px > 0; --px)
{
if (wstats[px - 1] > 0)
{
pos.right(px - 1);
break;
}
}
if (px == 0) pos.right(0);
if (pos.right() + margin < bmp_img.width()) pos.right(pos.right() + margin);
EXTL_ASSERT(pos.right() > pos.left());
}
inline void horizontal_side_slip_deskew_img(image_type const& bmp_img, image_type& new_img)
{
int py, px;
double angle = EXTL_PI / 4;
image_type mid_tmp(bmp_img.width() + 10, bmp_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(bmp_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;
calc_real_pos(mid_tmp, mid_pos);
// 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 = int(px - (py - ((double)left_tmp.height() / 2)) / 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;
calc_real_pos(left_tmp, left_pos);
// 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 = int(px - (py - ((double)right_tmp.height() / 2)) / 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;
calc_real_pos(right_tmp, right_pos);
// 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;
calc_real_pos(mid_tmp, mid_pos);
image_type tmp(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)
tmp.at(px - mid_pos.left(), py - mid_pos.top()).is_black(mid_tmp.at(px, py).is_black());
new_img = tmp;
}
inline void vertical_side_slip_deskew_img(image_type const& bmp_img, image_type& new_img)
{
int py, px;
double angle = EXTL_PI / 8;
image_type mid_tmp(bmp_img.height() + 10, bmp_img.width() + 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(bmp_img.at(py - 5, px - 5).is_black());
}
}
while (angle > EXTL_PI / 180)
{
// calculate the real position of image included pixels
basic_rect<int> mid_pos;
calc_real_pos(mid_tmp, mid_pos);
// top transform;
image_type top_tmp(mid_tmp.height(), mid_tmp.width());
for (py = 0; py < top_tmp.height(); ++py)
for (px = 0; px < top_tmp.width(); ++px)
top_tmp.at(px, py).is_black(false);
for (py = 0; py < top_tmp.height(); ++py)
{
for (px = 0; px < top_tmp.width(); ++px)
{
if (mid_tmp.at(px, py).is_black())
{
double slope = tan(-angle);
int new_i = int(py + (px - ((double)top_tmp.width() / 2)) * slope);
if (new_i >= 0 && new_i < top_tmp.height())
top_tmp.at(new_i, px).is_black(true);
}
}
}
// calculate the real position of image included pixels
basic_rect<int> top_pos;
calc_real_pos(top_tmp, top_pos);
// bottom transform;
image_type bottom_tmp(mid_tmp.height(), mid_tmp.width());
for (py = 0; py < bottom_tmp.height(); ++py)
for (px = 0; px < bottom_tmp.width(); ++px)
bottom_tmp.at(px, py).is_black(false);
for (py = 0; py < bottom_tmp.height(); ++py)
{
for (px = 0; px < bottom_tmp.width(); ++px)
{
if (mid_tmp.at(px, py).is_black())
{
double slope = tan(angle);
int new_i = int(py + (px - ((double)bottom_tmp.width() / 2)) * slope);
if (new_i >= 0 && new_i < bottom_tmp.height())
bottom_tmp.at(new_i, px).is_black(true);
}
}
}
// calculate the real position of image included pixels
basic_rect<int> bottom_pos;
calc_real_pos(bottom_tmp, bottom_pos);
// select better image
if (bottom_pos.height() < top_pos.height())
{
if (bottom_pos.height() < mid_pos.height())
mid_tmp = bottom_tmp;
}
else
{
if (top_pos.height() < mid_pos.height())
mid_tmp = top_tmp;
}