forked from rajbot/autocrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoCropScribe.c
2172 lines (1769 loc) · 66.4 KB
/
autoCropScribe.c
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
/*
Copyright(c)2008 Internet Archive. Software license GPL version 2.
run with:
autoCropScribe filein.jpg rotateDirection
rotationDirection is 1, -1, or 0
We use 1 to indicate that the page should be rotated clockwise, and -1 to
indicate counter-clockwise rotation. We use 0 to indicate foldout pages,
which do not need to be rotated.
*/
#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"
#include <assert.h>
#include <math.h> //for sqrt
#include <float.h> //for DBL_MAX
#include <limits.h> //for INT_MAX
#include "autoCropCommon.h"
#define debugstr printf
//#define debugstr
//#define WRITE_DEBUG_IMAGES 1
static const l_float32 deg2rad = 3.1415926535 / 180.;
static inline l_int32 min (l_int32 a, l_int32 b) {
return b + ((a-b) & (a-b)>>31);
}
static inline l_int32 max (l_int32 a, l_int32 b) {
return a - ((a-b) & (a-b)>>31);
}
#define DEBUGMOV 0
#if DEBUGMOV
#include <libgen.h> //for basename()
struct DebugMov{
double angle;
char *outDir;
char *filename;
int framenum;
int edgeBinding;
int edgeOuter;
} debugmov;
#endif //DEBUGMOV
/// CalculateAvgBlock()
/// calculate avg luma of a block
///____________________________________________________________________________
double CalculateAvgBlock(PIX *pixg,
l_uint32 left,
l_uint32 right,
l_uint32 top,
l_uint32 bottom)
{
l_uint32 acc=0;
l_uint32 a, i, j;
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
assert(top>=0);
assert(left>=0);
assert(bottom<h);
assert(right<w);
acc=0;
for (i=left; i<=right; i++) {
for (j=top; j<=bottom; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
acc += a;
}
}
//printf("%d \n", acc);
double avg = acc;
avg /= ((right-left+1)*(bottom-top+1));
return avg;
}
/// FindBestVarRow()
/// find row with least variance
///____________________________________________________________________________
l_uint32 FindMinVarRow(PIX *pixg,
l_uint32 left,
l_uint32 right,
l_uint32 top,
l_uint32 bottom,
double thresh,
l_int32 *retj,
double *retVar
)
{
l_uint32 i, j;
l_uint32 a;
double minVar=DBL_MAX;
l_int32 minj=-1;
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
assert(left>=0);
assert(left<right);
assert(right<w);
assert(top>=0);
assert(top<bottom);
assert(bottom<h);
double var;
l_uint32 width20 = (l_uint32)(w * 0.20);
for (j=top; j<=bottom; j++) {
printf("%d: ", j);
var = 0;
double avg = CalculateAvgRow(pixg, j, left+width20, right-width20);
if (avg<thresh) {
printf("avg too low, continuing! (%f)\n", avg);
continue;
}
for (i=left+width20; i<right-width20; i++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
double diff = avg-a;
var += (diff * diff);
}
printf("var=%f avg=%f\n", var, avg);
if (var < minVar) {
minVar = var;
minj = j;
}
}
*retj = minj;
*retVar = minVar;
return (-1 != minj);
}
/// FindBestVarCol()
/// find col with least variance
///____________________________________________________________________________
l_uint32 FindMinVarCol(PIX *pixg,
l_uint32 left,
l_uint32 right,
l_uint32 top,
l_uint32 bottom,
double thresh,
l_int32 *reti,
double *retVar
)
{
l_uint32 i, j;
l_uint32 a;
double minVar=DBL_MAX;
l_int32 mini=-1;
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
assert(left>=0);
assert(left<right);
assert(right<w);
assert(top>=0);
assert(top<bottom);
assert(bottom<h);
double var;
l_uint32 h20 = (l_uint32)(h * 0.20);
for (i=left; i<=right; i++) {
//printf("%d: ", i);
var = 0;
double avg = CalculateAvgCol(pixg, i, top+h20, bottom-h20);
if (avg<thresh) {
//printf("avg too low, continuing! (%f)\n", avg);
continue;
}
for (j=top+h20; j<bottom-h20; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
double diff = avg-a;
var += (diff * diff);
}
//printf("var=%f avg=%f\n", var, avg);
if (var < minVar) {
minVar = var;
mini = i;
}
}
*reti = mini;
*retVar = minVar;
return (-1 != mini);
}
/// CalculateFullPageSADrow()
/// calculate sum of absolute differences of two rows of adjacent columns
/// last SAD calculation is for row i=right and i=right+1.
///____________________________________________________________________________
double CalculateFullPageSADrow(PIX *pixg,
l_uint32 left,
l_uint32 right,
l_uint32 top,
l_uint32 bottom
)
{
l_uint32 i, j;
l_uint32 acc=0;
l_uint32 a,b;
l_uint32 maxDiff=0;
l_int32 maxj=-1;
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
assert(left>=0);
assert(left<right);
assert(right<w);
assert(top>=0);
assert(top<bottom);
assert(bottom<h);
for (j=top; j<bottom; j++) {
//printf("%d: ", i);
for (i=left; i<right; i++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
retval = pixGetPixel(pixg, i, j+1, &b);
assert(0 == retval);
//printf("%d ", val);
acc += (abs(a-b));
}
}
double sum = (double)acc;
//printf("acc=%d, sum=%f\n", acc, sum);
return sum;
}
/// FindGutterCrop()
/// This funciton finds the gutter-side (binding-side) crop line.
/// TODO: The return value should indicate a confidence.
///____________________________________________________________________________
l_uint32 FindGutterCrop(PIX *pixg, l_int32 rotDir) {
//Currently, we can only do right-hand leafs
assert(1 == rotDir);
#define kKernelHeight 0.30
//Assume we can find the binding within the first 10% of the image width
l_uint32 width = pixGetWidth(pixg);
l_uint32 width10 = (l_uint32)(width * 0.10);
l_uint32 h = pixGetHeight( pixg );
//kernel has height of (h/2 +/- h*hPercent/2)
l_uint32 jTop = (l_uint32)((1-kKernelHeight)*0.5*h);
l_uint32 jBot = (l_uint32)((1+kKernelHeight)*0.5*h);
l_int32 strongEdge;
l_uint32 strongEdgeDiff;
//TODO: calculate left bound based on amount of BRING_IN_BLACK due to rotation
CalculateSADcol(pixg, 5, width10, jTop, jBot, &strongEdge, &strongEdgeDiff);
printf("strongest edge of gutter is at i=%d with diff=%d\n", strongEdge, strongEdgeDiff);
//TODO: what if strongEdge = 0 or something obviously bad?
//Look for a second strong edge for the other side of the binding.
//This edge should exist within +/- 3% of the image width.
l_int32 secondEdgeL, secondEdgeR;
l_uint32 secondEdgeDiffL, secondEdgeDiffR;
l_uint32 width3p = (l_uint32)(width * 0.03);
if (0 != strongEdge) {
l_int32 searchLimit = max(0, strongEdge-width3p);
CalculateSADcol(pixg, searchLimit, strongEdge-1, jTop, jBot, &secondEdgeL, &secondEdgeDiffL);
printf("secondEdgeL = %d, diff = %d\n", secondEdgeL, secondEdgeDiffL);
} else {
//FIXME what to do here?
return 0;
}
if (strongEdge < (width-2)) {
l_int32 searchLimit = strongEdge + width3p;
assert(searchLimit>strongEdge+1);
CalculateSADcol(pixg, strongEdge+1, searchLimit, jTop, jBot, &secondEdgeR, &secondEdgeDiffR);
printf("secondEdgeR = %d, diff = %d\n", secondEdgeR, secondEdgeDiffR);
} else {
//FIXME what to do here?
return 0;
}
l_int32 secondEdge;
l_uint32 secondEdgeDiff;
if (secondEdgeDiffR > secondEdgeDiffL) {
secondEdge = secondEdgeR;
secondEdgeDiff = secondEdgeDiffR;
} else if (secondEdgeDiffR < secondEdgeDiffL) {
secondEdge = secondEdgeL;
secondEdgeDiff = secondEdgeDiffL;
} else {
//FIXME
return 0;
}
if ((secondEdgeDiff > (strongEdgeDiff*0.80)) && (secondEdgeDiff < (strongEdgeDiff*1.20))) {
printf("Found gutter at %d!\n", strongEdge);
return 1;
}
debugstr("Could not find gutter!\n");
return 0;
}
/// FindBindingEdge()
///____________________________________________________________________________
l_uint32 FindBindingEdge(PIX *pixg,
l_int32 rotDir,
float *skew,
l_uint32 *thesh)
{
//Currently, we can only do right-hand leafs
assert((1 == rotDir) || (-1 == rotDir));
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
l_uint32 width10 = (l_uint32)(w * 0.10);
//kernel has height of (h/2 +/- h*hPercent/2)
l_uint32 jTop = (l_uint32)((1-kKernelHeight)*0.5*h);
l_uint32 jBot = (l_uint32)((1+kKernelHeight)*0.5*h);
// Find the strong edge, which should be one of the two sides of the binding
// Rotate the image to maximize SAD
l_int32 bindingEdge = -1;
l_uint32 bindingEdgeDiff = 0;
float bindingDelta;
float delta;
//0.05 degrees is a good increment for the final search
for (delta=-1.0; delta<=1.0; delta+=0.2) {
PIX *pixt = pixRotate(pixg,
deg2rad*delta,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
l_int32 strongEdge;
l_uint32 strongEdgeDiff;
l_uint32 limitLeft = calcLimitLeft(w,h,delta);
//printf("limitLeft = %d\n", limitLeft);
#if DEBUGMOV
debugmov.angle = delta;
#endif //DEBUGMOV
l_uint32 left, right;
if (1 == rotDir) {
left = limitLeft;
right = width10;
} else {
left = w - width10;
right = w - limitLeft-1;
}
CalculateSADcol(pixt, left, right, jTop, jBot, &strongEdge, &strongEdgeDiff);
//printf("delta=%f, strongest edge of gutter is at i=%d with diff=%d, w,h=(%d,%d)\n", delta, strongEdge, strongEdgeDiff, w, h);
if (strongEdgeDiff > bindingEdgeDiff) {
bindingEdge = strongEdge;
bindingEdgeDiff = strongEdgeDiff;
bindingDelta = delta;
#if DEBUGMOV
debugmov.edgeBinding = bindingEdge;
#endif //DEBUGMOV
}
pixDestroy(&pixt);
}
assert(-1 != bindingEdge); //TODO: handle error
printf("BEST: delta=%f, strongest edge of gutter is at i=%d with diff=%d\n", bindingDelta, bindingEdge, bindingEdgeDiff);
*skew = bindingDelta;
#if DEBUGMOV
debugmov.angle = bindingDelta;
#endif //DEBUGMOV
// Now compute threshold for psudo-bitonalization
// Use midpoint between avg luma of dark and light lines of binding edge
PIX *pixt = pixRotate(pixg,
deg2rad*bindingDelta,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
//pixWrite(DEBUG_IMAGE_DIR "outgray.jpg", pixt, IFF_JFIF_JPEG);
double bindingLumaA = CalculateAvgCol(pixt, bindingEdge, jTop, jBot);
printf("lumaA = %f\n", bindingLumaA);
double bindingLumaB = CalculateAvgCol(pixt, bindingEdge+1, jTop, jBot);
printf("lumaB = %f\n", bindingLumaB);
/*
{
int i;
for (i=bindingEdge-10; i<bindingEdge+10; i++) {
double bindingLuma = CalculateAvgCol(pixt, i, jTop, jBot);
printf("i=%d, luma=%f\n", i, bindingLuma);
}
}
*/
double threshold = (l_uint32)((bindingLumaA + bindingLumaB) / 2);
//TODO: ensure this threshold is reasonable
printf("thesh = %f\n", threshold);
*thesh = (l_uint32)threshold;
l_uint32 width3p = (l_uint32)(w * 0.03);
l_uint32 rightEdge;
l_uint32 numBlackLines = 0;
if (bindingLumaA > bindingLumaB) { //found left edge
l_uint32 i;
l_uint32 rightLimit = bindingEdge+width3p;
for (i=bindingEdge+1; i<rightLimit; i++) {
double lumaAvg = CalculateAvgCol(pixt, i, jTop, jBot);
printf("i=%d, avg=%f\n", i, lumaAvg);
if (lumaAvg<threshold) {
numBlackLines++;
} else {
rightEdge = i-1;
break;
}
}
printf("numBlackLines = %d\n", numBlackLines);
} else if (bindingLumaA < bindingLumaB) { //found right edge
l_uint32 i;
l_uint32 leftLimit = bindingEdge-width3p;
rightEdge = bindingEdge;
if (leftLimit<0) leftLimit = 0;
printf("found right edge of gutter, leftLimit=%d, rightLimit=%d\n", leftLimit, bindingEdge-1);
for (i=bindingEdge-1; i>leftLimit; i--) {
double lumaAvg = CalculateAvgCol(pixt, i, jTop, jBot);
printf("i=%d, avg=%f\n", i, lumaAvg);
if (lumaAvg<threshold) {
numBlackLines++;
} else {
break;
}
}
printf("numBlackLines = %d\n", numBlackLines);
} else {
return -1; //TODO: handle error
}
///temp code to calculate some thesholds..
/*
l_uint32 a, j, i = rightEdge;
l_uint32 numBlackPels = 0;
for (j=jTop; j<jBot; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
if (a<threshold) {
numBlackPels++;
}
}
printf("%d: numBlack=%d\n", i, numBlackPels);
i = rightEdge+1;
numBlackPels = 0;
for (j=jTop; j<jBot; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
if (a<threshold) {
numBlackPels++;
}
}
printf("%d: numBlack=%d\n", i, numBlackPels);
*/
///end temp code
if ((numBlackLines >=1) && (numBlackLines<width3p)) {
return rightEdge;
} else {
debugstr("COULD NOT FIND BINDING, using strongest edge!\n");
return bindingEdge;
}
return 1; //TODO: return error code on failure
}
/// FindBindingEdge3()
///____________________________________________________________________________
l_int32 FindBindingEdge3(PIX *pixg,
l_int32 rotDir,
l_uint32 topEdge,
l_uint32 bottomEdge,
float *skew,
l_uint32 *thesh)
{
//Currently, we can only do right-hand leafs
assert((1 == rotDir) || (-1 == rotDir));
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
l_uint32 width10 = (l_uint32)(w * 0.10);
//kernel has height of (h/2 +/- h*hPercent/2)
l_uint32 kernelHeight10 = (l_uint32)(0.10*(bottomEdge-topEdge));
//l_uint32 jTop = (l_uint32)((1-kKernelHeight)*0.5*h);
//l_uint32 jBot = (l_uint32)((1+kKernelHeight)*0.5*h);
//l_uint32 jTop = topEdge+kernelHeight10;
//l_uint32 jBot = bottomEdge-kernelHeight10;
//we sometimes pick up an picture edge on teh opposing page..
//extending jTop and jBot allows us to hopefully get some page margin in the calculation
l_uint32 jTop = 0;
l_uint32 jBot = h-1;
// Find the strong edge, which should be one of the two sides of the binding
// Rotate the image to maximize SAD
l_uint32 left, right;
if (1 == rotDir) {
left = 0;
right = width10;
} else {
left = w - width10;
right = w - 1;
}
l_int32 bindingEdge;// = -1;
l_uint32 bindingEdgeDiff;// = 0;
float bindingDelta = 0.0;
CalculateSADcol(pixg, left, right, jTop, jBot, &bindingEdge, &bindingEdgeDiff);
float delta;
//0.05 degrees is a good increment for the final search
for (delta=-1.0; delta<=1.0; delta+=0.05) {
if ((delta>-0.01) && (delta<0.01)) { continue;}
PIX *pixt = pixRotate(pixg,
deg2rad*delta,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
l_int32 strongEdge;
l_uint32 strongEdgeDiff;
l_uint32 limitLeft = calcLimitLeft(w,h,delta);
//printf("limitLeft = %d\n", limitLeft);
#if DEBUGMOV
debugmov.angle = delta;
#endif //DEBUGMOV
//l_uint32 left, right;
if (1 == rotDir) {
left = limitLeft;
right = width10;
} else {
left = w - width10;
right = w - limitLeft-1;
}
CalculateSADcol(pixt, left, right, jTop, jBot, &strongEdge, &strongEdgeDiff);
//printf("delta=%f, strongest edge of gutter is at i=%d with diff=%d, w,h=(%d,%d)\n", delta, strongEdge, strongEdgeDiff, w, h);
if (strongEdgeDiff > bindingEdgeDiff) {
bindingEdge = strongEdge;
bindingEdgeDiff = strongEdgeDiff;
bindingDelta = delta;
#if DEBUGMOV
debugmov.edgeBinding = bindingEdge;
#endif //DEBUGMOV
}
pixDestroy(&pixt);
}
assert(-1 != bindingEdge); //TODO: handle error
printf("BEST: delta=%f, strongest edge of gutter is at i=%d with diff=%d\n", bindingDelta, bindingEdge, bindingEdgeDiff);
*skew = bindingDelta;
#if DEBUGMOV
debugmov.angle = bindingDelta;
#endif //DEBUGMOV
// Now compute threshold for psudo-bitonalization
// Use midpoint between avg luma of dark and light lines of binding edge
PIX *pixt = pixRotate(pixg,
deg2rad*bindingDelta,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
//pixWrite(DEBUG_IMAGE_DIR "outgray.jpg", pixt, IFF_JFIF_JPEG);
double bindingLumaA = CalculateAvgCol(pixt, bindingEdge, jTop, jBot);
printf("lumaA = %f\n", bindingLumaA);
double bindingLumaB = CalculateAvgCol(pixt, bindingEdge+1, jTop, jBot);
printf("lumaB = %f\n", bindingLumaB);
/*
{
int i;
for (i=bindingEdge-10; i<bindingEdge+10; i++) {
double bindingLuma = CalculateAvgCol(pixt, i, jTop, jBot);
printf("i=%d, luma=%f\n", i, bindingLuma);
}
}
*/
double threshold = (l_uint32)((bindingLumaA + bindingLumaB) / 2);
//TODO: ensure this threshold is reasonable
printf("thesh = %f\n", threshold);
*thesh = (l_uint32)threshold;
l_int32 width3p = (l_int32)(w * 0.03);
l_int32 rightEdge, leftEdge;
l_uint32 numBlackLines = 0;
if (bindingLumaA > bindingLumaB) { //found left edge
l_int32 i;
l_int32 rightLimit = min(bindingEdge+width3p, w);
rightEdge = bindingEdge; //init this something, in case we never break;
leftEdge = bindingEdge;
for (i=bindingEdge+1; i<rightLimit; i++) {
double lumaAvg = CalculateAvgCol(pixt, i, jTop, jBot);
debugstr("i=%d, avg=%f\n", i, lumaAvg);
if (lumaAvg<threshold) {
numBlackLines++;
} else {
rightEdge = i-1;
break;
}
}
printf("numBlackLines = %d\n", numBlackLines);
} else if (bindingLumaA < bindingLumaB) { //found right edge
l_int32 i;
l_int32 leftLimit = bindingEdge-width3p;
rightEdge = bindingEdge;
leftEdge = bindingEdge; //init this something, in case we never break;
if (leftLimit<0) leftLimit = 0;
printf("found right edge of gutter, leftLimit=%d, rightLimit=%d\n", leftLimit, bindingEdge-1);
for (i=bindingEdge-1; i>leftLimit; i--) {
double lumaAvg = CalculateAvgCol(pixt, i, jTop, jBot);
printf("i=%d, avg=%f\n", i, lumaAvg);
if (lumaAvg<threshold) {
numBlackLines++;
} else {
leftEdge = i-1;
break;
}
}
printf("numBlackLines = %d\n", numBlackLines);
} else {
return -1; //TODO: handle error
}
///temp code to calculate some thesholds..
/*
l_uint32 a, j, i = rightEdge;
l_uint32 numBlackPels = 0;
for (j=jTop; j<jBot; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
if (a<threshold) {
numBlackPels++;
}
}
printf("%d: numBlack=%d\n", i, numBlackPels);
i = rightEdge+1;
numBlackPels = 0;
for (j=jTop; j<jBot; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
if (a<threshold) {
numBlackPels++;
}
}
printf("%d: numBlack=%d\n", i, numBlackPels);
*/
///end temp code
printf("rightEdge = %d, bindingEdge = %d\n", rightEdge, bindingEdge);
if ((numBlackLines >=1) && (numBlackLines<width3p)) {
if (1 == rotDir) {
return rightEdge;
} else if (-1 == rotDir) {
return leftEdge;
} else {
assert(0);
}
} else {
debugstr("COULD NOT FIND BINDING, using strongest edge!\n");
return bindingEdge;
}
return 1; //TODO: return error code on failure
}
/// FindOuterEdge()
///____________________________________________________________________________
l_int32 FindOuterEdge(PIX *pixg,
l_int32 rotDir,
float *skew,
l_uint32 *threshOuter)
{
//Currently, we can only do right-hand leafs
assert((1 == rotDir) || (-1 == rotDir));
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
l_uint32 width75 = (l_uint32)(w * 0.75);
l_uint32 width25 = (l_uint32)(w * 0.25);
//kernel has height of (h/2 +/- h*hPercent/2)
l_uint32 jTop = (l_uint32)((1-kKernelHeight)*0.5*h);
l_uint32 jBot = (l_uint32)((1+kKernelHeight)*0.5*h);
l_int32 outerEdge = -1;
l_uint32 outerEdgeDiff = 0;
float outerDelta;
float delta;
//0.05 is a good increment, but too fine for testing
for (delta=-1.0; delta<=1.0; delta+=0.2) {
PIX *pixt = pixRotate(pixg,
deg2rad*delta,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
l_int32 strongEdge;
l_uint32 strongEdgeDiff;
l_uint32 limitLeft = calcLimitLeft(w,h,delta);
//printf("limitLeft = %d\n", limitLeft);
#if DEBUGMOV
debugmov.angle = delta;
#endif //DEBUGMOV
l_uint32 left, right;
if (1 == rotDir) {
left = width75;
right = w-limitLeft-1; //TODO: is w-leftLimit-1 right?
} else if (-1 == rotDir) {
left = limitLeft;
right = width25;
} else {
assert(0);
}
CalculateSADcol(pixt, left, right, jTop, jBot, &strongEdge, &strongEdgeDiff);
//printf("delta=%f, strongest outer edge at i=%d with diff=%d\n", delta, strongEdge, strongEdgeDiff);
if (strongEdgeDiff > outerEdgeDiff) {
outerEdge = strongEdge;
outerEdgeDiff = strongEdgeDiff;
outerDelta = delta;
#if DEBUGMOV
debugmov.edgeOuter = outerEdge;
#endif //DEBUGMOV
}
pixDestroy(&pixt);
}
assert(-1 != outerEdge); //TODO: handle error
printf("BEST: delta=%f, outer edge is at i=%d with diff=%d\n", outerDelta, outerEdge, outerEdgeDiff);
//calculate threshold
//l_uint32 jTop = (l_uint32)((1-kKernelHeight)*0.5*h);
//l_uint32 jBot = (l_uint32)((1+kKernelHeight)*0.5*h);
PIX *pixt = pixRotate(pixg,
deg2rad*outerDelta,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
double bindingLumaA = CalculateAvgCol(pixt, outerEdge, jTop, jBot);
printf("outer lumaA = %f\n", bindingLumaA);
double bindingLumaB = CalculateAvgCol(pixt, outerEdge+1, jTop, jBot);
printf("outer lumaB = %f\n", bindingLumaB);
double threshold = (l_uint32)((bindingLumaA + bindingLumaB) / 2);
//TODO: ensure this threshold is reasonable
printf("outer thesh = %f\n", threshold);
*threshOuter = (l_uint32)threshold;
pixDestroy(&pixt);
*skew = outerDelta;
return outerEdge;
}
/// FindHorizontalEdge()
///____________________________________________________________________________
l_uint32 FindHorizontalEdge(PIX *pixg,
l_int32 rotDir,
l_uint32 bindingEdge,
bool whichEdge,
float *skew,
l_uint32 *threshOut)
{
//Although we assume the page is centered vertically, we can't assume that
//the page is centered horizontally.
//Currently, we can only do right-hand leafs
assert((1 == rotDir) || (-1 == rotDir));
//start at bindingEdge, and go 25% into the image.
//TODO: generalize this to support both left and right hand leafs
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
l_uint32 width50 = (l_uint32)(w * 0.5);
l_uint32 height25 = (l_uint32)(h * 0.25);
l_uint32 left, right;
if (1 == rotDir) {
left = bindingEdge;
right = bindingEdge+width50;
} else if (-1 == rotDir) {
left = bindingEdge-width50;
right = bindingEdge;
} else {
assert(0);
}
l_int32 strongEdge;
l_uint32 strongEdgeDiff;
l_int32 topEdge = -1; //TODO: generalize - this should be horizEdge
l_uint32 topEdgeDiff = 0;
float topDelta;
float delta;
for (delta=-1.0; delta<=1.0; delta+=0.05) {
PIX *pixt = pixRotate(pixg,
deg2rad*delta,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
l_int32 strongEdge;
l_uint32 strongEdgeDiff;
l_uint32 topLimit = calcLimitTop(w,h,delta);
l_uint32 top, bottom;
if (0 == whichEdge) { //top Edge
top = topLimit;
bottom = height25;
} else {
bottom = h-topLimit-1; //TODO: is the -1 right?
top = h-height25;
}
CalculateSADrow(pixt, left, right, top, bottom, &strongEdge, &strongEdgeDiff);
//printf("delta=%f, strongest top edge is at i=%d with diff=%d\n", delta, strongEdge, strongEdgeDiff);
if (strongEdgeDiff > topEdgeDiff) {
topEdge = strongEdge;
topEdgeDiff = strongEdgeDiff;
topDelta = delta;
}
pixDestroy(&pixt);
}
//calculate threshold
PIX *pixt = pixRotate(pixg,
deg2rad*topDelta,
L_ROTATE_AREA_MAP,
L_BRING_IN_BLACK,0,0);
double bindingLumaA = CalculateAvgRow(pixt, topEdge, left, right);
printf("horiz%d lumaA = %f\n", whichEdge, bindingLumaA);
double bindingLumaB = CalculateAvgRow(pixt, topEdge+1, left, right);
printf("horiz%d lumaB = %f\n", whichEdge, bindingLumaB);
*threshOut = (l_uint32)((bindingLumaA + bindingLumaB) / 2);
//TODO: ensure this threshold is reasonable
printf("horiz%d thesh = %d\n", whichEdge, *threshOut);
pixDestroy(&pixt);
assert(-1 != topEdge); //TODO: handle error
printf("BEST Horiz: delta=%f at j=%d with diff=%d\n", topDelta, topEdge, topEdgeDiff);
*skew = topDelta;
return topEdge;
}
/// CalculateDifferentialSquareSum()
///____________________________________________________________________________
double CalculateDifferentialSquareSum(PIX *pixg,
l_uint32 cL,
l_uint32 cR,
l_uint32 cT,
l_uint32 cB)
{
l_uint32 i, j;
l_uint32 a, b;
l_uint32 lineSum0, lineSum1;
double sum=0;
//init lineSum0;
lineSum0=0;
for (i=cL; i<=cR; i++) {
l_int32 retval = pixGetPixel(pixg, i, cT, &a);
assert(0 == retval);
lineSum0 += a;
}
for (j=cT+1; j<cB; j++) {
lineSum1 = 0;
for (i=cL; i<=cR; i++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
lineSum1 +=a;
}
double diff = (double)lineSum0 - (double)lineSum1;
sum += (diff*diff);
//printf("\tl0=%d, l1=%d, diff=%f, sum=%f\n", lineSum0, lineSum1, diff, sum);
lineSum0 = lineSum1;
}
return sum;
}
/// Deskew()
/// This works if you pass in a bitonal image, but doesn't work well with grayscale
///____________________________________________________________________________
int Deskew(PIX *pixg,
l_int32 cropL,
l_int32 cropR,
l_int32 cropT,
l_int32 cropB,
double *skew,
double *skewConf)
{
assert(cropR>cropL);
assert(cropB>cropT);
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
l_uint32 width10 = (l_uint32)(w * 0.10);
l_uint32 height10 = (l_uint32)(h * 0.10);
//first, reduce cropbox by 10% to get rid of non-page pixels
printf("before reduce: cL=%d, cR=%d, cT=%d, cB=%d, w=%d, h=%d\n", cropL, cropR, cropT, cropB, w,h);
if ( ((cropR-cropL) > (2*width10)) && ((cropB-cropT) > (2*height10)) ) {
cropL += width10;
cropR -= width10;
cropT += height10;
cropB -= height10;
}