-
Notifications
You must be signed in to change notification settings - Fork 380
/
adaptmap.c
2944 lines (2699 loc) · 109 KB
/
adaptmap.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) 2001 Leptonica. All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the following
- disclaimer in the documentation and/or other materials
- provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*====================================================================*/
/*!
* \file adaptmap.c
* <pre>
*
* -------------------------------------------------------------------
*
* Image binarization algorithms are found in:
* grayquant.c: standard, simple, general grayscale quantization
* adaptmap.c: local adaptive; mostly gray-to-gray in preparation
* for binarization
* binarize.c: special binarization methods, locally adaptive.
*
* -------------------------------------------------------------------
*
* Clean background to white using background normalization
* PIX *pixCleanBackgroundToWhite()
*
* Adaptive background normalization (top-level functions)
* PIX *pixBackgroundNormSimple() 8 and 32 bpp
* PIX *pixBackgroundNorm() 8 and 32 bpp
* PIX *pixBackgroundNormMorph() 8 and 32 bpp
*
* Arrays of inverted background values for normalization (16 bpp)
* l_int32 pixBackgroundNormGrayArray() 8 bpp input
* l_int32 pixBackgroundNormRGBArrays() 32 bpp input
* l_int32 pixBackgroundNormGrayArrayMorph() 8 bpp input
* l_int32 pixBackgroundNormRGBArraysMorph() 32 bpp input
*
* Measurement of local background
* l_int32 pixGetBackgroundGrayMap() 8 bpp
* l_int32 pixGetBackgroundRGBMap() 32 bpp
* l_int32 pixGetBackgroundGrayMapMorph() 8 bpp
* l_int32 pixGetBackgroundRGBMapMorph() 32 bpp
* l_int32 pixFillMapHoles()
* PIX *pixExtendByReplication() 8 bpp
* l_int32 pixSmoothConnectedRegions() 8 bpp
*
* Measurement of local foreground
* l_int32 pixGetForegroundGrayMap() 8 bpp
*
* Generate inverted background map for each component
* PIX *pixGetInvBackgroundMap() 16 bpp
*
* Apply inverse background map to image
* PIX *pixApplyInvBackgroundGrayMap() 8 bpp
* PIX *pixApplyInvBackgroundRGBMap() 32 bpp
*
* Apply variable map
* PIX *pixApplyVariableGrayMap() 8 bpp
*
* Non-adaptive (global) mapping
* PIX *pixGlobalNormRGB() 32 bpp or cmapped
* PIX *pixGlobalNormNoSatRGB() 32 bpp
*
* Adaptive threshold spread normalization
* l_int32 pixThresholdSpreadNorm() 8 bpp
*
* Adaptive background normalization (flexible adaptaption)
* PIX *pixBackgroundNormFlex() 8 bpp
*
* Adaptive contrast normalization
* PIX *pixContrastNorm() 8 bpp
* l_int32 pixMinMaxTiles()
* l_int32 pixSetLowContrast()
* PIX *pixLinearTRCTiled()
* static l_int32 *iaaGetLinearTRC()
*
* Background normalization is done by generating a reduced map (or set
* of maps) representing the estimated background value of the
* input image, and using this to shift the pixel values so that
* this background value is set to some constant value.
*
* Specifically, normalization has 3 steps:
* (1) Generate a background map at a reduced scale.
* (2) Make the array of inverted background values by inverting
* the map. The result is an array of local multiplicative factors.
* (3) Apply this inverse background map to the image
*
* The inverse background arrays can be generated in two different ways here:
* (1) Remove the 'foreground' pixels and average over the remaining
* pixels in each tile. Propagate values into tiles where
* values have not been assigned, either because there was not
* enough background in the tile or because the tile is covered
* by a foreground region described by an image mask.
* After the background map is made, the inverse map is generated by
* smoothing over some number of adjacent tiles
* (block convolution) and then inverting.
* (2) Remove the foreground pixels using a morphological closing
* on a subsampled version of the image. Propagate values
* into pixels covered by an optional image mask. Invert the
* background map without preconditioning by convolutional smoothing.
*
* Other methods for adaptively normalizing the image are also given here.
*
* (1) pixThresholdSpreadNorm() computes a local threshold over the image
* and normalizes the input pixel values so that this computed threshold
* is a constant across the entire image.
*
* (2) pixContrastNorm() computes and applies a local TRC so that the
* local dynamic range is expanded to the full 8 bits, where the
* darkest pixels are mapped to 0 and the lightest to 255. This is
* useful for improving the appearance of pages with very light
* foreground or very dark background, and where the local TRC
* function doesn't change rapidly with position.
* </pre>
*/
#ifdef HAVE_CONFIG_H
#include <config_auto.h>
#endif /* HAVE_CONFIG_H */
#include "allheaders.h"
/* Default input parameters for pixBackgroundNormSimple()
* Notes:
* (1) mincount must never exceed the tile area (width * height)
* (2) bgval must be sufficiently below 255 to avoid accidental
* saturation; otherwise it should be large to avoid
* shrinking the dynamic range
* (3) results should otherwise not be sensitive to these values
*/
static const l_int32 DefaultTileWidth = 10; /*!< default tile width */
static const l_int32 DefaultTileHeight = 15; /*!< default tile height */
static const l_int32 DefaultFgThreshold = 60; /*!< default fg threshold */
static const l_int32 DefaultMinCount = 40; /*!< default minimum count */
static const l_int32 DefaultBgVal = 200; /*!< default bg value */
static const l_int32 DefaultXSmoothSize = 2; /*!< default x smooth size */
static const l_int32 DefaultYSmoothSize = 1; /*!< default y smooth size */
static l_int32 *iaaGetLinearTRC(l_int32 **iaa, l_int32 diff);
#ifndef NO_CONSOLE_IO
#define DEBUG_GLOBAL 0 /*!< set to 1 to debug pixGlobalNormNoSatRGB() */
#endif /* ~NO_CONSOLE_IO */
/*------------------------------------------------------------------*
* Clean background to white using background normalization *
*------------------------------------------------------------------*/
/*!
* \brief pixCleanBackgroundToWhite()
*
* \param[in] pixs 8 bpp grayscale or 32 bpp rgb
* \param[in] pixim [optional] 1 bpp 'image' mask; can be null
* \param[in] pixg [optional] 8 bpp grayscale version; can be null
* \param[in] gamma gamma correction; must be > 0.0; typically ~1.0
* \param[in] blackval dark value to set to black (0)
* \param[in] whiteval light value to set to white (255)
* \return pixd 8 bpp or 32 bpp rgb, or NULL on error
*
* <pre>
* Notes:
* (1) This is a simplified interface for cleaning an image.
* For comparison, see pixAdaptThresholdToBinaryGen().
* (2) The suggested default values for the input parameters are:
* gamma: 1.0 (reduce this to increase the contrast; e.g.,
* for light text)
* blackval 70 (a bit more than 60)
* whiteval 190 (a bit less than 200)
* </pre>
*/
PIX *
pixCleanBackgroundToWhite(PIX *pixs,
PIX *pixim,
PIX *pixg,
l_float32 gamma,
l_int32 blackval,
l_int32 whiteval)
{
l_int32 d;
PIX *pixd;
PROCNAME("pixCleanBackgroundToWhite");
if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
d = pixGetDepth(pixs);
if (d != 8 && d != 32)
return (PIX *)ERROR_PTR("depth not 8 or 32", procName, NULL);
pixd = pixBackgroundNormSimple(pixs, pixim, pixg);
if (!pixd)
return (PIX *)ERROR_PTR("background norm failedd", procName, NULL);
pixGammaTRC(pixd, pixd, gamma, blackval, whiteval);
return pixd;
}
/*------------------------------------------------------------------*
* Adaptive background normalization *
*------------------------------------------------------------------*/
/*!
* \brief pixBackgroundNormSimple()
*
* \param[in] pixs 8 bpp grayscale or 32 bpp rgb
* \param[in] pixim [optional] 1 bpp 'image' mask; can be null
* \param[in] pixg [optional] 8 bpp grayscale version; can be null
* \return pixd 8 bpp or 32 bpp rgb, or NULL on error
*
* <pre>
* Notes:
* (1) This is a simplified interface to pixBackgroundNorm(),
* where seven parameters are defaulted.
* (2) The input image is either grayscale or rgb.
* (3) See pixBackgroundNorm() for usage and function.
* </pre>
*/
PIX *
pixBackgroundNormSimple(PIX *pixs,
PIX *pixim,
PIX *pixg)
{
return pixBackgroundNorm(pixs, pixim, pixg,
DefaultTileWidth, DefaultTileHeight,
DefaultFgThreshold, DefaultMinCount,
DefaultBgVal, DefaultXSmoothSize,
DefaultYSmoothSize);
}
/*!
* \brief pixBackgroundNorm()
*
* \param[in] pixs 8 bpp grayscale or 32 bpp rgb
* \param[in] pixim [optional] 1 bpp 'image' mask; can be null
* \param[in] pixg [optional] 8 bpp grayscale version; can be null
* \param[in] sx, sy tile size in pixels
* \param[in] thresh threshold for determining foreground
* \param[in] mincount min threshold on counts in a tile
* \param[in] bgval target bg val; typ. > 128
* \param[in] smoothx half-width of block convolution kernel width
* \param[in] smoothy half-width of block convolution kernel height
* \return pixd 8 bpp or 32 bpp rgb, or NULL on error
*
* <pre>
* Notes:
* (1) This is a top-level interface for normalizing the image intensity
* by mapping the image so that the background is near the input
* value 'bgval'.
* (2) The input image is either grayscale or rgb.
* (3) For each component in the input image, the background value
* in each tile is estimated using the values in the tile that
* are not part of the foreground, where the foreground is
* determined by the input 'thresh' argument.
* (4) An optional binary mask can be specified, with the foreground
* pixels typically over image regions. The resulting background
* map values will be determined by surrounding pixels that are
* not under the mask foreground. The origin (0,0) of this mask
* is assumed to be aligned with the origin of the input image.
* This binary mask must not fully cover pixs, because then there
* will be no pixels in the input image available to compute
* the background.
* (5) An optional grayscale version of the input pixs can be supplied.
* The only reason to do this is if the input is RGB and this
* grayscale version can be used elsewhere. If the input is RGB
* and this is not supplied, it is made internally using only
* the green component, and destroyed after use.
* (6) The dimensions of the pixel tile (sx, sy) give the amount by
* by which the map is reduced in size from the input image.
* (7) The threshold is used to binarize the input image, in order to
* locate the foreground components. If this is set too low,
* some actual foreground may be used to determine the maps;
* if set too high, there may not be enough background
* to determine the map values accurately. Typically, it's
* better to err by setting the threshold too high.
* (8) A 'mincount' threshold is a minimum count of pixels in a
* tile for which a background reading is made, in order for that
* pixel in the map to be valid. This number should perhaps be
* at least 1/3 the size of the tile.
* (9) A 'bgval' target background value for the normalized image. This
* should be at least 128. If set too close to 255, some
* clipping will occur in the result.
* (10) Two factors, 'smoothx' and 'smoothy', are input for smoothing
* the map. Each low-pass filter kernel dimension is
* is 2 * (smoothing factor) + 1, so a
* value of 0 means no smoothing. A value of 1 or 2 is recommended.
* </pre>
*/
PIX *
pixBackgroundNorm(PIX *pixs,
PIX *pixim,
PIX *pixg,
l_int32 sx,
l_int32 sy,
l_int32 thresh,
l_int32 mincount,
l_int32 bgval,
l_int32 smoothx,
l_int32 smoothy)
{
l_int32 d, allfg;
PIX *pixm, *pixmi, *pixd;
PIX *pixmr, *pixmg, *pixmb, *pixmri, *pixmgi, *pixmbi;
PROCNAME("pixBackgroundNorm");
if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
d = pixGetDepth(pixs);
if (d != 8 && d != 32)
return (PIX *)ERROR_PTR("pixs not 8 or 32 bpp", procName, NULL);
if (sx < 4 || sy < 4)
return (PIX *)ERROR_PTR("sx and sy must be >= 4", procName, NULL);
if (mincount > sx * sy) {
L_WARNING("mincount too large for tile size\n", procName);
mincount = (sx * sy) / 3;
}
/* If pixim exists, verify that it is not all foreground. */
if (pixim) {
pixInvert(pixim, pixim);
pixZero(pixim, &allfg);
pixInvert(pixim, pixim);
if (allfg)
return (PIX *)ERROR_PTR("pixim all foreground", procName, NULL);
}
pixd = NULL;
if (d == 8) {
pixm = NULL;
pixGetBackgroundGrayMap(pixs, pixim, sx, sy, thresh, mincount, &pixm);
if (!pixm) {
L_WARNING("map not made; return a copy of the source\n", procName);
return pixCopy(NULL, pixs);
}
pixmi = pixGetInvBackgroundMap(pixm, bgval, smoothx, smoothy);
if (!pixmi) {
L_WARNING("pixmi not made; return a copy of source\n", procName);
pixDestroy(&pixm);
return pixCopy(NULL, pixs);
} else {
pixd = pixApplyInvBackgroundGrayMap(pixs, pixmi, sx, sy);
}
pixDestroy(&pixm);
pixDestroy(&pixmi);
}
else {
pixmr = pixmg = pixmb = NULL;
pixGetBackgroundRGBMap(pixs, pixim, pixg, sx, sy, thresh,
mincount, &pixmr, &pixmg, &pixmb);
if (!pixmr || !pixmg || !pixmb) {
pixDestroy(&pixmr);
pixDestroy(&pixmg);
pixDestroy(&pixmb);
L_WARNING("map not made; return a copy of the source\n", procName);
return pixCopy(NULL, pixs);
}
pixmri = pixGetInvBackgroundMap(pixmr, bgval, smoothx, smoothy);
pixmgi = pixGetInvBackgroundMap(pixmg, bgval, smoothx, smoothy);
pixmbi = pixGetInvBackgroundMap(pixmb, bgval, smoothx, smoothy);
if (!pixmri || !pixmgi || !pixmbi) {
L_WARNING("not all pixm*i are made; return src copy\n", procName);
pixd = pixCopy(NULL, pixs);
} else {
pixd = pixApplyInvBackgroundRGBMap(pixs, pixmri, pixmgi, pixmbi,
sx, sy);
}
pixDestroy(&pixmr);
pixDestroy(&pixmg);
pixDestroy(&pixmb);
pixDestroy(&pixmri);
pixDestroy(&pixmgi);
pixDestroy(&pixmbi);
}
if (!pixd)
ERROR_PTR("pixd not made", procName, NULL);
pixCopyResolution(pixd, pixs);
return pixd;
}
/*!
* \brief pixBackgroundNormMorph()
*
* \param[in] pixs 8 bpp grayscale or 32 bpp rgb
* \param[in] pixim [optional] 1 bpp 'image' mask; can be null
* \param[in] reduction at which morph closings are done; between 2 and 16
* \param[in] size of square Sel for the closing; use an odd number
* \param[in] bgval target bg val; typ. > 128
* \return pixd 8 bpp, or NULL on error
*
* <pre>
* Notes:
* (1) This is a top-level interface for normalizing the image intensity
* by mapping the image so that the background is near the input
* value 'bgval'.
* (2) The input image is either grayscale or rgb.
* (3) For each component in the input image, the background value
* is estimated using a grayscale closing; hence the 'Morph'
* in the function name.
* (4) An optional binary mask can be specified, with the foreground
* pixels typically over image regions. The resulting background
* map values will be determined by surrounding pixels that are
* not under the mask foreground. The origin (0,0) of this mask
* is assumed to be aligned with the origin of the input image.
* This binary mask must not fully cover pixs, because then there
* will be no pixels in the input image available to compute
* the background.
* (5) The map is computed at reduced size (given by 'reduction')
* from the input pixs and optional pixim. At this scale,
* pixs is closed to remove the background, using a square Sel
* of odd dimension. The product of reduction * size should be
* large enough to remove most of the text foreground.
* (6) No convolutional smoothing needs to be done on the map before
* inverting it.
* (7) A 'bgval' target background value for the normalized image. This
* should be at least 128. If set too close to 255, some
* clipping will occur in the result.
* </pre>
*/
PIX *
pixBackgroundNormMorph(PIX *pixs,
PIX *pixim,
l_int32 reduction,
l_int32 size,
l_int32 bgval)
{
l_int32 d, allfg;
PIX *pixm, *pixmi, *pixd;
PIX *pixmr, *pixmg, *pixmb, *pixmri, *pixmgi, *pixmbi;
PROCNAME("pixBackgroundNormMorph");
if (!pixs)
return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
d = pixGetDepth(pixs);
if (d != 8 && d != 32)
return (PIX *)ERROR_PTR("pixs not 8 or 32 bpp", procName, NULL);
if (reduction < 2 || reduction > 16)
return (PIX *)ERROR_PTR("reduction must be between 2 and 16",
procName, NULL);
/* If pixim exists, verify that it is not all foreground. */
if (pixim) {
pixInvert(pixim, pixim);
pixZero(pixim, &allfg);
pixInvert(pixim, pixim);
if (allfg)
return (PIX *)ERROR_PTR("pixim all foreground", procName, NULL);
}
pixd = NULL;
if (d == 8) {
pixGetBackgroundGrayMapMorph(pixs, pixim, reduction, size, &pixm);
if (!pixm)
return (PIX *)ERROR_PTR("pixm not made", procName, NULL);
pixmi = pixGetInvBackgroundMap(pixm, bgval, 0, 0);
if (!pixmi)
ERROR_PTR("pixmi not made", procName, NULL);
else
pixd = pixApplyInvBackgroundGrayMap(pixs, pixmi,
reduction, reduction);
pixDestroy(&pixm);
pixDestroy(&pixmi);
}
else { /* d == 32 */
pixmr = pixmg = pixmb = NULL;
pixGetBackgroundRGBMapMorph(pixs, pixim, reduction, size,
&pixmr, &pixmg, &pixmb);
if (!pixmr || !pixmg || !pixmb) {
pixDestroy(&pixmr);
pixDestroy(&pixmg);
pixDestroy(&pixmb);
return (PIX *)ERROR_PTR("not all pixm*", procName, NULL);
}
pixmri = pixGetInvBackgroundMap(pixmr, bgval, 0, 0);
pixmgi = pixGetInvBackgroundMap(pixmg, bgval, 0, 0);
pixmbi = pixGetInvBackgroundMap(pixmb, bgval, 0, 0);
if (!pixmri || !pixmgi || !pixmbi)
ERROR_PTR("not all pixm*i are made", procName, NULL);
else
pixd = pixApplyInvBackgroundRGBMap(pixs, pixmri, pixmgi, pixmbi,
reduction, reduction);
pixDestroy(&pixmr);
pixDestroy(&pixmg);
pixDestroy(&pixmb);
pixDestroy(&pixmri);
pixDestroy(&pixmgi);
pixDestroy(&pixmbi);
}
if (!pixd)
ERROR_PTR("pixd not made", procName, NULL);
pixCopyResolution(pixd, pixs);
return pixd;
}
/*-------------------------------------------------------------------------*
* Arrays of inverted background values for normalization *
*-------------------------------------------------------------------------*
* Notes for these four functions: *
* (1) They are useful if you need to save the actual mapping array. *
* (2) They could be used in the top-level functions but are *
* not because their use makes those functions less clear. *
* (3) Each component in the input pixs generates a 16 bpp pix array. *
*-------------------------------------------------------------------------*/
/*!
* \brief pixBackgroundNormGrayArray()
*
* \param[in] pixs 8 bpp grayscale
* \param[in] pixim [optional] 1 bpp 'image' mask; can be null
* \param[in] sx, sy tile size in pixels
* \param[in] thresh threshold for determining foreground
* \param[in] mincount min threshold on counts in a tile
* \param[in] bgval target bg val; typ. > 128
* \param[in] smoothx half-width of block convolution kernel width
* \param[in] smoothy half-width of block convolution kernel height
* \param[out] ppixd 16 bpp array of inverted background value
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) See notes in pixBackgroundNorm().
* (2) This returns a 16 bpp pix that can be used by
* pixApplyInvBackgroundGrayMap() to generate a normalized version
* of the input pixs.
* </pre>
*/
l_ok
pixBackgroundNormGrayArray(PIX *pixs,
PIX *pixim,
l_int32 sx,
l_int32 sy,
l_int32 thresh,
l_int32 mincount,
l_int32 bgval,
l_int32 smoothx,
l_int32 smoothy,
PIX **ppixd)
{
l_int32 allfg;
PIX *pixm;
PROCNAME("pixBackgroundNormGrayArray");
if (!ppixd)
return ERROR_INT("&pixd not defined", procName, 1);
*ppixd = NULL;
if (!pixs || pixGetDepth(pixs) != 8)
return ERROR_INT("pixs not defined or not 8 bpp", procName, 1);
if (pixGetColormap(pixs))
return ERROR_INT("pixs is colormapped", procName, 1);
if (pixim && pixGetDepth(pixim) != 1)
return ERROR_INT("pixim not 1 bpp", procName, 1);
if (sx < 4 || sy < 4)
return ERROR_INT("sx and sy must be >= 4", procName, 1);
if (mincount > sx * sy) {
L_WARNING("mincount too large for tile size\n", procName);
mincount = (sx * sy) / 3;
}
/* If pixim exists, verify that it is not all foreground. */
if (pixim) {
pixInvert(pixim, pixim);
pixZero(pixim, &allfg);
pixInvert(pixim, pixim);
if (allfg)
return ERROR_INT("pixim all foreground", procName, 1);
}
pixGetBackgroundGrayMap(pixs, pixim, sx, sy, thresh, mincount, &pixm);
if (!pixm)
return ERROR_INT("pixm not made", procName, 1);
*ppixd = pixGetInvBackgroundMap(pixm, bgval, smoothx, smoothy);
pixCopyResolution(*ppixd, pixs);
pixDestroy(&pixm);
return 0;
}
/*!
* \brief pixBackgroundNormRGBArrays()
*
* \param[in] pixs 32 bpp rgb
* \param[in] pixim [optional] 1 bpp 'image' mask; can be null
* \param[in] pixg [optional] 8 bpp grayscale version; can be null
* \param[in] sx, sy tile size in pixels
* \param[in] thresh threshold for determining foreground
* \param[in] mincount min threshold on counts in a tile
* \param[in] bgval target bg val; typ. > 128
* \param[in] smoothx half-width of block convolution kernel width
* \param[in] smoothy half-width of block convolution kernel height
* \param[out] ppixr 16 bpp array of inverted R background value
* \param[out] ppixg 16 bpp array of inverted G background value
* \param[out] ppixb 16 bpp array of inverted B background value
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) See notes in pixBackgroundNorm().
* (2) This returns a set of three 16 bpp pix that can be used by
* pixApplyInvBackgroundGrayMap() to generate a normalized version
* of each component of the input pixs.
* </pre>
*/
l_ok
pixBackgroundNormRGBArrays(PIX *pixs,
PIX *pixim,
PIX *pixg,
l_int32 sx,
l_int32 sy,
l_int32 thresh,
l_int32 mincount,
l_int32 bgval,
l_int32 smoothx,
l_int32 smoothy,
PIX **ppixr,
PIX **ppixg,
PIX **ppixb)
{
l_int32 allfg;
PIX *pixmr, *pixmg, *pixmb;
PROCNAME("pixBackgroundNormRGBArrays");
if (!ppixr || !ppixg || !ppixb)
return ERROR_INT("&pixr, &pixg, &pixb not all defined", procName, 1);
*ppixr = *ppixg = *ppixb = NULL;
if (!pixs)
return ERROR_INT("pixs not defined", procName, 1);
if (pixGetDepth(pixs) != 32)
return ERROR_INT("pixs not 32 bpp", procName, 1);
if (pixim && pixGetDepth(pixim) != 1)
return ERROR_INT("pixim not 1 bpp", procName, 1);
if (sx < 4 || sy < 4)
return ERROR_INT("sx and sy must be >= 4", procName, 1);
if (mincount > sx * sy) {
L_WARNING("mincount too large for tile size\n", procName);
mincount = (sx * sy) / 3;
}
/* If pixim exists, verify that it is not all foreground. */
if (pixim) {
pixInvert(pixim, pixim);
pixZero(pixim, &allfg);
pixInvert(pixim, pixim);
if (allfg)
return ERROR_INT("pixim all foreground", procName, 1);
}
pixGetBackgroundRGBMap(pixs, pixim, pixg, sx, sy, thresh, mincount,
&pixmr, &pixmg, &pixmb);
if (!pixmr || !pixmg || !pixmb) {
pixDestroy(&pixmr);
pixDestroy(&pixmg);
pixDestroy(&pixmb);
return ERROR_INT("not all pixm* made", procName, 1);
}
*ppixr = pixGetInvBackgroundMap(pixmr, bgval, smoothx, smoothy);
*ppixg = pixGetInvBackgroundMap(pixmg, bgval, smoothx, smoothy);
*ppixb = pixGetInvBackgroundMap(pixmb, bgval, smoothx, smoothy);
pixDestroy(&pixmr);
pixDestroy(&pixmg);
pixDestroy(&pixmb);
return 0;
}
/*!
* \brief pixBackgroundNormGrayArrayMorph()
*
* \param[in] pixs 8 bpp grayscale
* \param[in] pixim [optional] 1 bpp 'image' mask; can be null
* \param[in] reduction at which morph closings are done; between 2 and 16
* \param[in] size of square Sel for the closing; use an odd number
* \param[in] bgval target bg val; typ. > 128
* \param[out] ppixd 16 bpp array of inverted background value
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) See notes in pixBackgroundNormMorph().
* (2) This returns a 16 bpp pix that can be used by
* pixApplyInvBackgroundGrayMap() to generate a normalized version
* of the input pixs.
* </pre>
*/
l_ok
pixBackgroundNormGrayArrayMorph(PIX *pixs,
PIX *pixim,
l_int32 reduction,
l_int32 size,
l_int32 bgval,
PIX **ppixd)
{
l_int32 allfg;
PIX *pixm;
PROCNAME("pixBackgroundNormGrayArrayMorph");
if (!ppixd)
return ERROR_INT("&pixd not defined", procName, 1);
*ppixd = NULL;
if (!pixs)
return ERROR_INT("pixs not defined", procName, 1);
if (pixGetDepth(pixs) != 8)
return ERROR_INT("pixs not 8 bpp", procName, 1);
if (pixim && pixGetDepth(pixim) != 1)
return ERROR_INT("pixim not 1 bpp", procName, 1);
if (reduction < 2 || reduction > 16)
return ERROR_INT("reduction must be between 2 and 16", procName, 1);
/* If pixim exists, verify that it is not all foreground. */
if (pixim) {
pixInvert(pixim, pixim);
pixZero(pixim, &allfg);
pixInvert(pixim, pixim);
if (allfg)
return ERROR_INT("pixim all foreground", procName, 1);
}
pixGetBackgroundGrayMapMorph(pixs, pixim, reduction, size, &pixm);
if (!pixm)
return ERROR_INT("pixm not made", procName, 1);
*ppixd = pixGetInvBackgroundMap(pixm, bgval, 0, 0);
pixCopyResolution(*ppixd, pixs);
pixDestroy(&pixm);
return 0;
}
/*!
* \brief pixBackgroundNormRGBArraysMorph()
*
* \param[in] pixs 32 bpp rgb
* \param[in] pixim [optional] 1 bpp 'image' mask; can be null
* \param[in] reduction at which morph closings are done; between 2 and 16
* \param[in] size of square Sel for the closing; use an odd number
* \param[in] bgval target bg val; typ. > 128
* \param[out] ppixr 16 bpp array of inverted R background value
* \param[out] ppixg 16 bpp array of inverted G background value
* \param[out] ppixb 16 bpp array of inverted B background value
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) See notes in pixBackgroundNormMorph().
* (2) This returns a set of three 16 bpp pix that can be used by
* pixApplyInvBackgroundGrayMap() to generate a normalized version
* of each component of the input pixs.
* </pre>
*/
l_ok
pixBackgroundNormRGBArraysMorph(PIX *pixs,
PIX *pixim,
l_int32 reduction,
l_int32 size,
l_int32 bgval,
PIX **ppixr,
PIX **ppixg,
PIX **ppixb)
{
l_int32 allfg;
PIX *pixmr, *pixmg, *pixmb;
PROCNAME("pixBackgroundNormRGBArraysMorph");
if (!ppixr || !ppixg || !ppixb)
return ERROR_INT("&pixr, &pixg, &pixb not all defined", procName, 1);
*ppixr = *ppixg = *ppixb = NULL;
if (!pixs)
return ERROR_INT("pixs not defined", procName, 1);
if (pixGetDepth(pixs) != 32)
return ERROR_INT("pixs not 32 bpp", procName, 1);
if (pixim && pixGetDepth(pixim) != 1)
return ERROR_INT("pixim not 1 bpp", procName, 1);
if (reduction < 2 || reduction > 16)
return ERROR_INT("reduction must be between 2 and 16", procName, 1);
/* If pixim exists, verify that it is not all foreground. */
if (pixim) {
pixInvert(pixim, pixim);
pixZero(pixim, &allfg);
pixInvert(pixim, pixim);
if (allfg)
return ERROR_INT("pixim all foreground", procName, 1);
}
pixGetBackgroundRGBMapMorph(pixs, pixim, reduction, size,
&pixmr, &pixmg, &pixmb);
if (!pixmr || !pixmg || !pixmb) {
pixDestroy(&pixmr);
pixDestroy(&pixmg);
pixDestroy(&pixmb);
return ERROR_INT("not all pixm* made", procName, 1);
}
*ppixr = pixGetInvBackgroundMap(pixmr, bgval, 0, 0);
*ppixg = pixGetInvBackgroundMap(pixmg, bgval, 0, 0);
*ppixb = pixGetInvBackgroundMap(pixmb, bgval, 0, 0);
pixDestroy(&pixmr);
pixDestroy(&pixmg);
pixDestroy(&pixmb);
return 0;
}
/*------------------------------------------------------------------*
* Measurement of local background *
*------------------------------------------------------------------*/
/*!
* \brief pixGetBackgroundGrayMap()
*
* \param[in] pixs 8 bpp grayscale; not cmapped
* \param[in] pixim [optional] 1 bpp 'image' mask; can be null;
* it should not have only foreground pixels
* \param[in] sx, sy tile size in pixels
* \param[in] thresh threshold for determining foreground
* \param[in] mincount min threshold on counts in a tile
* \param[out] ppixd 8 bpp grayscale map
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) The background is measured in regions that don't have
* images. It is then propagated into the image regions,
* and finally smoothed in each image region.
* </pre>
*/
l_ok
pixGetBackgroundGrayMap(PIX *pixs,
PIX *pixim,
l_int32 sx,
l_int32 sy,
l_int32 thresh,
l_int32 mincount,
PIX **ppixd)
{
l_int32 w, h, wd, hd, wim, him, wpls, wplim, wpld, wplf;
l_int32 xim, yim, delx, nx, ny, i, j, k, m;
l_int32 count, sum, val8;
l_int32 empty, fgpixels;
l_uint32 *datas, *dataim, *datad, *dataf, *lines, *lineim, *lined, *linef;
l_float32 scalex, scaley;
PIX *pixd, *piximi, *pixb, *pixf, *pixims;
PROCNAME("pixGetBackgroundGrayMap");
if (!ppixd)
return ERROR_INT("&pixd not defined", procName, 1);
*ppixd = NULL;
if (!pixs || pixGetDepth(pixs) != 8)
return ERROR_INT("pixs not defined or not 8 bpp", procName, 1);
if (pixGetColormap(pixs))
return ERROR_INT("pixs is colormapped", procName, 1);
if (pixim && pixGetDepth(pixim) != 1)
return ERROR_INT("pixim not 1 bpp", procName, 1);
if (sx < 4 || sy < 4)
return ERROR_INT("sx and sy must be >= 4", procName, 1);
if (mincount > sx * sy) {
L_WARNING("mincount too large for tile size\n", procName);
mincount = (sx * sy) / 3;
}
/* Evaluate the 'image' mask, pixim, and make sure
* it is not all fg. */
fgpixels = 0; /* boolean for existence of fg pixels in the image mask. */
if (pixim) {
piximi = pixInvert(NULL, pixim); /* set non-'image' pixels to 1 */
pixZero(piximi, &empty);
pixDestroy(&piximi);
if (empty)
return ERROR_INT("pixim all fg; no background", procName, 1);
pixZero(pixim, &empty);
if (!empty) /* there are fg pixels in pixim */
fgpixels = 1;
}
/* Generate the foreground mask, pixf, which is at
* full resolution. These pixels will be ignored when
* computing the background values. */
pixb = pixThresholdToBinary(pixs, thresh);
pixf = pixMorphSequence(pixb, "d7.1 + d1.7", 0);
pixDestroy(&pixb);
/* ------------- Set up the output map pixd --------------- */
/* Generate pixd, which is reduced by the factors (sx, sy). */
w = pixGetWidth(pixs);
h = pixGetHeight(pixs);
wd = (w + sx - 1) / sx;
hd = (h + sy - 1) / sy;
pixd = pixCreate(wd, hd, 8);
/* Note: we only compute map values in tiles that are complete.
* In general, tiles at right and bottom edges will not be
* complete, and we must fill them in later. */
nx = w / sx;
ny = h / sy;
wpls = pixGetWpl(pixs);
datas = pixGetData(pixs);
wpld = pixGetWpl(pixd);
datad = pixGetData(pixd);
wplf = pixGetWpl(pixf);
dataf = pixGetData(pixf);
for (i = 0; i < ny; i++) {
lines = datas + sy * i * wpls;
linef = dataf + sy * i * wplf;
lined = datad + i * wpld;
for (j = 0; j < nx; j++) {
delx = j * sx;
sum = 0;
count = 0;
for (k = 0; k < sy; k++) {
for (m = 0; m < sx; m++) {
if (GET_DATA_BIT(linef + k * wplf, delx + m) == 0) {
sum += GET_DATA_BYTE(lines + k * wpls, delx + m);
count++;
}
}
}
if (count >= mincount) {
val8 = sum / count;
SET_DATA_BYTE(lined, j, val8);
}
}
}
pixDestroy(&pixf);
/* If there is an optional mask with fg pixels, erase the previous
* calculation for the corresponding map pixels, setting the
* map values to 0. Then, when all the map holes are filled,
* these erased pixels will be set by the surrounding map values.
*
* The calculation here is relatively efficient: for each pixel
* in pixd (which corresponds to a tile of mask pixels in pixim)
* we look only at the pixel in pixim that is at the center
* of the tile. If the mask pixel is ON, we reset the map
* pixel in pixd to 0, so that it can later be filled in. */
pixims = NULL;
if (pixim && fgpixels) {
wim = pixGetWidth(pixim);
him = pixGetHeight(pixim);
dataim = pixGetData(pixim);
wplim = pixGetWpl(pixim);
for (i = 0; i < ny; i++) {
yim = i * sy + sy / 2;
if (yim >= him)
break;
lineim = dataim + yim * wplim;
for (j = 0; j < nx; j++) {
xim = j * sx + sx / 2;
if (xim >= wim)
break;
if (GET_DATA_BIT(lineim, xim))
pixSetPixel(pixd, j, i, 0);
}
}
}
/* Fill all the holes in the map. */
if (pixFillMapHoles(pixd, nx, ny, L_FILL_BLACK)) {
pixDestroy(&pixd);
L_WARNING("can't make the map\n", procName);
return 1;
}
/* Finally, for each connected region corresponding to the
* 'image' mask, reset all pixels to their average value.
* Each of these components represents an image (or part of one)
* in the input, and this smooths the background values
* in each of these regions. */
if (pixim && fgpixels) {
scalex = 1. / (l_float32)sx;
scaley = 1. / (l_float32)sy;
pixims = pixScaleBySampling(pixim, scalex, scaley);