-
Notifications
You must be signed in to change notification settings - Fork 9
/
autoCropCommon.c
1949 lines (1582 loc) · 57.4 KB
/
autoCropCommon.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
#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"
#include <math.h> //for sqrt
#include <assert.h>
#include <float.h> //for DBL_MAX
#include <limits.h> //for INT_MAX
#include "autoCropCommon.h"
#define debugstr printf
//#define debugstr
static const l_float32 deg2rad = 3.1415926535 / 180.;
l_int32 min_int32(l_int32 a, l_int32 b) {
return b + ((a-b) & (a-b)>>31);
}
l_int32 max_int32(l_int32 a, l_int32 b) {
return a - ((a-b) & (a-b)>>31);
}
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);
}
//FIXME: left limit for angle=0 should be zero, returns 1
l_uint32 calcLimitLeft(l_uint32 w, l_uint32 h, l_float32 angle) {
l_uint32 w2 = w>>1;
l_uint32 h2 = h>>1;
l_float32 r = sqrt(w2*w2 + h2*h2);
l_float32 theta = atan2(h2, w2);
l_float32 radang = fabs(angle)*deg2rad;
return w2 - (int)(r*cos(theta + radang));
}
l_uint32 calcLimitTop(l_uint32 w, l_uint32 h, l_float32 angle) {
l_uint32 w2 = w>>1;
l_uint32 h2 = h>>1;
l_float32 r = sqrt(w2*w2 + h2*h2);
l_float32 theta = atan2(h2, w2);
l_float32 radang = fabs(angle)*deg2rad;
return h2 - (int)(r*sin(theta - radang));
}
/// CalculateAvgCol()
/// calculate avg luma of a column
/// last SAD calculation is for row i=right and i=right+1.
///____________________________________________________________________________
double CalculateAvgCol(PIX *pixg,
l_uint32 i,
l_uint32 jTop,
l_uint32 jBot)
{
l_uint32 acc=0;
l_uint32 a, j;
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
assert(i>=0);
assert(i<w);
//kernel has height of (h/2 +/- h*hPercent/2)
//l_uint32 jTop = (l_uint32)((1-hPercent)*0.5*h);
//l_uint32 jBot = (l_uint32)((1+hPercent)*0.5*h);
//printf("jTop/Bot is %d/%d\n", jTop, jBot);
acc=0;
for (j=jTop; j<jBot; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
acc += a;
}
//printf("%d \n", acc);
double avg = acc;
avg /= (jBot-jTop);
return avg;
}
/// CalculateAvgRow()
/// calculate avg luma of a row
///____________________________________________________________________________
double CalculateAvgRow(PIX *pixg,
l_uint32 j,
l_uint32 iLeft,
l_uint32 iRight)
{
l_uint32 acc=0;
l_uint32 a, i;
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
assert(j>=0);
assert(j<h);
acc=0;
for (i=iLeft; i<iRight; i++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
acc += a;
}
//printf("%d \n", acc);
double avg = acc;
avg /= (iRight-iLeft);
return avg;
}
/// CalculateVarRow()
/// calculate luma variance of a row
///____________________________________________________________________________
double CalculateVarRow(PIX *pixg,
l_uint32 j,
l_uint32 iLeft,
l_uint32 iRight)
{
l_uint32 a, i;
l_uint32 h = pixGetHeight( pixg );
assert(j>=0);
assert(j<h);
double avg = CalculateAvgRow(pixg, j, iLeft, iRight);
double var = 0;
for (i=iLeft; i<iRight; i++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
double diff = avg-a;
var += (diff * diff);
}
return var;
}
/// CalculateVarCol()
/// calculate luma variance of a column
///____________________________________________________________________________
double CalculateVarCol(PIX *pixg,
l_uint32 i,
l_uint32 jTop,
l_uint32 jBot)
{
l_uint32 a, j;
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
assert(i>=0);
assert(i<w);
double avg = CalculateAvgCol(pixg, i, jTop, jBot);
double var = 0;
for (j=jTop; j<jBot; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
double diff = avg-a;
var += (diff * diff);
}
return var;
}
/// CalculateSADcol()
/// calculate sum of absolute differences of two rows of adjacent columns
/// last SAD calculation is for row i=right and i=right+1.
///____________________________________________________________________________
l_uint32 CalculateSADcol(PIX *pixg,
l_uint32 left,
l_uint32 right,
l_uint32 jTop,
l_uint32 jBot,
l_int32 *reti,
l_uint32 *retDiff
)
{
l_uint32 i, j;
l_uint32 acc=0;
l_uint32 a,b;
l_uint32 maxDiff=0;
l_int32 maxi=-1;
l_uint32 w = pixGetWidth( pixg );
l_uint32 h = pixGetHeight( pixg );
assert(left>=0);
assert(left<right);
assert(right<w);
//kernel has height of (h/2 +/- h*hPercent/2)
//l_uint32 jTop = (l_uint32)((1-hPercent)*0.5*h);
//l_uint32 jBot = (l_uint32)((1+hPercent)*0.5*h);
//printf("jTop/Bot is %d/%d\n", jTop, jBot);
for (i=left; i<right; i++) {
//printf("%d: ", i);
acc=0;
for (j=jTop; j<jBot; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
retval = pixGetPixel(pixg, i+1, j, &b);
assert(0 == retval);
//printf("%d ", val);
acc += (abs(a-b));
//printf("acc: %d\n", acc);
}
//printf("%d \n", acc);
if (acc > maxDiff) {
maxi=i;
maxDiff = acc;
}
#if DEBUGMOV
{
debugmov.framenum++;
char cmd[512];
int ret = snprintf(cmd, 512, "convert " DEBUG_IMAGE_DIR "debugmov/smallgray.jpg -background black -rotate %f -pointsize 18 -fill yellow -annotate 0x0+10+20 '%s' -fill red -annotate 0x0+10+40 'angle = %0.2f' -draw 'line %d,%d %d,%d' -fill green -draw 'line %d,%d %d,%d' -draw 'line %d,%d %d,%d' \"%s/frames/%06d.jpg\"", debugmov.angle, debugmov.filename, debugmov.angle, i, jTop, i, jBot, debugmov.edgeBinding, jTop, debugmov.edgeBinding, jBot, debugmov.edgeOuter, jTop, debugmov.edgeOuter, jBot, debugmov.outDir, debugmov.framenum);
assert(ret);
printf(cmd);
printf("\n");
ret = system(cmd);
assert(0 == ret);
}
#endif //DEBUGMOV
}
*reti = maxi;
*retDiff = maxDiff;
return (-1 != maxi);
}
/// CalculateSADrow()
/// calculate sum of absolute differences of two rows of adjacent columns
/// last SAD calculation is for row i=right and i=right+1.
///____________________________________________________________________________
l_uint32 CalculateSADrow(PIX *pixg,
l_uint32 left,
l_uint32 right,
l_uint32 top,
l_uint32 bottom,
l_int32 *reti,
l_uint32 *retDiff
)
{
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: ", j);
acc=0;
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);
acc += (abs(a-b));
}
//printf("%d \n", acc);
if (acc > maxDiff) {
maxj=j;
maxDiff = acc;
}
}
*reti = maxj;
*retDiff = maxDiff;
return (-1 != maxj);
}
/// CalculateTreshInitial ()
///____________________________________________________________________________
l_int32 CalculateTreshInitial(PIX *pixg, l_int32 *histmax) {
NUMA *hist = pixGetGrayHistogram(pixg, 1);
assert(NULL != hist);
assert(256 == numaGetCount(hist));
int i;
// for (i=0; i<255; i++) {
// int dummy;
// numaGetIValue(hist, i, &dummy);
// printf("init hist: %d: %d\n", i, dummy);
// }
l_int32 brightestPel;
for (i=255; i>=0; i--) {
float dummy;
numaGetFValue(hist, i, &dummy);
if (dummy > 0) {
brightestPel = i;
break;
}
}
l_int32 darkestPel;
for (i=0; i<=255; i++) {
float dummy;
numaGetFValue(hist, i, &dummy);
if (dummy > 0) {
darkestPel = i;
break;
}
}
l_int32 limit = (brightestPel-darkestPel)/2;
printf("brighestPel=%d, darkestPel=%d, limit=%d\n", brightestPel, darkestPel, limit);
float peak = 0;
l_int32 peaki;
for (i=255; i>=limit; i--) {
float dummy;
numaGetFValue(hist, i, &dummy);
if (dummy > peak) {
peak = dummy;
peaki = i;
}
}
//printf("hist peak at i=%d with val=%f\n", peaki, peak);
l_int32 thresh = -1;
float threshLimit = peak * 0.2;
//printf("thresh limit = %f\n", threshLimit);
for (i=peaki-1; i>0; i--) {
float dummy;
numaGetFValue(hist, i, &dummy);
if (dummy<threshLimit) {
thresh = i;
break;
}
}
if (-1 == thresh) {
thresh = peaki >>1;
}
if (0 == thresh) {
//this could be a plain black img.
thresh = 140;
}
//printf("init thresh at i=%d\n", thresh);
*histmax = peaki;
return thresh;
}
/// CalculateNumBlackPelsRow
///____________________________________________________________________________
l_int32 CalculateNumBlackPelsRow(PIX *pixg, l_int32 j, l_int32 limitL, l_int32 limitR, l_uint32 blackThresh) {
l_int32 numBlackPels = 0;
l_int32 i;
l_uint32 a;
for (i=limitL; i<=limitR; i++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
if (a<blackThresh) {
numBlackPels++;
}
}
return numBlackPels;
}
/// CalculateNumBlackPelsCol
///____________________________________________________________________________
l_int32 CalculateNumBlackPelsCol(PIX *pixg, l_int32 i, l_int32 limitT, l_int32 limitB, l_uint32 blackThresh) {
l_int32 numBlackPels = 0;
l_int32 j;
l_uint32 a;
for (j=limitT; j<=limitB; j++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
if (a<blackThresh) {
numBlackPels++;
}
}
return numBlackPels;
}
/// FindBlackBar()
///____________________________________________________________________________
l_int32 FindBlackBar(PIX *pixg,
l_int32 left,
l_int32 right,
l_int32 h,
l_int32 thresh,
l_int32 *bindingEdgeL,
l_int32 *bindingEdgeR)
{
l_int32 i;
l_int32 gotEdgeL = 0;
l_int32 gotEdgeR = 0;
for (i=left; i<=right; i++) {
//printf("%d (%d): ", i, i*8);
l_int32 numBlackPels = CalculateNumBlackPelsCol(pixg, i, 0, h-1, thresh);
//printf("numBlackPels=%d, h=%d thresh=%d", numBlackPels, h, thresh);
if (numBlackPels == h) {
*bindingEdgeL = i;
gotEdgeL = 1;
//printf("FOUND!\n");
break;
}
//printf("\n");
}
for (i=right; i>=left; i--) {
//printf("%d: ", i);
l_int32 numBlackPels = CalculateNumBlackPelsCol(pixg, i, 0, h-1, thresh);
//printf("numBlackPels=%d, h=%d ", numBlackPels, h);
if (numBlackPels == h) {
*bindingEdgeR = i;
gotEdgeR = 1;
//printf("FOUND!\n");
break;
}
//printf("\n");
}
if (!(gotEdgeL & gotEdgeR)) {
return -1;
//assert(0);
}
return 1;
}
/// FindBlackBarAndThresh()
///____________________________________________________________________________
void FindBlackBarAndThresh(PIX *pixg,
l_int32 left,
l_int32 right,
l_int32 h,
l_int32 *barEdgeL,
l_int32 *barEdgeR,
l_int32 *barThresh)
{
pixWrite("findblackbar.jpg", pixg, IFF_JFIF_JPEG);
l_int32 histmax;
l_int32 darkThresh = CalculateTreshInitial(pixg, &histmax);
l_int32 thresh;
for (thresh = darkThresh; thresh<histmax; thresh++) {
l_int32 blackBarL, blackBarR;
printf("thresh=%d ", thresh);
l_int32 retval = FindBlackBar(pixg, left, right, h, thresh, &blackBarL, &blackBarR);
if (-1 == retval) continue;
l_int32 barWidth = blackBarR - blackBarL;
printf("L=%d, R=%d, W=%d\n", blackBarL, blackBarR, barWidth);
if (barWidth >= 1) {
*barEdgeL = blackBarL;
*barEdgeR = blackBarR;
*barThresh = thresh;
return;
}
}
// float delta;
// //0.05 degrees is a good increment for the final search
// for (delta=-1.0; delta<=1.0; delta+=0.05) {
// printf("trying delta=%f\n", delta);
// 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 blackBarL, blackBarR;
// l_int32 retval = FindBlackBar(pixg, left, right, h, thresh, &blackBarL, &blackBarR);
// if (-1 == retval) continue;
//
// l_int32 barWidth = blackBarR - blackBarL;
// printf("delta=%f, L=%d, R=%d, W=%d\n", delta, blackBarL, blackBarR, barWidth);
// if (barWidth >= 1) {
// *barEdgeL = blackBarL;
// *barEdgeR = blackBarR;
// *barThresh = thresh;
// return;
// }
//
// pixDestroy(&pixt);
// }
assert(0);
}
/// ExpandRowOrCol()
///____________________________________________________________________________
void ExpandRowOrCol(l_int32 numPels, l_int32 limitMin, l_int32 limitMax, l_int32 *min, l_int32 *max) {
l_int32 localMin = *min;
l_int32 localMax = *max;
if ((localMin - numPels) < limitMin) {
*min = limitMin;
} else {
*min = localMin - numPels;
}
if ((localMax+numPels) > limitMax) {
*max = limitMax;
} else {
*max = localMax + numPels;
}
}
/// FindBindingEdge2()
///____________________________________________________________________________
l_int32 FindBindingEdge2(PIX *pixg,
l_int32 rotDir,
l_uint32 topEdge,
l_uint32 bottomEdge,
float *skew,
l_uint32 *thesh,
l_int32 textBlockL,
l_int32 textBlockR)
{
//pixWrite("findbinding.jpg", pixg, IFF_JFIF_JPEG);
//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;
// 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;
if (-1 == textBlockL) {
right = width10;
} else {
right = textBlockL;
}
} else {
if (-1 == textBlockR) {
left = w - width10;
} else {
left = textBlockR;
}
right = w - 1;
}
l_int32 bindingEdge;// = -1;
l_uint32 bindingEdgeDiff;// = 0;
float bindingDelta = 0.0;
printf("left = %d, right=%d, textBlockR = %d, width = %d, width10=%d\n", left, right, textBlockR, w, width10);
l_int32 blackBarL, blackBarR;
l_int32 histmax;
l_int32 darkThresh; // = CalculateTreshInitial(pixg, &histmax);
//FindBlackBar(pixg, left, right, h, darkThresh, &blackBarL, &blackBarR);
FindBlackBarAndThresh(pixg, left, right, h, &blackBarL, &blackBarR, &darkThresh);
printf("init blackBar L=%d, R=%d, width=%d, thresh=%d\n", blackBarL, blackBarR, blackBarR-blackBarL, darkThresh);
//CalculateSADcol(pixg, left, right, jTop, jBot, &bindingEdge, &bindingEdgeDiff);
CalculateSADcol(pixg, blackBarL, blackBarR, jTop, jBot, &bindingEdge, &bindingEdgeDiff);
//printf("init bindingEdge=%d, diff=%d\n", bindingEdge*8, 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);
//printf("textBlockL=%d, textBlockR=%d, width=%d, limitLeft=%d\n", textBlockL, textBlockR, w, limitLeft);
#if DEBUGMOV
debugmov.angle = delta;
#endif //DEBUGMOV
//l_uint32 left, right;
if (1 == rotDir) {
left = limitLeft;
if (-1 == textBlockL) {
right = width10;
} else {
if (textBlockL <= limitLeft) {
right = width10;
} else {
right = textBlockL;
}
}
} else {
if (-1 == textBlockR) {
left = w - width10;
} else {
if (textBlockR >= w-limitLeft-1) { /*fix for reportofsuperint196566leen leaf 8*/
left = w- width10;
} else {
left = textBlockR;
}
}
right = w - limitLeft-1;
}
FindBlackBar(pixg, left, right, h, darkThresh, &blackBarL, &blackBarR);
ExpandRowOrCol(1, left, right, &blackBarL, &blackBarR); /*fix for reportofsuperint1314leen leaf 37*/
//printf("blackBar L=%d, R=%d, width=%d\n", blackBarL, blackBarR, blackBarR-blackBarL);
//CalculateSADcol(pixt, left, right, jTop, jBot, &strongEdge, &strongEdgeDiff);
CalculateSADcol(pixt, blackBarL, blackBarR, 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;
//printf("setting best delta to %f\n", bindingDelta);
#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 = bindingEdge+width3p;
rightLimit = min_int32(rightLimit, w-1); /*fix for reportofsuperint196566leen leaf 34*/
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);
printf("i=%d, avg=%f\n", i, lumaAvg);
if (lumaAvg<threshold) {
rightEdge = i; /*fix for reportofsuperint196566leen leaf 34*/
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;
leftLimit = max_int32(leftLimit, 0);
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) {
leftEdge = i;
numBlackLines++;
} else {
leftEdge = i-1; //should this be 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, leftEdge = %d\n", rightEdge, leftEdge);
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
}
/// FindBindingUsingBlackBar()
///____________________________________________________________________________
l_int32 FindBindingUsingBlackBar(PIX *pixg,
l_int32 rotDir,
l_int32 topEdge,
l_int32 bottomEdge,
l_int32 textBlockL,
l_int32 textBlockR)
{
//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);
l_int32 histmax;
l_int32 darkThresh = CalculateTreshInitial(pixg, &histmax);
l_int32 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_int32 jTop = topEdge+kernelHeight10;
l_int32 jBot = bottomEdge-kernelHeight10;
l_uint32 left, right;
if (1 == rotDir) {
left = 0;
if (-1 == textBlockL) {
right = width10;
} else {
right = textBlockL;
}
} else {
if (-1 == textBlockR) {
left = w - width10;
} else {
left = textBlockR;
}
right = w - 1;
}
l_int32 bindingEdge;// = -1;
l_uint32 bindingEdgeDiff;// = 0;
l_int32 blackBarL, blackBarR;// = -1;
l_int32 bindingWidth;// = 0;
float bindingDelta = 0.0;
FindBlackBar(pixg, left, right, h, darkThresh, &blackBarL, &blackBarR);
printf("init blackBar L=%d, R=%d, width=%d\n", blackBarL, blackBarR, blackBarR-blackBarL);
CalculateSADcol(pixg, blackBarL, blackBarR, jTop, jBot, &bindingEdge, &bindingEdgeDiff);
printf("init bindingEdge=%d, diff=%d\n", bindingEdge, bindingEdgeDiff);
//if (1 == rotDir) {
// return bindingEdgeL;
//} else {
// return bindingEdgeR;
//}
return 1;
}
/// ConvertToGray()
///____________________________________________________________________________
PIX* ConvertToGray(PIX *pix, l_int32 *grayChannel) {
PIX *pixg;
l_int32 maxchannel;
l_int32 useSingleChannelForGray = 0;
NUMA *histR, *histG, *histB;
l_int32 ret = pixGetColorHistogram(pix, 1, &histR, &histG, &histB);
assert(0 == ret);
l_float32 maxval;
l_int32 maxloc[3];
ret = numaGetMax(histR, &maxval, &maxloc[0]);
assert(0 == ret);
printf("red peak at %d with val %f\n", maxloc[0], maxval);
ret = numaGetMax(histG, &maxval, &maxloc[1]);
assert(0 == ret);
printf("green peak at %d with val %f\n", maxloc[1], maxval);
ret = numaGetMax(histB, &maxval, &maxloc[2]);
assert(0 == ret);
printf("blue peak at %d with val %f\n", maxloc[2], maxval);
l_int32 i;
l_int32 max=0, secondmax=0;
for (i=0; i<3; i++) {
if (maxloc[i] > max) {
max = maxloc[i];
maxchannel = i;
} else if (maxloc[i] > secondmax) {
secondmax = maxloc[i];
}
}
printf("max = %d, secondmax=%d\n", max, secondmax);
if (max > (secondmax*2)) {
printf("grayMode: SINGLE-channel, channel=%d\n", maxchannel);
useSingleChannelForGray = 1;
} else {
printf("grayMode: three-channel\n");
}
if (useSingleChannelForGray) {
pixg = pixConvertRGBToGray (pix, (0==maxchannel), (1==maxchannel), (2==maxchannel));
*grayChannel = maxchannel;
} else {
pixg = pixConvertRGBToGray (pix, 0.30, 0.60, 0.10);
*grayChannel = kGrayModeThreeChannel;
}
return pixg;
}
/// RemoveBackgroundTop()
///____________________________________________________________________________
l_int32 RemoveBackgroundTop(PIX *pixg, l_int32 rotDir, l_int32 initialBlackThresh) {
l_uint32 w = pixGetWidth(pixg);
l_uint32 h = pixGetHeight(pixg);
l_uint32 a;
l_uint32 limitL, limitR, limitB;
if (1 == rotDir) {
limitL = (l_uint32)(0.20*w);
limitR = w-1;
} else if (-1 == rotDir) {
limitL = 1;
limitR = (l_uint32)(0.80*w);
} else if (0 == rotDir) {
limitL = 1;
limitR = w-1;
} else {
assert(0);
}
limitB = l_uint32(0.80*h);
//printf("T: limitL=%d, limitR=%d, limitB=%d\n", limitL, limitR, limitB);
//l_int32 initialBlackThresh = 140;
l_uint32 numBlackRequired = (l_uint32)(0.90*(limitR-limitL));
l_uint32 i, j;
for(j=0; j<=limitB; j++) {
l_uint32 numBlackPels = 0;
for (i=limitL; i<=limitR; i++) {
l_int32 retval = pixGetPixel(pixg, i, j, &a);
assert(0 == retval);
if (a<initialBlackThresh) {
numBlackPels++;
}
}
//printf("T %d: numBlack=%d\n", j, numBlackPels);
if (numBlackPels<numBlackRequired) {
//printf("break!\n");
return j;
}
}
return 0;
}
/// RemoveBackgroundBottom()
///____________________________________________________________________________
l_int32 RemoveBackgroundBottom(PIX *pixg, l_int32 rotDir, l_int32 initialBlackThresh) {
l_uint32 w = pixGetWidth(pixg);
l_uint32 h = pixGetHeight(pixg);
l_uint32 a;
l_int32 limitL, limitR, limitT;
if (1 == rotDir) {
limitL = (l_uint32)(w*0.20);