-
Notifications
You must be signed in to change notification settings - Fork 393
/
pix5.c
3221 lines (2925 loc) · 102 KB
/
pix5.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 pix5.c
* <pre>
*
* This file has these operations:
*
* (1) Measurement of 1 bpp image properties
* (2) Extract rectangular regions
* (3) Clip to foreground
* (4) Extract pixel averages, reversals and variance along lines
* (5) Rank row and column transforms
*
* Measurement of properties
* l_int32 pixaFindDimensions()
* l_int32 pixFindAreaPerimRatio()
* NUMA *pixaFindPerimToAreaRatio()
* l_int32 pixFindPerimToAreaRatio()
* NUMA *pixaFindPerimSizeRatio()
* l_int32 pixFindPerimSizeRatio()
* NUMA *pixaFindAreaFraction()
* l_int32 pixFindAreaFraction()
* NUMA *pixaFindAreaFractionMasked()
* l_int32 pixFindAreaFractionMasked()
* NUMA *pixaFindWidthHeightRatio()
* NUMA *pixaFindWidthHeightProduct()
* l_int32 pixFindOverlapFraction()
* BOXA *pixFindRectangleComps()
* l_int32 pixConformsToRectangle()
*
* Extract rectangular region
* PIXA *pixClipRectangles()
* PIX *pixClipRectangle()
* PIX *pixClipRectangleWithBorder()
* PIX *pixClipMasked()
* l_int32 pixCropToMatch()
* PIX *pixCropToSize()
* PIX *pixResizeToMatch()
*
* Select a connected component by size
* PIX *pixSelectComponentBySize()
* PIX *pixFilterComponentBySize()
*
* Make special masks
* PIX *pixMakeSymmetricMask()
* PIX *pixMakeFrameMask()
*
* Generate a covering of rectangles over connected components
* PIX * pixMakeCoveringOfRectangles()
*
* Fraction of Fg pixels under a mask
* l_int32 pixFractionFgInMask()
*
* Clip to foreground
* PIX *pixClipToForeground()
* l_int32 pixTestClipToForeground()
* l_int32 pixClipBoxToForeground()
* l_int32 pixScanForForeground()
* l_int32 pixClipBoxToEdges()
* l_int32 pixScanForEdge()
*
* Extract pixel averages and reversals along lines
* NUMA *pixExtractOnLine()
* l_float32 pixAverageOnLine()
* NUMA *pixAverageIntensityProfile()
* NUMA *pixReversalProfile()
*
* Extract windowed variance along a line
* NUMA *pixWindowedVarianceOnLine()
*
* Extract min/max of pixel values near lines
* l_int32 pixMinMaxNearLine()
*
* Rank row and column transforms
* PIX *pixRankRowTransform()
* PIX *pixRankColumnTransform()
* </pre>
*/
#ifdef HAVE_CONFIG_H
#include <config_auto.h>
#endif /* HAVE_CONFIG_H */
#include <string.h>
#include <math.h>
#include "allheaders.h"
static const l_uint32 rmask32[] = {0x0,
0x00000001, 0x00000003, 0x00000007, 0x0000000f,
0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff,
0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff,
0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff,
0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff};
#ifndef NO_CONSOLE_IO
#define DEBUG_EDGES 0
#endif /* ~NO_CONSOLE_IO */
/*-------------------------------------------------------------*
* Measurement of properties *
*-------------------------------------------------------------*/
/*!
* \brief pixaFindDimensions()
*
* \param[in] pixa
* \param[out] pnaw [optional] numa of pix widths
* \param[out] pnah [optional] numa of pix heights
* \return 0 if OK, 1 on error
*/
l_ok
pixaFindDimensions(PIXA *pixa,
NUMA **pnaw,
NUMA **pnah)
{
l_int32 i, n, w, h;
PIX *pixt;
PROCNAME("pixaFindDimensions");
if (pnaw) *pnaw = NULL;
if (pnah) *pnah = NULL;
if (!pnaw && !pnah)
return ERROR_INT("no output requested", procName, 1);
if (!pixa)
return ERROR_INT("pixa not defined", procName, 1);
n = pixaGetCount(pixa);
if (pnaw) *pnaw = numaCreate(n);
if (pnah) *pnah = numaCreate(n);
for (i = 0; i < n; i++) {
pixt = pixaGetPix(pixa, i, L_CLONE);
pixGetDimensions(pixt, &w, &h, NULL);
if (pnaw)
numaAddNumber(*pnaw, w);
if (pnah)
numaAddNumber(*pnah, h);
pixDestroy(&pixt);
}
return 0;
}
/*!
* \brief pixFindAreaPerimRatio()
*
* \param[in] pixs 1 bpp
* \param[in] tab [optional] pixel sum table, can be NULL
* \param[out] pfract area/perimeter ratio
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) The area is the number of fg pixels that are not on the
* boundary (i.e., are not 8-connected to a bg pixel), and the
* perimeter is the number of fg boundary pixels. Returns
* 0.0 if there are no fg pixels.
* (2) This function is retained because clients are using it.
* </pre>
*/
l_ok
pixFindAreaPerimRatio(PIX *pixs,
l_int32 *tab,
l_float32 *pfract)
{
l_int32 *tab8;
l_int32 nfg, nbound;
PIX *pixt;
PROCNAME("pixFindAreaPerimRatio");
if (!pfract)
return ERROR_INT("&fract not defined", procName, 1);
*pfract = 0.0;
if (!pixs || pixGetDepth(pixs) != 1)
return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
if (!tab)
tab8 = makePixelSumTab8();
else
tab8 = tab;
pixt = pixErodeBrick(NULL, pixs, 3, 3);
pixCountPixels(pixt, &nfg, tab8);
if (nfg == 0) {
pixDestroy(&pixt);
if (!tab) LEPT_FREE(tab8);
return 0;
}
pixXor(pixt, pixt, pixs);
pixCountPixels(pixt, &nbound, tab8);
*pfract = (l_float32)nfg / (l_float32)nbound;
pixDestroy(&pixt);
if (!tab) LEPT_FREE(tab8);
return 0;
}
/*!
* \brief pixaFindPerimToAreaRatio()
*
* \param[in] pixa of 1 bpp pix
* \return na of perimeter/arear ratio for each pix, or NULL on error
*
* <pre>
* Notes:
* (1) This is typically used for a pixa consisting of
* 1 bpp connected components.
* </pre>
*/
NUMA *
pixaFindPerimToAreaRatio(PIXA *pixa)
{
l_int32 i, n;
l_int32 *tab;
l_float32 fract;
NUMA *na;
PIX *pixt;
PROCNAME("pixaFindPerimToAreaRatio");
if (!pixa)
return (NUMA *)ERROR_PTR("pixa not defined", procName, NULL);
n = pixaGetCount(pixa);
na = numaCreate(n);
tab = makePixelSumTab8();
for (i = 0; i < n; i++) {
pixt = pixaGetPix(pixa, i, L_CLONE);
pixFindPerimToAreaRatio(pixt, tab, &fract);
numaAddNumber(na, fract);
pixDestroy(&pixt);
}
LEPT_FREE(tab);
return na;
}
/*!
* \brief pixFindPerimToAreaRatio()
*
* \param[in] pixs 1 bpp
* \param[in] tab [optional] pixel sum table, can be NULL
* \param[out] pfract perimeter/area ratio
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) The perimeter is the number of fg boundary pixels, and the
* area is the number of fg pixels. This returns 0.0 if
* there are no fg pixels.
* (2) Unlike pixFindAreaPerimRatio(), this uses the full set of
* fg pixels for the area, and the ratio is taken in the opposite
* order.
* (3) This is typically used for a single connected component.
* This always has a value <= 1.0, and if the average distance
* of a fg pixel from the nearest bg pixel is d, this has
* a value ~1/d.
* </pre>
*/
l_ok
pixFindPerimToAreaRatio(PIX *pixs,
l_int32 *tab,
l_float32 *pfract)
{
l_int32 *tab8;
l_int32 nfg, nbound;
PIX *pixt;
PROCNAME("pixFindPerimToAreaRatio");
if (!pfract)
return ERROR_INT("&fract not defined", procName, 1);
*pfract = 0.0;
if (!pixs || pixGetDepth(pixs) != 1)
return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
if (!tab)
tab8 = makePixelSumTab8();
else
tab8 = tab;
pixCountPixels(pixs, &nfg, tab8);
if (nfg == 0) {
if (!tab) LEPT_FREE(tab8);
return 0;
}
pixt = pixErodeBrick(NULL, pixs, 3, 3);
pixXor(pixt, pixt, pixs);
pixCountPixels(pixt, &nbound, tab8);
*pfract = (l_float32)nbound / (l_float32)nfg;
pixDestroy(&pixt);
if (!tab) LEPT_FREE(tab8);
return 0;
}
/*!
* \brief pixaFindPerimSizeRatio()
*
* \param[in] pixa of 1 bpp pix
* \return na of fg perimeter/(2*(w+h)) ratio for each pix,
* or NULL on error
*
* <pre>
* Notes:
* (1) This is typically used for a pixa consisting of
* 1 bpp connected components.
* (2) This has a minimum value for a circle of pi/4; a value for
* a rectangle component of approx. 1.0; and a value much larger
* than 1.0 for a component with a highly irregular boundary.
* </pre>
*/
NUMA *
pixaFindPerimSizeRatio(PIXA *pixa)
{
l_int32 i, n;
l_int32 *tab;
l_float32 ratio;
NUMA *na;
PIX *pixt;
PROCNAME("pixaFindPerimSizeRatio");
if (!pixa)
return (NUMA *)ERROR_PTR("pixa not defined", procName, NULL);
n = pixaGetCount(pixa);
na = numaCreate(n);
tab = makePixelSumTab8();
for (i = 0; i < n; i++) {
pixt = pixaGetPix(pixa, i, L_CLONE);
pixFindPerimSizeRatio(pixt, tab, &ratio);
numaAddNumber(na, ratio);
pixDestroy(&pixt);
}
LEPT_FREE(tab);
return na;
}
/*!
* \brief pixFindPerimSizeRatio()
*
* \param[in] pixs 1 bpp
* \param[in] tab [optional] pixel sum table, can be NULL
* \param[out] pratio perimeter/size ratio
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) We take the 'size' as twice the sum of the width and
* height of pixs, and the perimeter is the number of fg
* boundary pixels. We use the fg pixels of the boundary
* because the pix may be clipped to the boundary, so an
* erosion is required to count all boundary pixels.
* (2) This has a large value for dendritic, fractal-like components
* with highly irregular boundaries.
* (3) This is typically used for a single connected component.
* It has a value of about 1.0 for rectangular components with
* relatively smooth boundaries.
* </pre>
*/
l_ok
pixFindPerimSizeRatio(PIX *pixs,
l_int32 *tab,
l_float32 *pratio)
{
l_int32 *tab8;
l_int32 w, h, nbound;
PIX *pixt;
PROCNAME("pixFindPerimSizeRatio");
if (!pratio)
return ERROR_INT("&ratio not defined", procName, 1);
*pratio = 0.0;
if (!pixs || pixGetDepth(pixs) != 1)
return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
if (!tab)
tab8 = makePixelSumTab8();
else
tab8 = tab;
pixt = pixErodeBrick(NULL, pixs, 3, 3);
pixXor(pixt, pixt, pixs);
pixCountPixels(pixt, &nbound, tab8);
pixGetDimensions(pixs, &w, &h, NULL);
*pratio = (0.5 * nbound) / (l_float32)(w + h);
pixDestroy(&pixt);
if (!tab) LEPT_FREE(tab8);
return 0;
}
/*!
* \brief pixaFindAreaFraction()
*
* \param[in] pixa of 1 bpp pix
* \return na of area fractions for each pix, or NULL on error
*
* <pre>
* Notes:
* (1) This is typically used for a pixa consisting of
* 1 bpp connected components.
* </pre>
*/
NUMA *
pixaFindAreaFraction(PIXA *pixa)
{
l_int32 i, n;
l_int32 *tab;
l_float32 fract;
NUMA *na;
PIX *pixt;
PROCNAME("pixaFindAreaFraction");
if (!pixa)
return (NUMA *)ERROR_PTR("pixa not defined", procName, NULL);
n = pixaGetCount(pixa);
na = numaCreate(n);
tab = makePixelSumTab8();
for (i = 0; i < n; i++) {
pixt = pixaGetPix(pixa, i, L_CLONE);
pixFindAreaFraction(pixt, tab, &fract);
numaAddNumber(na, fract);
pixDestroy(&pixt);
}
LEPT_FREE(tab);
return na;
}
/*!
* \brief pixFindAreaFraction()
*
* \param[in] pixs 1 bpp
* \param[in] tab [optional] pixel sum table, can be NULL
* \param[out] pfract fg area/size ratio
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) This finds the ratio of the number of fg pixels to the
* size of the pix (w * h). It is typically used for a
* single connected component.
* </pre>
*/
l_ok
pixFindAreaFraction(PIX *pixs,
l_int32 *tab,
l_float32 *pfract)
{
l_int32 w, h, sum;
l_int32 *tab8;
PROCNAME("pixFindAreaFraction");
if (!pfract)
return ERROR_INT("&fract not defined", procName, 1);
*pfract = 0.0;
if (!pixs || pixGetDepth(pixs) != 1)
return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
if (!tab)
tab8 = makePixelSumTab8();
else
tab8 = tab;
pixGetDimensions(pixs, &w, &h, NULL);
pixCountPixels(pixs, &sum, tab8);
*pfract = (l_float32)sum / (l_float32)(w * h);
if (!tab) LEPT_FREE(tab8);
return 0;
}
/*!
* \brief pixaFindAreaFractionMasked()
*
* \param[in] pixa of 1 bpp pix
* \param[in] pixm mask image
* \param[in] debug 1 for output, 0 to suppress
* \return na of ratio masked/total fractions for each pix,
* or NULL on error
*
* <pre>
* Notes:
* (1) This is typically used for a pixa consisting of
* 1 bpp connected components, which has an associated
* boxa giving the location of the components relative
* to the mask origin.
* (2) The debug flag displays in green and red the masked and
* unmasked parts of the image from which pixa was derived.
* </pre>
*/
NUMA *
pixaFindAreaFractionMasked(PIXA *pixa,
PIX *pixm,
l_int32 debug)
{
l_int32 i, n, full;
l_int32 *tab;
l_float32 fract;
BOX *box;
NUMA *na;
PIX *pix;
PROCNAME("pixaFindAreaFractionMasked");
if (!pixa)
return (NUMA *)ERROR_PTR("pixa not defined", procName, NULL);
if (!pixm || pixGetDepth(pixm) != 1)
return (NUMA *)ERROR_PTR("pixm undefined or not 1 bpp", procName, NULL);
n = pixaGetCount(pixa);
na = numaCreate(n);
tab = makePixelSumTab8();
pixaIsFull(pixa, NULL, &full); /* check boxa */
box = NULL;
for (i = 0; i < n; i++) {
pix = pixaGetPix(pixa, i, L_CLONE);
if (full)
box = pixaGetBox(pixa, i, L_CLONE);
pixFindAreaFractionMasked(pix, box, pixm, tab, &fract);
numaAddNumber(na, fract);
boxDestroy(&box);
pixDestroy(&pix);
}
LEPT_FREE(tab);
if (debug) {
l_int32 w, h;
PIX *pix1, *pix2;
pixGetDimensions(pixm, &w, &h, NULL);
pix1 = pixaDisplay(pixa, w, h); /* recover original image */
pix2 = pixCreate(w, h, 8); /* make an 8 bpp white image ... */
pixSetColormap(pix2, pixcmapCreate(8)); /* that's cmapped ... */
pixSetBlackOrWhite(pix2, L_SET_WHITE); /* and init to white */
pixSetMaskedCmap(pix2, pix1, 0, 0, 255, 0, 0); /* color all fg red */
pixRasterop(pix1, 0, 0, w, h, PIX_MASK, pixm, 0, 0);
pixSetMaskedCmap(pix2, pix1, 0, 0, 0, 255, 0); /* turn masked green */
pixDisplay(pix2, 100, 100);
pixDestroy(&pix1);
pixDestroy(&pix2);
}
return na;
}
/*!
* \brief pixFindAreaFractionMasked()
*
* \param[in] pixs 1 bpp, typically a single component
* \param[in] box [optional] for pixs relative to pixm
* \param[in] pixm 1 bpp mask, typically over the entire image from
* which the component pixs was extracted
* \param[in] tab [optional] pixel sum table, can be NULL
* \param[out] pfract fg area/size ratio
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) This finds the ratio of the number of masked fg pixels
* in pixs to the total number of fg pixels in pixs.
* It is typically used for a single connected component.
* If there are no fg pixels, this returns a ratio of 0.0.
* (2) The box gives the location of the pix relative to that
* of the UL corner of the mask. Therefore, the rasterop
* is performed with the pix translated to its location
* (x, y) in the mask before ANDing.
* If box == NULL, the UL corners of pixs and pixm are aligned.
* </pre>
*/
l_ok
pixFindAreaFractionMasked(PIX *pixs,
BOX *box,
PIX *pixm,
l_int32 *tab,
l_float32 *pfract)
{
l_int32 x, y, w, h, sum, masksum;
l_int32 *tab8;
PIX *pix1;
PROCNAME("pixFindAreaFractionMasked");
if (!pfract)
return ERROR_INT("&fract not defined", procName, 1);
*pfract = 0.0;
if (!pixs || pixGetDepth(pixs) != 1)
return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
if (!pixm || pixGetDepth(pixm) != 1)
return ERROR_INT("pixm not defined or not 1 bpp", procName, 1);
if (!tab)
tab8 = makePixelSumTab8();
else
tab8 = tab;
x = y = 0;
if (box)
boxGetGeometry(box, &x, &y, NULL, NULL);
pixGetDimensions(pixs, &w, &h, NULL);
pix1 = pixCopy(NULL, pixs);
pixRasterop(pix1, 0, 0, w, h, PIX_MASK, pixm, x, y);
pixCountPixels(pixs, &sum, tab8);
if (sum == 0) {
pixDestroy(&pix1);
if (!tab) LEPT_FREE(tab8);
return 0;
}
pixCountPixels(pix1, &masksum, tab8);
*pfract = (l_float32)masksum / (l_float32)sum;
if (!tab) LEPT_FREE(tab8);
pixDestroy(&pix1);
return 0;
}
/*!
* \brief pixaFindWidthHeightRatio()
*
* \param[in] pixa of 1 bpp pix
* \return na of width/height ratios for each pix, or NULL on error
*
* <pre>
* Notes:
* (1) This is typically used for a pixa consisting of
* 1 bpp connected components.
* </pre>
*/
NUMA *
pixaFindWidthHeightRatio(PIXA *pixa)
{
l_int32 i, n, w, h;
NUMA *na;
PIX *pixt;
PROCNAME("pixaFindWidthHeightRatio");
if (!pixa)
return (NUMA *)ERROR_PTR("pixa not defined", procName, NULL);
n = pixaGetCount(pixa);
na = numaCreate(n);
for (i = 0; i < n; i++) {
pixt = pixaGetPix(pixa, i, L_CLONE);
pixGetDimensions(pixt, &w, &h, NULL);
numaAddNumber(na, (l_float32)w / (l_float32)h);
pixDestroy(&pixt);
}
return na;
}
/*!
* \brief pixaFindWidthHeightProduct()
*
* \param[in] pixa of 1 bpp pix
* \return na of width*height products for each pix, or NULL on error
*
* <pre>
* Notes:
* (1) This is typically used for a pixa consisting of
* 1 bpp connected components.
* </pre>
*/
NUMA *
pixaFindWidthHeightProduct(PIXA *pixa)
{
l_int32 i, n, w, h;
NUMA *na;
PIX *pixt;
PROCNAME("pixaFindWidthHeightProduct");
if (!pixa)
return (NUMA *)ERROR_PTR("pixa not defined", procName, NULL);
n = pixaGetCount(pixa);
na = numaCreate(n);
for (i = 0; i < n; i++) {
pixt = pixaGetPix(pixa, i, L_CLONE);
pixGetDimensions(pixt, &w, &h, NULL);
numaAddNumber(na, w * h);
pixDestroy(&pixt);
}
return na;
}
/*!
* \brief pixFindOverlapFraction()
*
* \param[in] pixs1, pixs2 1 bpp
* \param[in] x2, y2 location in pixs1 of UL corner of pixs2
* \param[in] tab [optional] pixel sum table, can be null
* \param[out] pratio ratio fg intersection to fg union
* \param[out] pnoverlap [optional] number of overlapping pixels
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) The UL corner of pixs2 is placed at (x2, y2) in pixs1.
* (2) This measure is similar to the correlation.
* </pre>
*/
l_ok
pixFindOverlapFraction(PIX *pixs1,
PIX *pixs2,
l_int32 x2,
l_int32 y2,
l_int32 *tab,
l_float32 *pratio,
l_int32 *pnoverlap)
{
l_int32 *tab8;
l_int32 w, h, nintersect, nunion;
PIX *pixt;
PROCNAME("pixFindOverlapFraction");
if (pnoverlap) *pnoverlap = 0;
if (!pratio)
return ERROR_INT("&ratio not defined", procName, 1);
*pratio = 0.0;
if (!pixs1 || pixGetDepth(pixs1) != 1)
return ERROR_INT("pixs1 not defined or not 1 bpp", procName, 1);
if (!pixs2 || pixGetDepth(pixs2) != 1)
return ERROR_INT("pixs2 not defined or not 1 bpp", procName, 1);
if (!tab)
tab8 = makePixelSumTab8();
else
tab8 = tab;
pixGetDimensions(pixs2, &w, &h, NULL);
pixt = pixCopy(NULL, pixs1);
pixRasterop(pixt, x2, y2, w, h, PIX_MASK, pixs2, 0, 0); /* AND */
pixCountPixels(pixt, &nintersect, tab8);
if (pnoverlap)
*pnoverlap = nintersect;
pixCopy(pixt, pixs1);
pixRasterop(pixt, x2, y2, w, h, PIX_PAINT, pixs2, 0, 0); /* OR */
pixCountPixels(pixt, &nunion, tab8);
if (!tab) LEPT_FREE(tab8);
pixDestroy(&pixt);
if (nunion > 0)
*pratio = (l_float32)nintersect / (l_float32)nunion;
return 0;
}
/*!
* \brief pixFindRectangleComps()
*
* \param[in] pixs 1 bpp
* \param[in] dist max distance allowed between bounding box
* and nearest foreground pixel within it
* \param[in] minw, minh minimum size in each direction as a requirement
* for a conforming rectangle
* \return boxa of components that conform, or NULL on error
*
* <pre>
* Notes:
* (1) This applies the function pixConformsToRectangle() to
* each 8-c.c. in pixs, and returns a boxa containing the
* regions of all components that are conforming.
* (2) Conforming components must satisfy both the size constraint
* given by %minsize and the slop in conforming to a rectangle
* determined by %dist.
* </pre>
*/
BOXA *
pixFindRectangleComps(PIX *pixs,
l_int32 dist,
l_int32 minw,
l_int32 minh)
{
l_int32 w, h, i, n, conforms;
BOX *box;
BOXA *boxa, *boxad;
PIX *pix;
PIXA *pixa;
PROCNAME("pixFindRectangleComps");
if (!pixs || pixGetDepth(pixs) != 1)
return (BOXA *)ERROR_PTR("pixs undefined or not 1 bpp", procName, NULL);
if (dist < 0)
return (BOXA *)ERROR_PTR("dist must be >= 0", procName, NULL);
if (minw <= 2 * dist && minh <= 2 * dist)
return (BOXA *)ERROR_PTR("invalid parameters", procName, NULL);
boxa = pixConnComp(pixs, &pixa, 8);
boxad = boxaCreate(0);
n = pixaGetCount(pixa);
for (i = 0; i < n; i++) {
pix = pixaGetPix(pixa, i, L_CLONE);
pixGetDimensions(pix, &w, &h, NULL);
if (w < minw || h < minh) {
pixDestroy(&pix);
continue;
}
pixConformsToRectangle(pix, NULL, dist, &conforms);
if (conforms) {
box = boxaGetBox(boxa, i, L_COPY);
boxaAddBox(boxad, box, L_INSERT);
}
pixDestroy(&pix);
}
boxaDestroy(&boxa);
pixaDestroy(&pixa);
return boxad;
}
/*!
* \brief pixConformsToRectangle()
*
* \param[in] pixs 1 bpp
* \param[in] box [optional] if null, use the entire pixs
* \param[in] dist max distance allowed between bounding box and
* nearest foreground pixel within it
* \param[out] pconforms 0 (false) if not conforming;
* 1 (true) if conforming
* \return 0 if OK, 1 on error
*
* <pre>
* Notes:
* (1) There are several ways to test if a connected component has
* an essentially rectangular boundary, such as:
* a. Fraction of fill into the bounding box
* b. Max-min distance of fg pixel from periphery of bounding box
* c. Max depth of bg intrusions into component within bounding box
* The weakness of (a) is that it is highly sensitive to holes
* within the c.c. The weakness of (b) is that it can have
* arbitrarily large intrusions into the c.c. Method (c) tests
* the integrity of the outer boundary of the c.c., with respect
* to the enclosing bounding box, so we use it.
* (2) This tests if the connected component within the box conforms
* to the box at all points on the periphery within %dist.
* Inside, at a distance from the box boundary that is greater
* than %dist, we don't care about the pixels in the c.c.
* (3) We can think of the conforming condition as follows:
* No pixel inside a distance %dist from the boundary
* can connect to the boundary through a path through the bg.
* To implement this, we need to do a flood fill. We can go
* either from inside toward the boundary, or the other direction.
* It's easiest to fill from the boundary, and then verify that
* there are no filled pixels farther than %dist from the boundary.
* </pre>
*/
l_ok
pixConformsToRectangle(PIX *pixs,
BOX *box,
l_int32 dist,
l_int32 *pconforms)
{
l_int32 w, h, empty;
PIX *pix1, *pix2;
PROCNAME("pixConformsToRectangle");
if (!pconforms)
return ERROR_INT("&conforms not defined", procName, 1);
*pconforms = 0;
if (!pixs || pixGetDepth(pixs) != 1)
return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
if (dist < 0)
return ERROR_INT("dist must be >= 0", procName, 1);
pixGetDimensions(pixs, &w, &h, NULL);
if (w <= 2 * dist || h <= 2 * dist) {
L_WARNING("automatic conformation: distance too large\n", procName);
*pconforms = 1;
return 0;
}
/* Extract the region, if necessary */
if (box)
pix1 = pixClipRectangle(pixs, box, NULL);
else
pix1 = pixCopy(NULL, pixs);
/* Invert and fill from the boundary into the interior.
* Because we're considering the connected component in an
* 8-connected sense, we do the background filling as 4 c.c. */
pixInvert(pix1, pix1);
pix2 = pixExtractBorderConnComps(pix1, 4);
/* Mask out all pixels within a distance %dist from the box
* boundary. Any remaining pixels are from filling that goes
* more than %dist from the boundary. If no pixels remain,
* the component conforms to the bounding rectangle within
* a distance %dist. */
pixSetOrClearBorder(pix2, dist, dist, dist, dist, PIX_CLR);
pixZero(pix2, &empty);
pixDestroy(&pix1);
pixDestroy(&pix2);
*pconforms = (empty) ? 1 : 0;
return 0;
}
/*-----------------------------------------------------------------------*
* Extract rectangular region *
*-----------------------------------------------------------------------*/
/*!
* \brief pixClipRectangles()
*
* \param[in] pixs
* \param[in] boxa requested clipping regions
* \return pixa consisting of requested regions, or NULL on error
*
* <pre>
* Notes:
* (1) The returned pixa includes the actual regions clipped out from
* the input pixs.
* </pre>
*/
PIXA *
pixClipRectangles(PIX *pixs,
BOXA *boxa)
{
l_int32 i, n;
BOX *box, *boxc;
PIX *pix;
PIXA *pixa;
PROCNAME("pixClipRectangles");
if (!pixs)
return (PIXA *)ERROR_PTR("pixs not defined", procName, NULL);
if (!boxa)
return (PIXA *)ERROR_PTR("boxa not defined", procName, NULL);
n = boxaGetCount(boxa);
pixa = pixaCreate(n);
for (i = 0; i < n; i++) {
box = boxaGetBox(boxa, i, L_CLONE);
pix = pixClipRectangle(pixs, box, &boxc);
pixaAddPix(pixa, pix, L_INSERT);
pixaAddBox(pixa, boxc, L_INSERT);
boxDestroy(&box);
}
return pixa;
}
/*!
* \brief pixClipRectangle()
*
* \param[in] pixs
* \param[in] box requested clipping region; const
* \param[out] pboxc [optional] actual box of clipped region
* \return clipped pix, or NULL on error or if rectangle
* doesn't intersect pixs
*
* <pre>
* Notes:
*