Coverage Report

Created: 2024-02-28 06:46

/src/leptonica/src/compare.c
Line
Count
Source (jump to first uncovered line)
1
/*====================================================================*
2
 -  Copyright (C) 2001 Leptonica.  All rights reserved.
3
 -
4
 -  Redistribution and use in source and binary forms, with or without
5
 -  modification, are permitted provided that the following conditions
6
 -  are met:
7
 -  1. Redistributions of source code must retain the above copyright
8
 -     notice, this list of conditions and the following disclaimer.
9
 -  2. Redistributions in binary form must reproduce the above
10
 -     copyright notice, this list of conditions and the following
11
 -     disclaimer in the documentation and/or other materials
12
 -     provided with the distribution.
13
 -
14
 -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15
 -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16
 -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17
 -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
18
 -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19
 -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
 -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21
 -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22
 -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23
 -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
 -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 *====================================================================*/
26
27
/*!
28
 * \file compare.c
29
 * <pre>
30
 *
31
 *      Test for pix equality
32
 *           l_int32     pixEqual()
33
 *           l_int32     pixEqualWithAlpha()
34
 *           l_int32     pixEqualWithCmap()
35
 *           l_int32     cmapEqual()
36
 *           l_int32     pixUsesCmapColor()
37
 *
38
 *      Binary correlation
39
 *           l_int32     pixCorrelationBinary()
40
 *
41
 *      Difference of two images of same size
42
 *           l_int32     pixDisplayDiff()
43
 *           l_int32     pixDisplayDiffBinary()
44
 *           l_int32     pixCompareBinary()
45
 *           l_int32     pixCompareGrayOrRGB()
46
 *           l_int32     pixCompareGray()
47
 *           l_int32     pixCompareRGB()
48
 *           l_int32     pixCompareTiled()
49
 *
50
 *      Other measures of the difference of two images of the same size
51
 *           NUMA       *pixCompareRankDifference()
52
 *           l_int32     pixTestForSimilarity()
53
 *           l_int32     pixGetDifferenceStats()
54
 *           NUMA       *pixGetDifferenceHistogram()
55
 *           l_int32     pixGetPerceptualDiff()
56
 *           l_int32     pixGetPSNR()
57
 *
58
 *      Comparison of photo regions by histogram
59
 *           l_int32     pixaComparePhotoRegionsByHisto()  -- top-level
60
 *           l_int32     pixComparePhotoRegionsByHisto()  -- top-level for 2
61
 *           l_int32     pixGenPhotoHistos()
62
 *           PIX        *pixPadToCenterCentroid()
63
 *           l_int32     pixCentroid8()
64
 *           l_int32     pixDecideIfPhotoImage()
65
 *       static l_int32  findHistoGridDimensions()
66
 *           l_int32     compareTilesByHisto()
67
 *
68
 *           l_int32     pixCompareGrayByHisto()  -- top-level for 2
69
 *       static l_int32  pixCompareTilesByHisto()
70
 *           l_int32     pixCropAlignedToCentroid()
71
 *
72
 *           l_uint8    *l_compressGrayHistograms()
73
 *           NUMAA      *l_uncompressGrayHistograms()
74
 *
75
 *      Translated images at the same resolution
76
 *           l_int32     pixCompareWithTranslation()
77
 *           l_int32     pixBestCorrelation()
78
 *
79
 *  For comparing images using tiled histograms, essentially all the
80
 *  computation goes into deciding if a region of an image is a photo,
81
 *  whether that photo region is amenable to similarity measurements
82
 *  using histograms, and finally the calculation of the gray histograms
83
 *  for each of the tiled regions.  The actual comparison is essentially
84
 *  instantaneous.  Therefore, with a large number of images to compare
85
 *  with each other, it is important to first calculate the histograms
86
 *  for each image.  Then the comparisons, which go as the square of the
87
 *  number of images, actually takes no time.
88
 *
89
 *  A high level function that takes a pixa of images and does
90
 *  all comparisons, pixaComparePhotosByHisto(), uses this split
91
 *  approach.  It pads the images so that the centroid is in the center,
92
 *  which will allow the tiles to be better aligned.
93
 *
94
 *  For testing purposes, two functions are given that do all the work
95
 *  to compare just two photo regions:
96
 *    *  pixComparePhotoRegionsByHisto() uses the split approach, qualifying
97
 *       the images first with pixGenPhotoHistos(), and then comparing
98
 *       with compareTilesByHisto().
99
 *    *  pixCompareGrayByHisto() aligns the two images by centroid
100
 *       and calls pixCompareTilesByHisto() to generate the histograms
101
 *       and do the comparison.
102
 *
103
 * </pre>
104
 */
105
106
#ifdef HAVE_CONFIG_H
107
#include <config_auto.h>
108
#endif  /* HAVE_CONFIG_H */
109
110
#include <string.h>
111
#include <math.h>
112
#include "allheaders.h"
113
114
    /* Small enough to consider equal to 0.0, for plot output */
115
static const l_float32  TINY = 0.00001f;
116
117
static l_ok findHistoGridDimensions(l_int32 n, l_int32 w, l_int32 h,
118
                                    l_int32 *pnx, l_int32 *pny, l_int32 debug);
119
static l_ok pixCompareTilesByHisto(PIX *pix1, PIX *pix2, l_int32 maxgray,
120
                                   l_int32 factor, l_int32 n,
121
                                   l_float32 *pscore, PIXA *pixadebug);
122
123
/*------------------------------------------------------------------*
124
 *                        Test for pix equality                     *
125
 *------------------------------------------------------------------*/
126
/*!
127
 * \brief   pixEqual()
128
 *
129
 * \param[in]    pix1
130
 * \param[in]    pix2
131
 * \param[out]   psame  1 if same; 0 if different
132
 * \return  0 if OK; 1 on error
133
 *
134
 * <pre>
135
 * Notes:
136
 *      (1) Equality is defined as having the same pixel values for
137
 *          each respective image pixel.
138
 *      (2) This works on two pix of any depth.  If one or both pix
139
 *          have a colormap, the depths can be different and the
140
 *          two pix can still be equal.
141
 *      (3) This ignores the alpha component for 32 bpp images.
142
 *      (4) If both pix have colormaps and the depths are equal,
143
 *          use the pixEqualWithCmap() function, which does a fast
144
 *          comparison if the colormaps are identical and a relatively
145
 *          slow comparison otherwise.
146
 *      (5) In all other cases, any existing colormaps must first be
147
 *          removed before doing pixel comparison.  After the colormaps
148
 *          are removed, the resulting two images must have the same depth.
149
 *          The "lowest common denominator" is RGB, but this is only
150
 *          chosen when necessary, or when both have colormaps but
151
 *          different depths.
152
 *      (6) For images without colormaps that are not 32 bpp, all bits
153
 *          in the image part of the data array must be identical.
154
 * </pre>
155
 */
156
l_ok
157
pixEqual(PIX      *pix1,
158
         PIX      *pix2,
159
         l_int32  *psame)
160
0
{
161
0
    return pixEqualWithAlpha(pix1, pix2, 0, psame);
162
0
}
163
164
165
/*!
166
 * \brief   pixEqualWithAlpha()
167
 *
168
 * \param[in]    pix1
169
 * \param[in]    pix2
170
 * \param[in]    use_alpha   1 to compare alpha in RGBA; 0 to ignore
171
 * \param[out]   psame       1 if same; 0 if different
172
 * \return  0 if OK; 1 on error
173
 *
174
 * <pre>
175
 * Notes:
176
 *      (1) See notes in pixEqual().
177
 *      (2) This is more general than pixEqual(), in that for 32 bpp
178
 *          RGBA images, where spp = 4, you can optionally include
179
 *          the alpha component in the comparison.
180
 * </pre>
181
 */
182
l_ok
183
pixEqualWithAlpha(PIX      *pix1,
184
                  PIX      *pix2,
185
                  l_int32   use_alpha,
186
                  l_int32  *psame)
187
0
{
188
0
l_int32    w1, h1, d1, w2, h2, d2, wpl1, wpl2;
189
0
l_int32    spp1, spp2, i, j, color, mismatch, opaque;
190
0
l_int32    fullwords, linebits, endbits;
191
0
l_uint32   endmask, wordmask;
192
0
l_uint32  *data1, *data2, *line1, *line2;
193
0
PIX       *pixs1, *pixs2, *pixt1, *pixt2, *pixalpha;
194
0
PIXCMAP   *cmap1, *cmap2;
195
196
0
    if (!psame)
197
0
        return ERROR_INT("psame not defined", __func__, 1);
198
0
    *psame = 0;  /* init to not equal */
199
0
    if (!pix1 || !pix2)
200
0
        return ERROR_INT("pix1 and pix2 not both defined", __func__, 1);
201
0
    pixGetDimensions(pix1, &w1, &h1, &d1);
202
0
    pixGetDimensions(pix2, &w2, &h2, &d2);
203
0
    if (w1 != w2 || h1 != h2) {
204
0
        L_INFO("pix sizes differ\n", __func__);
205
0
        return 0;
206
0
    }
207
208
        /* Suppose the use_alpha flag is true.
209
         * If only one of two 32 bpp images has spp == 4, we call that
210
         * a "mismatch" of the alpha component.  In the case of a mismatch,
211
         * if the 4 bpp pix does not have all alpha components opaque (255),
212
         * the images are not-equal.  However if they are all opaque,
213
         * this image is equivalent to spp == 3, so we allow the
214
         * comparison to go forward, testing only for the RGB equality. */
215
0
    spp1 = pixGetSpp(pix1);
216
0
    spp2 = pixGetSpp(pix2);
217
0
    mismatch = 0;
218
0
    if (use_alpha && d1 == 32 && d2 == 32) {
219
0
        mismatch = ((spp1 == 4 && spp2 != 4) || (spp1 != 4 && spp2 == 4));
220
0
        if (mismatch) {
221
0
            pixalpha = (spp1 == 4) ? pix1 : pix2;
222
0
            pixAlphaIsOpaque(pixalpha, &opaque);
223
0
            if (!opaque) {
224
0
                L_INFO("just one pix has a non-opaque alpha layer\n", __func__);
225
0
                return 0;
226
0
            }
227
0
        }
228
0
    }
229
230
0
    cmap1 = pixGetColormap(pix1);
231
0
    cmap2 = pixGetColormap(pix2);
232
0
    if (!cmap1 && !cmap2 && (d1 != d2) && (d1 == 32 || d2 == 32)) {
233
0
        L_INFO("no colormaps, pix depths unequal, and one of them is RGB\n",
234
0
               __func__);
235
0
        return 0;
236
0
    }
237
238
0
    if (cmap1 && cmap2 && (d1 == d2))   /* use special function */
239
0
        return pixEqualWithCmap(pix1, pix2, psame);
240
241
        /* Must remove colormaps if they exist, and in the process
242
         * end up with the resulting images having the same depth. */
243
0
    if (cmap1 && !cmap2) {
244
0
        pixUsesCmapColor(pix1, &color);
245
0
        if (color && d2 <= 8)  /* can't be equal */
246
0
            return 0;
247
0
        if (d2 < 8)
248
0
            pixs2 = pixConvertTo8(pix2, FALSE);
249
0
        else
250
0
            pixs2 = pixClone(pix2);
251
0
        if (d2 <= 8)
252
0
            pixs1 = pixRemoveColormap(pix1, REMOVE_CMAP_TO_GRAYSCALE);
253
0
        else
254
0
            pixs1 = pixRemoveColormap(pix1, REMOVE_CMAP_TO_FULL_COLOR);
255
0
    } else if (!cmap1 && cmap2) {
256
0
        pixUsesCmapColor(pix2, &color);
257
0
        if (color && d1 <= 8)  /* can't be equal */
258
0
            return 0;
259
0
        if (d1 < 8)
260
0
            pixs1 = pixConvertTo8(pix1, FALSE);
261
0
        else
262
0
            pixs1 = pixClone(pix1);
263
0
        if (d1 <= 8)
264
0
            pixs2 = pixRemoveColormap(pix2, REMOVE_CMAP_TO_GRAYSCALE);
265
0
        else
266
0
            pixs2 = pixRemoveColormap(pix2, REMOVE_CMAP_TO_FULL_COLOR);
267
0
    } else if (cmap1 && cmap2) {  /* depths not equal; use rgb */
268
0
        pixs1 = pixRemoveColormap(pix1, REMOVE_CMAP_TO_FULL_COLOR);
269
0
        pixs2 = pixRemoveColormap(pix2, REMOVE_CMAP_TO_FULL_COLOR);
270
0
    } else {  /* no colormaps */
271
0
        pixs1 = pixClone(pix1);
272
0
        pixs2 = pixClone(pix2);
273
0
    }
274
275
        /* OK, we have no colormaps, but the depths may still be different */
276
0
    d1 = pixGetDepth(pixs1);
277
0
    d2 = pixGetDepth(pixs2);
278
0
    if (d1 != d2) {
279
0
        if (d1 == 16 || d2 == 16) {
280
0
            L_INFO("one pix is 16 bpp\n", __func__);
281
0
            pixDestroy(&pixs1);
282
0
            pixDestroy(&pixs2);
283
0
            return 0;
284
0
        }
285
0
        pixt1 = pixConvertLossless(pixs1, 8);
286
0
        pixt2 = pixConvertLossless(pixs2, 8);
287
0
        if (!pixt1 || !pixt2) {
288
0
            L_INFO("failure to convert to 8 bpp\n", __func__);
289
0
            pixDestroy(&pixs1);
290
0
            pixDestroy(&pixs2);
291
0
            pixDestroy(&pixt1);
292
0
            pixDestroy(&pixt2);
293
0
            return 0;
294
0
        }
295
0
    } else {
296
0
        pixt1 = pixClone(pixs1);
297
0
        pixt2 = pixClone(pixs2);
298
0
    }
299
0
    pixDestroy(&pixs1);
300
0
    pixDestroy(&pixs2);
301
302
        /* No colormaps, equal depths; do pixel comparisons */
303
0
    d1 = pixGetDepth(pixt1);
304
0
    d2 = pixGetDepth(pixt2);
305
0
    wpl1 = pixGetWpl(pixt1);
306
0
    wpl2 = pixGetWpl(pixt2);
307
0
    data1 = pixGetData(pixt1);
308
0
    data2 = pixGetData(pixt2);
309
310
0
    if (d1 == 32) {  /* test either RGB or RGBA pixels */
311
0
        if (use_alpha && !mismatch)
312
0
            wordmask = (spp1 == 3) ? 0xffffff00 : 0xffffffff;
313
0
        else
314
0
            wordmask = 0xffffff00;
315
0
        for (i = 0; i < h1; i++) {
316
0
            line1 = data1 + wpl1 * i;
317
0
            line2 = data2 + wpl2 * i;
318
0
            for (j = 0; j < wpl1; j++) {
319
0
                if ((*line1 ^ *line2) & wordmask) {
320
0
                    pixDestroy(&pixt1);
321
0
                    pixDestroy(&pixt2);
322
0
                    return 0;
323
0
                }
324
0
                line1++;
325
0
                line2++;
326
0
            }
327
0
        }
328
0
    } else {  /* all bits count */
329
0
        linebits = d1 * w1;
330
0
        fullwords = linebits / 32;
331
0
        endbits = linebits & 31;
332
0
        endmask = (endbits == 0) ? 0 : (0xffffffff << (32 - endbits));
333
0
        for (i = 0; i < h1; i++) {
334
0
            line1 = data1 + wpl1 * i;
335
0
            line2 = data2 + wpl2 * i;
336
0
            for (j = 0; j < fullwords; j++) {
337
0
                if (*line1 ^ *line2) {
338
0
                    pixDestroy(&pixt1);
339
0
                    pixDestroy(&pixt2);
340
0
                    return 0;
341
0
                }
342
0
                line1++;
343
0
                line2++;
344
0
            }
345
0
            if (endbits) {
346
0
                if ((*line1 ^ *line2) & endmask) {
347
0
                    pixDestroy(&pixt1);
348
0
                    pixDestroy(&pixt2);
349
0
                    return 0;
350
0
                }
351
0
            }
352
0
        }
353
0
    }
354
355
0
    pixDestroy(&pixt1);
356
0
    pixDestroy(&pixt2);
357
0
    *psame = 1;
358
0
    return 0;
359
0
}
360
361
362
/*!
363
 * \brief   pixEqualWithCmap()
364
 *
365
 * \param[in]    pix1
366
 * \param[in]    pix2
367
 * \param[out]   psame
368
 * \return  0 if OK, 1 on error
369
 *
370
 * <pre>
371
 * Notes:
372
 *      (1) This returns same = TRUE if the images have identical content.
373
 *      (2) Both pix must have a colormap, and be of equal size and depth.
374
 *          If these conditions are not satisfied, it is not an error;
375
 *          the returned result is same = FALSE.
376
 *      (3) We then check whether the colormaps are the same; if so,
377
 *          the comparison proceeds 32 bits at a time.
378
 *      (4) If the colormaps are different, the comparison is done by
379
 *          slow brute force.
380
 * </pre>
381
 */
382
l_ok
383
pixEqualWithCmap(PIX      *pix1,
384
                 PIX      *pix2,
385
                 l_int32  *psame)
386
0
{
387
0
l_int32    d, w, h, wpl1, wpl2, i, j, linebits, fullwords, endbits;
388
0
l_int32    rval1, rval2, gval1, gval2, bval1, bval2, samecmaps;
389
0
l_uint32   endmask, val1, val2;
390
0
l_uint32  *data1, *data2, *line1, *line2;
391
0
PIXCMAP   *cmap1, *cmap2;
392
393
0
    if (!psame)
394
0
        return ERROR_INT("&same not defined", __func__, 1);
395
0
    *psame = 0;
396
0
    if (!pix1)
397
0
        return ERROR_INT("pix1 not defined", __func__, 1);
398
0
    if (!pix2)
399
0
        return ERROR_INT("pix2 not defined", __func__, 1);
400
401
0
    if (pixSizesEqual(pix1, pix2) == 0)
402
0
        return 0;
403
0
    cmap1 = pixGetColormap(pix1);
404
0
    cmap2 = pixGetColormap(pix2);
405
0
    if (!cmap1 || !cmap2) {
406
0
        L_INFO("both images don't have colormap\n", __func__);
407
0
        return 0;
408
0
    }
409
0
    pixGetDimensions(pix1, &w, &h, &d);
410
0
    if (d != 1 && d != 2 && d != 4 && d != 8) {
411
0
        L_INFO("pix depth not in {1, 2, 4, 8}\n", __func__);
412
0
        return 0;
413
0
    }
414
415
0
    cmapEqual(cmap1, cmap2, 3, &samecmaps);
416
0
    if (samecmaps == TRUE) {  /* colormaps are identical; compare by words */
417
0
        linebits = d * w;
418
0
        wpl1 = pixGetWpl(pix1);
419
0
        wpl2 = pixGetWpl(pix2);
420
0
        data1 = pixGetData(pix1);
421
0
        data2 = pixGetData(pix2);
422
0
        fullwords = linebits / 32;
423
0
        endbits = linebits & 31;
424
0
        endmask = (endbits == 0) ? 0 : (0xffffffff << (32 - endbits));
425
0
        for (i = 0; i < h; i++) {
426
0
            line1 = data1 + wpl1 * i;
427
0
            line2 = data2 + wpl2 * i;
428
0
            for (j = 0; j < fullwords; j++) {
429
0
                if (*line1 ^ *line2)
430
0
                    return 0;
431
0
                line1++;
432
0
                line2++;
433
0
            }
434
0
            if (endbits) {
435
0
                if ((*line1 ^ *line2) & endmask)
436
0
                    return 0;
437
0
            }
438
0
        }
439
0
        *psame = 1;
440
0
        return 0;
441
0
    }
442
443
        /* Colormaps aren't identical; compare pixel by pixel */
444
0
    for (i = 0; i < h; i++) {
445
0
        for (j = 0; j < w; j++) {
446
0
            pixGetPixel(pix1, j, i, &val1);
447
0
            pixGetPixel(pix2, j, i, &val2);
448
0
            pixcmapGetColor(cmap1, val1, &rval1, &gval1, &bval1);
449
0
            pixcmapGetColor(cmap2, val2, &rval2, &gval2, &bval2);
450
0
            if (rval1 != rval2 || gval1 != gval2 || bval1 != bval2)
451
0
                return 0;
452
0
        }
453
0
    }
454
455
0
    *psame = 1;
456
0
    return 0;
457
0
}
458
459
460
/*!
461
 * \brief   cmapEqual()
462
 *
463
 * \param[in]    cmap1
464
 * \param[in]    cmap2
465
 * \param[in]    ncomps  3 for RGB, 4 for RGBA
466
 * \param[out]   psame
467
 * \return  0 if OK, 1 on error
468
 *
469
 * <pre>
470
 * Notes:
471
 *      (1) This returns %same = TRUE if the colormaps have identical entries.
472
 *      (2) If %ncomps == 4, the alpha components of the colormaps are also
473
 *          compared.
474
 * </pre>
475
 */
476
l_ok
477
cmapEqual(PIXCMAP  *cmap1,
478
          PIXCMAP  *cmap2,
479
          l_int32   ncomps,
480
          l_int32  *psame)
481
0
{
482
0
l_int32  n1, n2, i, rval1, rval2, gval1, gval2, bval1, bval2, aval1, aval2;
483
484
0
    if (!psame)
485
0
        return ERROR_INT("&same not defined", __func__, 1);
486
0
    *psame = FALSE;
487
0
    if (!cmap1)
488
0
        return ERROR_INT("cmap1 not defined", __func__, 1);
489
0
    if (!cmap2)
490
0
        return ERROR_INT("cmap2 not defined", __func__, 1);
491
0
    if (ncomps != 3 && ncomps != 4)
492
0
        return ERROR_INT("ncomps not 3 or 4", __func__, 1);
493
494
0
    n1 = pixcmapGetCount(cmap1);
495
0
    n2 = pixcmapGetCount(cmap2);
496
0
    if (n1 != n2) {
497
0
        L_INFO("colormap sizes are different\n", __func__);
498
0
        return 0;
499
0
    }
500
501
0
    for (i = 0; i < n1; i++) {
502
0
        pixcmapGetRGBA(cmap1, i, &rval1, &gval1, &bval1, &aval1);
503
0
        pixcmapGetRGBA(cmap2, i, &rval2, &gval2, &bval2, &aval2);
504
0
        if (rval1 != rval2 || gval1 != gval2 || bval1 != bval2)
505
0
            return 0;
506
0
        if (ncomps == 4 && aval1 != aval2)
507
0
            return 0;
508
0
    }
509
0
    *psame = TRUE;
510
0
    return 0;
511
0
}
512
513
514
/*!
515
 * \brief   pixUsesCmapColor()
516
 *
517
 * \param[in]    pixs     any depth, colormap
518
 * \param[out]   pcolor   TRUE if color found
519
 * \return  0 if OK, 1 on error
520
 *
521
 * <pre>
522
 * Notes:
523
 *      (1) This returns color = TRUE if three things are obtained:
524
 *          (a) the pix has a colormap
525
 *          (b) the colormap has at least one color entry
526
 *          (c) a color entry is actually used
527
 *      (2) It is used in pixEqual() for comparing two images, in a
528
 *          situation where it is required to know if the colormap
529
 *          has color entries that are actually used in the image.
530
 * </pre>
531
 */
532
l_ok
533
pixUsesCmapColor(PIX      *pixs,
534
                 l_int32  *pcolor)
535
0
{
536
0
l_int32   n, i, rval, gval, bval, numpix;
537
0
NUMA     *na;
538
0
PIXCMAP  *cmap;
539
540
0
    if (!pcolor)
541
0
        return ERROR_INT("&color not defined", __func__, 1);
542
0
    *pcolor = 0;
543
0
    if (!pixs)
544
0
        return ERROR_INT("pixs not defined", __func__, 1);
545
546
0
    if ((cmap = pixGetColormap(pixs)) == NULL)
547
0
        return 0;
548
549
0
    pixcmapHasColor(cmap, pcolor);
550
0
    if (*pcolor == 0)  /* no color */
551
0
        return 0;
552
553
        /* The cmap has color entries.  Are they used? */
554
0
    na = pixGetGrayHistogram(pixs, 1);
555
0
    n = pixcmapGetCount(cmap);
556
0
    for (i = 0; i < n; i++) {
557
0
        pixcmapGetColor(cmap, i, &rval, &gval, &bval);
558
0
        numaGetIValue(na, i, &numpix);
559
0
        if ((rval != gval || rval != bval) && numpix) {  /* color found! */
560
0
            *pcolor = 1;
561
0
            break;
562
0
        }
563
0
    }
564
0
    numaDestroy(&na);
565
566
0
    return 0;
567
0
}
568
569
570
/*------------------------------------------------------------------*
571
 *                          Binary correlation                      *
572
 *------------------------------------------------------------------*/
573
/*!
574
 * \brief   pixCorrelationBinary()
575
 *
576
 * \param[in]    pix1    1 bpp
577
 * \param[in]    pix2    1 bpp
578
 * \param[out]   pval    correlation
579
 * \return  0 if OK; 1 on error
580
 *
581
 * <pre>
582
 * Notes:
583
 *      (1) The correlation is a number between 0.0 and 1.0,
584
 *          based on foreground similarity:
585
 *                           (|1 AND 2|)**2
586
 *            correlation =  --------------
587
 *                             |1| * |2|
588
 *          where |x| is the count of foreground pixels in image x.
589
 *          If the images are identical, this is 1.0.
590
 *          If they have no fg pixels in common, this is 0.0.
591
 *          If one or both images have no fg pixels, the correlation is 0.0.
592
 *      (2) Typically the two images are of equal size, but this
593
 *          is not enforced.  Instead, the UL corners are aligned.
594
 * </pre>
595
 */
596
l_ok
597
pixCorrelationBinary(PIX        *pix1,
598
                     PIX        *pix2,
599
                     l_float32  *pval)
600
0
{
601
0
l_int32   count1, count2, countn;
602
0
l_int32  *tab8;
603
0
PIX      *pixn;
604
605
0
    if (!pval)
606
0
        return ERROR_INT("&pval not defined", __func__, 1);
607
0
    *pval = 0.0;
608
0
    if (!pix1)
609
0
        return ERROR_INT("pix1 not defined", __func__, 1);
610
0
    if (!pix2)
611
0
        return ERROR_INT("pix2 not defined", __func__, 1);
612
613
0
    tab8 = makePixelSumTab8();
614
0
    pixCountPixels(pix1, &count1, tab8);
615
0
    pixCountPixels(pix2, &count2, tab8);
616
0
    if (count1 == 0 || count2 == 0) {
617
0
        LEPT_FREE(tab8);
618
0
        return 0;
619
0
    }
620
0
    pixn = pixAnd(NULL, pix1, pix2);
621
0
    pixCountPixels(pixn, &countn, tab8);
622
0
    *pval = (l_float32)countn * (l_float32)countn /
623
0
              ((l_float32)count1 * (l_float32)count2);
624
0
    LEPT_FREE(tab8);
625
0
    pixDestroy(&pixn);
626
0
    return 0;
627
0
}
628
629
630
/*------------------------------------------------------------------*
631
 *                   Difference of two images                       *
632
 *------------------------------------------------------------------*/
633
/*!
634
 * \brief   pixDisplayDiff()
635
 *
636
 * \param[in]    pix1       any depth
637
 * \param[in]    pix2       any depth
638
 * \param[in]    showall    1 to display input images; 0 to only display result
639
 * \param[in]    mindiff    min difference to identify pixel
640
 * \param[in]    diffcolor  color of pixel indicating difference >= mindiff
641
 * \return  pixd  32 bpp rgb, or NULL on error
642
 *
643
 * <pre>
644
 * Notes:
645
 *      (1) This aligns the UL corners of pix1 and pix2, crops to the
646
 *          overlapping pixels, and shows which pixels have a significant
647
 *          difference in value.
648
 *      (2) Requires %pix1 and %pix2 to have the same depth.
649
 *      (3) If rgb, a pixel is identified as different if any component
650
 *          values of the corresponding pixels equals or exceeds %mindiff.
651
 *      (4) %diffcolor is in format 0xrrggbbaa.
652
 *      (5) If %pix1 and %pix2 are 1 bpp, ignores %mindiff and %diffcolor,
653
 *          and uses the result of pixDisplayDiffBinary().
654
 * </pre>
655
 */
656
PIX *
657
pixDisplayDiff(PIX      *pix1,
658
               PIX      *pix2,
659
               l_int32   showall,
660
               l_int32   mindiff,
661
               l_uint32  diffcolor)
662
0
{
663
0
l_int32    i, j, w1, h1, d1, w2, h2, d2, minw, minh, wpl1, wpl2, wpl3;
664
0
l_int32    rval1, gval1, bval1, rval2, gval2, bval2;
665
0
l_uint32   val1, val2;
666
0
l_uint32  *data1, *data2, *data3, *line1, *line2, *line3;
667
0
PIX       *pix3 = NULL, *pix4 = NULL, *pixd;
668
0
PIXA      *pixa1;
669
670
0
    if (!pix1 || !pix2)
671
0
        return (PIX *)ERROR_PTR("pix1, pix2 not both defined", __func__, NULL);
672
0
    pixGetDimensions(pix1, &w1, &h1, &d1);
673
0
    pixGetDimensions(pix2, &w2, &h2, &d2);
674
0
    if (d1 != d2)
675
0
        return (PIX *)ERROR_PTR("unequal depths", __func__, NULL);
676
0
    if (mindiff <= 0)
677
0
        return (PIX *)ERROR_PTR("mindiff must be > 0", __func__, NULL);
678
679
0
    if (d1 == 1) {
680
0
        pix3 = pixDisplayDiffBinary(pix1, pix2);
681
0
        pixd = pixConvertTo32(pix3); 
682
0
        pixDestroy(&pix3);
683
0
    } else {
684
0
        minw = L_MIN(w1, w2);
685
0
        minh = L_MIN(h1, h2);
686
0
        pix3 = pixConvertTo32(pix1);
687
0
        pix4 = pixConvertTo32(pix2);
688
0
        pixd = pixCreate(minw, minh, 32);
689
0
        pixRasterop(pixd, 0, 0, minw, minh, PIX_SRC, pix3, 0, 0);
690
0
        data1 = pixGetData(pix3);
691
0
        wpl1 = pixGetWpl(pix3);
692
0
        data2 = pixGetData(pix4);
693
0
        wpl2 = pixGetWpl(pix4);
694
0
        data3 = pixGetData(pixd);
695
0
        wpl3 = pixGetWpl(pixd);
696
0
        for (i = 0; i < minh; i++) {
697
0
            line1 = data1 + i * wpl1;
698
0
            line2 = data2 + i * wpl2;
699
0
            line3 = data3 + i * wpl3;
700
0
            for (j = 0; j < minw; j++) {
701
0
                val1 = GET_DATA_FOUR_BYTES(line1, j);
702
0
                val2 = GET_DATA_FOUR_BYTES(line2, j);
703
0
                extractRGBValues(val1, &rval1, &gval1, &bval1);
704
0
                extractRGBValues(val2, &rval2, &gval2, &bval2);
705
0
                if (L_ABS(rval1 - rval2) >= mindiff ||
706
0
                    L_ABS(gval1 - gval2) >= mindiff ||
707
0
                    L_ABS(bval1 - bval2) >= mindiff)
708
0
                    SET_DATA_FOUR_BYTES(line3, j, diffcolor);
709
0
            }
710
0
        }
711
0
    }
712
                
713
0
    if (showall) {
714
0
        pixa1 = pixaCreate(3);
715
0
        if (d1 == 1) {
716
0
            pixaAddPix(pixa1, pix1, L_COPY);
717
0
            pixaAddPix(pixa1, pix2, L_COPY);
718
0
        } else {
719
0
            pixaAddPix(pixa1, pix3, L_INSERT);
720
0
            pixaAddPix(pixa1, pix4, L_INSERT);
721
0
        }
722
0
        pixaAddPix(pixa1, pixd, L_INSERT);  /* save diff image */
723
0
        pixd = pixaDisplayTiledInColumns(pixa1, 2, 1.0, 30, 2);  /* all 3 */
724
0
        pixaDestroy(&pixa1);
725
0
    }
726
0
    return pixd;
727
0
}
728
729
730
/*!
731
 * \brief   pixDisplayDiffBinary()
732
 *
733
 * \param[in]    pix1    1 bpp
734
 * \param[in]    pix2    1 bpp
735
 * \return  pixd 4 bpp cmapped, or NULL on error
736
 *
737
 * <pre>
738
 * Notes:
739
 *      (1) This gives a color representation of the difference between
740
 *          pix1 and pix2.  The color difference depends on the order.
741
 *          The pixels in pixd have 4 colors:
742
 *           * unchanged:  black (on), white (off)
743
 *           * on in pix1, off in pix2: red
744
 *           * on in pix2, off in pix1: green
745
 *      (2) This aligns the UL corners of pix1 and pix2, and crops
746
 *          to the overlapping pixels.
747
 * </pre>
748
 */
749
PIX *
750
pixDisplayDiffBinary(PIX  *pix1,
751
                     PIX  *pix2)
752
0
{
753
0
l_int32   w1, h1, d1, w2, h2, d2, minw, minh;
754
0
PIX      *pixt, *pixd;
755
0
PIXCMAP  *cmap;
756
757
0
    if (!pix1 || !pix2)
758
0
        return (PIX *)ERROR_PTR("pix1, pix2 not both defined", __func__, NULL);
759
0
    pixGetDimensions(pix1, &w1, &h1, &d1);
760
0
    pixGetDimensions(pix2, &w2, &h2, &d2);
761
0
    if (d1 != 1 || d2 != 1)
762
0
        return (PIX *)ERROR_PTR("pix1 and pix2 not 1 bpp", __func__, NULL);
763
0
    minw = L_MIN(w1, w2);
764
0
    minh = L_MIN(h1, h2);
765
766
0
    pixd = pixCreate(minw, minh, 4);
767
0
    cmap = pixcmapCreate(4);
768
0
    pixcmapAddColor(cmap, 255, 255, 255);  /* initialized to white */
769
0
    pixcmapAddColor(cmap, 0, 0, 0);
770
0
    pixcmapAddColor(cmap, 255, 0, 0);
771
0
    pixcmapAddColor(cmap, 0, 255, 0);
772
0
    pixSetColormap(pixd, cmap);
773
774
0
    pixt = pixAnd(NULL, pix1, pix2);
775
0
    pixPaintThroughMask(pixd, pixt, 0, 0, 0x0);  /* black */
776
0
    pixSubtract(pixt, pix1, pix2);
777
0
    pixPaintThroughMask(pixd, pixt, 0, 0, 0xff000000);  /* red */
778
0
    pixSubtract(pixt, pix2, pix1);
779
0
    pixPaintThroughMask(pixd, pixt, 0, 0, 0x00ff0000);  /* green */
780
0
    pixDestroy(&pixt);
781
0
    return pixd;
782
0
}
783
784
785
/*!
786
 * \brief   pixCompareBinary()
787
 *
788
 * \param[in]    pix1       1 bpp
789
 * \param[in]    pix2       1 bpp
790
 * \param[in]    comptype   L_COMPARE_XOR, L_COMPARE_SUBTRACT
791
 * \param[out]   pfract     fraction of pixels that are different
792
 * \param[out]   ppixdiff   [optional] pix of difference
793
 * \return  0 if OK; 1 on error
794
 *
795
 * <pre>
796
 * Notes:
797
 *      (1) The two images are aligned at the UL corner, and do not
798
 *          need to be the same size.
799
 *      (2) If using L_COMPARE_SUBTRACT, pix2 is subtracted from pix1.
800
 *      (3) The total number of pixels is determined by pix1.
801
 *      (4) On error, the returned fraction is 1.0.
802
 * </pre>
803
 */
804
l_ok
805
pixCompareBinary(PIX        *pix1,
806
                 PIX        *pix2,
807
                 l_int32     comptype,
808
                 l_float32  *pfract,
809
                 PIX       **ppixdiff)
810
0
{
811
0
l_int32   w, h, count;
812
0
PIX      *pixt;
813
814
0
    if (ppixdiff) *ppixdiff = NULL;
815
0
    if (!pfract)
816
0
        return ERROR_INT("&pfract not defined", __func__, 1);
817
0
    *pfract = 1.0;  /* initialize to max difference */
818
0
    if (!pix1 || pixGetDepth(pix1) != 1)
819
0
        return ERROR_INT("pix1 not defined or not 1 bpp", __func__, 1);
820
0
    if (!pix2 || pixGetDepth(pix2) != 1)
821
0
        return ERROR_INT("pix2 not defined or not 1 bpp", __func__, 1);
822
0
    if (comptype != L_COMPARE_XOR && comptype != L_COMPARE_SUBTRACT)
823
0
        return ERROR_INT("invalid comptype", __func__, 1);
824
825
0
    if (comptype == L_COMPARE_XOR)
826
0
        pixt = pixXor(NULL, pix1, pix2);
827
0
    else  /* comptype == L_COMPARE_SUBTRACT) */
828
0
        pixt = pixSubtract(NULL, pix1, pix2);
829
0
    pixCountPixels(pixt, &count, NULL);
830
0
    pixGetDimensions(pix1, &w, &h, NULL);
831
0
    *pfract = (l_float32)(count) / (l_float32)(w * h);
832
833
0
    if (ppixdiff)
834
0
        *ppixdiff = pixt;
835
0
    else
836
0
        pixDestroy(&pixt);
837
0
    return 0;
838
0
}
839
840
841
/*!
842
 * \brief   pixCompareGrayOrRGB()
843
 *
844
 * \param[in]    pix1      2,4,8,16 bpp gray, 32 bpp rgb, or colormapped
845
 * \param[in]    pix2      2,4,8,16 bpp gray, 32 bpp rgb, or colormapped
846
 * \param[in]    comptype  L_COMPARE_SUBTRACT, L_COMPARE_ABS_DIFF
847
 * \param[in]    plottype  gplot plot output type, or 0 for no plot
848
 * \param[out]   psame     [optional] 1 if pixel values are identical
849
 * \param[out]   pdiff     [optional] average difference
850
 * \param[out]   prmsdiff  [optional] rms of difference
851
 * \param[out]   ppixdiff  [optional] pix of difference
852
 * \return  0 if OK; 1 on error
853
 *
854
 * <pre>
855
 * Notes:
856
 *      (1) The two images are aligned at the UL corner, and do not
857
 *          need to be the same size.  If they are not the same size,
858
 *          the comparison will be made over overlapping pixels.
859
 *      (2) If there is a colormap, it is removed and the result
860
 *          is either gray or RGB depending on the colormap.
861
 *      (3) If RGB, each component is compared separately.
862
 *      (4) If type is L_COMPARE_ABS_DIFF, pix2 is subtracted from pix1
863
 *          and the absolute value is taken.
864
 *      (5) If type is L_COMPARE_SUBTRACT, pix2 is subtracted from pix1
865
 *          and the result is clipped to 0.
866
 *      (6) The plot output types are specified in gplot.h.
867
 *          Use 0 if no difference plot is to be made.
868
 *      (7) If the images are pixelwise identical, no difference
869
 *          plot is made, even if requested.  The result (TRUE or FALSE)
870
 *          is optionally returned in the parameter 'same'.
871
 *      (8) The average difference (either subtracting or absolute value)
872
 *          is optionally returned in the parameter 'diff'.
873
 *      (9) The RMS difference is optionally returned in the
874
 *          parameter 'rmsdiff'.  For RGB, we return the average of
875
 *          the RMS differences for each of the components.
876
 *     (10) Because pixel values are compared, pix1 and pix2 can be equal when:
877
 *          * they are both gray with different depth
878
 *          * one is colormapped and the other is not
879
 *          * they are both colormapped and have different size colormaps
880
 * </pre>
881
 */
882
l_ok
883
pixCompareGrayOrRGB(PIX        *pix1,
884
                    PIX        *pix2,
885
                    l_int32     comptype,
886
                    l_int32     plottype,
887
                    l_int32    *psame,
888
                    l_float32  *pdiff,
889
                    l_float32  *prmsdiff,
890
                    PIX       **ppixdiff)
891
0
{
892
0
l_int32  retval, d1, d2;
893
0
PIX     *pixt1, *pixt2, *pixs1, *pixs2;
894
895
0
    if (psame) *psame = 0;
896
0
    if (pdiff) *pdiff = 255.0;
897
0
    if (prmsdiff) *prmsdiff = 255.0;
898
0
    if (ppixdiff) *ppixdiff = NULL;
899
0
    if (!pix1 || pixGetDepth(pix1) == 1)
900
0
        return ERROR_INT("pix1 not defined or 1 bpp", __func__, 1);
901
0
    if (!pix2 || pixGetDepth(pix2) == 1)
902
0
        return ERROR_INT("pix2 not defined or 1 bpp", __func__, 1);
903
0
    if (comptype != L_COMPARE_SUBTRACT && comptype != L_COMPARE_ABS_DIFF)
904
0
        return ERROR_INT("invalid comptype", __func__, 1);
905
0
    if (plottype < 0 || plottype >= NUM_GPLOT_OUTPUTS)
906
0
        return ERROR_INT("invalid plottype", __func__, 1);
907
908
0
    pixt1 = pixRemoveColormap(pix1, REMOVE_CMAP_BASED_ON_SRC);
909
0
    pixt2 = pixRemoveColormap(pix2, REMOVE_CMAP_BASED_ON_SRC);
910
0
    d1 = pixGetDepth(pixt1);
911
0
    d2 = pixGetDepth(pixt2);
912
0
    if (d1 < 8)
913
0
        pixs1 = pixConvertTo8(pixt1, FALSE);
914
0
    else
915
0
        pixs1 = pixClone(pixt1);
916
0
    if (d2 < 8)
917
0
        pixs2 = pixConvertTo8(pixt2, FALSE);
918
0
    else
919
0
        pixs2 = pixClone(pixt2);
920
0
    pixDestroy(&pixt1);
921
0
    pixDestroy(&pixt2);
922
0
    d1 = pixGetDepth(pixs1);
923
0
    d2 = pixGetDepth(pixs2);
924
0
    if (d1 != d2) {
925
0
        pixDestroy(&pixs1);
926
0
        pixDestroy(&pixs2);
927
0
        return ERROR_INT("intrinsic depths are not equal", __func__, 1);
928
0
    }
929
930
0
    if (d1 == 8 || d1 == 16)
931
0
        retval = pixCompareGray(pixs1, pixs2, comptype, plottype, psame,
932
0
                                pdiff, prmsdiff, ppixdiff);
933
0
    else  /* d1 == 32 */
934
0
        retval = pixCompareRGB(pixs1, pixs2, comptype, plottype, psame,
935
0
                               pdiff, prmsdiff, ppixdiff);
936
0
    pixDestroy(&pixs1);
937
0
    pixDestroy(&pixs2);
938
0
    return retval;
939
0
}
940
941
942
/*!
943
 * \brief   pixCompareGray()
944
 *
945
 * \param[in]    pix1       8 or 16 bpp, not cmapped
946
 * \param[in]    pix2       8 or 16 bpp, not cmapped
947
 * \param[in]    comptype   L_COMPARE_SUBTRACT, L_COMPARE_ABS_DIFF
948
 * \param[in]    plottype   gplot plot output type, or 0 for no plot
949
 * \param[out]   psame      [optional] 1 if pixel values are identical
950
 * \param[out]   pdiff      [optional] average difference
951
 * \param[out]   prmsdiff   [optional] rms of difference
952
 * \param[out]   ppixdiff   [optional] pix of difference
953
 * \return  0 if OK; 1 on error
954
 *
955
 * <pre>
956
 * Notes:
957
 *      (1) See pixCompareGrayOrRGB() for details.
958
 *      (2) Use pixCompareGrayOrRGB() if the input pix are colormapped.
959
 *      (3) Note: setting %plottype > 0 can result in writing named
960
 *                output files.
961
 * </pre>
962
 */
963
l_ok
964
pixCompareGray(PIX        *pix1,
965
               PIX        *pix2,
966
               l_int32     comptype,
967
               l_int32     plottype,
968
               l_int32    *psame,
969
               l_float32  *pdiff,
970
               l_float32  *prmsdiff,
971
               PIX       **ppixdiff)
972
0
{
973
0
char            buf[64];
974
0
static l_atomic index = 0;
975
0
l_int32         d1, d2, same, first, last;
976
0
GPLOT          *gplot;
977
0
NUMA           *na, *nac;
978
0
PIX            *pixt;
979
980
0
    if (psame) *psame = 0;
981
0
    if (pdiff) *pdiff = 255.0;
982
0
    if (prmsdiff) *prmsdiff = 255.0;
983
0
    if (ppixdiff) *ppixdiff = NULL;
984
0
    if (!pix1)
985
0
        return ERROR_INT("pix1 not defined", __func__, 1);
986
0
    if (!pix2)
987
0
        return ERROR_INT("pix2 not defined", __func__, 1);
988
0
    d1 = pixGetDepth(pix1);
989
0
    d2 = pixGetDepth(pix2);
990
0
    if ((d1 != d2) || (d1 != 8 && d1 != 16))
991
0
        return ERROR_INT("depths unequal or not 8 or 16 bpp", __func__, 1);
992
0
    if (pixGetColormap(pix1) || pixGetColormap(pix2))
993
0
        return ERROR_INT("pix1 and/or pix2 are colormapped", __func__, 1);
994
0
    if (comptype != L_COMPARE_SUBTRACT && comptype != L_COMPARE_ABS_DIFF)
995
0
        return ERROR_INT("invalid comptype", __func__, 1);
996
0
    if (plottype < 0 || plottype >= NUM_GPLOT_OUTPUTS)
997
0
        return ERROR_INT("invalid plottype", __func__, 1);
998
999
0
    lept_mkdir("lept/comp");
1000
1001
0
    if (comptype == L_COMPARE_SUBTRACT)
1002
0
        pixt = pixSubtractGray(NULL, pix1, pix2);
1003
0
    else  /* comptype == L_COMPARE_ABS_DIFF) */
1004
0
        pixt = pixAbsDifference(pix1, pix2);
1005
1006
0
    pixZero(pixt, &same);
1007
0
    if (same)
1008
0
        L_INFO("Images are pixel-wise identical\n", __func__);
1009
0
    if (psame) *psame = same;
1010
1011
0
    if (pdiff)
1012
0
        pixGetAverageMasked(pixt, NULL, 0, 0, 1, L_MEAN_ABSVAL, pdiff);
1013
1014
        /* Don't bother to plot if the images are the same */
1015
0
    if (plottype && !same) {
1016
0
        L_INFO("Images differ: output plots will be generated\n", __func__);
1017
0
        na = pixGetGrayHistogram(pixt, 1);
1018
0
        numaGetNonzeroRange(na, TINY, &first, &last);
1019
0
        nac = numaClipToInterval(na, 0, last);
1020
0
        snprintf(buf, sizeof(buf), "/tmp/lept/comp/compare_gray%d", index);
1021
0
        gplot = gplotCreate(buf, plottype,
1022
0
                            "Pixel Difference Histogram", "diff val",
1023
0
                            "number of pixels");
1024
0
        gplotAddPlot(gplot, NULL, nac, GPLOT_LINES, "gray");
1025
0
        gplotMakeOutput(gplot);
1026
0
        gplotDestroy(&gplot);
1027
0
        snprintf(buf, sizeof(buf), "/tmp/lept/comp/compare_gray%d.png",
1028
0
                 index++);
1029
0
        l_fileDisplay(buf, 100, 100, 1.0);
1030
0
        numaDestroy(&na);
1031
0
        numaDestroy(&nac);
1032
0
    }
1033
1034
0
    if (ppixdiff)
1035
0
        *ppixdiff = pixCopy(NULL, pixt);
1036
1037
0
    if (prmsdiff) {
1038
0
        if (comptype == L_COMPARE_SUBTRACT) {  /* wrong type for rms diff */
1039
0
            pixDestroy(&pixt);
1040
0
            pixt = pixAbsDifference(pix1, pix2);
1041
0
        }
1042
0
        pixGetAverageMasked(pixt, NULL, 0, 0, 1, L_ROOT_MEAN_SQUARE, prmsdiff);
1043
0
    }
1044
1045
0
    pixDestroy(&pixt);
1046
0
    return 0;
1047
0
}
1048
1049
1050
/*!
1051
 * \brief   pixCompareRGB()
1052
 *
1053
 * \param[in]    pix1       32 bpp rgb
1054
 * \param[in]    pix2       32 bpp rgb
1055
 * \param[in]    comptype   L_COMPARE_SUBTRACT, L_COMPARE_ABS_DIFF
1056
 * \param[in]    plottype   gplot plot output type, or 0 for no plot
1057
 * \param[out]   psame      [optional] 1 if pixel values are identical
1058
 * \param[out]   pdiff      [optional] average difference
1059
 * \param[out]   prmsdiff   [optional] rms of difference
1060
 * \param[out]   ppixdiff   [optional] pix of difference
1061
 * \return  0 if OK; 1 on error
1062
 *
1063
 * <pre>
1064
 * Notes:
1065
 *      (1) See pixCompareGrayOrRGB() for details.
1066
 *      (2) Note: setting %plottype > 0 can result in writing named
1067
 *                output files.
1068
 * </pre>
1069
 */
1070
l_ok
1071
pixCompareRGB(PIX        *pix1,
1072
              PIX        *pix2,
1073
              l_int32     comptype,
1074
              l_int32     plottype,
1075
              l_int32    *psame,
1076
              l_float32  *pdiff,
1077
              l_float32  *prmsdiff,
1078
              PIX       **ppixdiff)
1079
0
{
1080
0
char            buf[64];
1081
0
static l_atomic index = 0;
1082
0
l_int32         rsame, gsame, bsame, same, first, rlast, glast, blast, last;
1083
0
l_float32       rdiff, gdiff, bdiff;
1084
0
GPLOT          *gplot;
1085
0
NUMA           *nar, *nag, *nab, *narc, *nagc, *nabc;
1086
0
PIX            *pixr1, *pixr2, *pixg1, *pixg2, *pixb1, *pixb2;
1087
0
PIX            *pixr, *pixg, *pixb;
1088
1089
0
    if (psame) *psame = 0;
1090
0
    if (pdiff) *pdiff = 0.0;
1091
0
    if (prmsdiff) *prmsdiff = 0.0;
1092
0
    if (ppixdiff) *ppixdiff = NULL;
1093
0
    if (!pix1 || pixGetDepth(pix1) != 32)
1094
0
        return ERROR_INT("pix1 not defined or not 32 bpp", __func__, 1);
1095
0
    if (!pix2 || pixGetDepth(pix2) != 32)
1096
0
        return ERROR_INT("pix2 not defined or not ew bpp", __func__, 1);
1097
0
    if (comptype != L_COMPARE_SUBTRACT && comptype != L_COMPARE_ABS_DIFF)
1098
0
        return ERROR_INT("invalid comptype", __func__, 1);
1099
0
    if (plottype < 0 || plottype >= NUM_GPLOT_OUTPUTS)
1100
0
        return ERROR_INT("invalid plottype", __func__, 1);
1101
1102
0
    lept_mkdir("lept/comp");
1103
1104
0
    pixr1 = pixGetRGBComponent(pix1, COLOR_RED);
1105
0
    pixr2 = pixGetRGBComponent(pix2, COLOR_RED);
1106
0
    pixg1 = pixGetRGBComponent(pix1, COLOR_GREEN);
1107
0
    pixg2 = pixGetRGBComponent(pix2, COLOR_GREEN);
1108
0
    pixb1 = pixGetRGBComponent(pix1, COLOR_BLUE);
1109
0
    pixb2 = pixGetRGBComponent(pix2, COLOR_BLUE);
1110
0
    if (comptype == L_COMPARE_SUBTRACT) {
1111
0
        pixr = pixSubtractGray(NULL, pixr1, pixr2);
1112
0
        pixg = pixSubtractGray(NULL, pixg1, pixg2);
1113
0
        pixb = pixSubtractGray(NULL, pixb1, pixb2);
1114
0
    } else { /* comptype == L_COMPARE_ABS_DIFF) */
1115
0
        pixr = pixAbsDifference(pixr1, pixr2);
1116
0
        pixg = pixAbsDifference(pixg1, pixg2);
1117
0
        pixb = pixAbsDifference(pixb1, pixb2);
1118
0
    }
1119
1120
0
    pixZero(pixr, &rsame);
1121
0
    pixZero(pixg, &gsame);
1122
0
    pixZero(pixb, &bsame);
1123
0
    same = rsame && gsame && bsame;
1124
0
    if (same)
1125
0
        L_INFO("Images are pixel-wise identical\n", __func__);
1126
0
    if (psame) *psame = same;
1127
1128
0
    if (pdiff) {
1129
0
        pixGetAverageMasked(pixr, NULL, 0, 0, 1, L_MEAN_ABSVAL, &rdiff);
1130
0
        pixGetAverageMasked(pixg, NULL, 0, 0, 1, L_MEAN_ABSVAL, &gdiff);
1131
0
        pixGetAverageMasked(pixb, NULL, 0, 0, 1, L_MEAN_ABSVAL, &bdiff);
1132
0
        *pdiff = (rdiff + gdiff + bdiff) / 3.0;
1133
0
    }
1134
1135
        /* Don't bother to plot if the images are the same */
1136
0
    if (plottype && !same) {
1137
0
        L_INFO("Images differ: output plots will be generated\n", __func__);
1138
0
        nar = pixGetGrayHistogram(pixr, 1);
1139
0
        nag = pixGetGrayHistogram(pixg, 1);
1140
0
        nab = pixGetGrayHistogram(pixb, 1);
1141
0
        numaGetNonzeroRange(nar, TINY, &first, &rlast);
1142
0
        numaGetNonzeroRange(nag, TINY, &first, &glast);
1143
0
        numaGetNonzeroRange(nab, TINY, &first, &blast);
1144
0
        last = L_MAX(rlast, glast);
1145
0
        last = L_MAX(last, blast);
1146
0
        narc = numaClipToInterval(nar, 0, last);
1147
0
        nagc = numaClipToInterval(nag, 0, last);
1148
0
        nabc = numaClipToInterval(nab, 0, last);
1149
0
        snprintf(buf, sizeof(buf), "/tmp/lept/comp/compare_rgb%d", index);
1150
0
        gplot = gplotCreate(buf, plottype,
1151
0
                            "Pixel Difference Histogram", "diff val",
1152
0
                            "number of pixels");
1153
0
        gplotAddPlot(gplot, NULL, narc, GPLOT_LINES, "red");
1154
0
        gplotAddPlot(gplot, NULL, nagc, GPLOT_LINES, "green");
1155
0
        gplotAddPlot(gplot, NULL, nabc, GPLOT_LINES, "blue");
1156
0
        gplotMakeOutput(gplot);
1157
0
        gplotDestroy(&gplot);
1158
0
        snprintf(buf, sizeof(buf), "/tmp/lept/comp/compare_rgb%d.png",
1159
0
                 index++);
1160
0
        l_fileDisplay(buf, 100, 100, 1.0);
1161
0
        numaDestroy(&nar);
1162
0
        numaDestroy(&nag);
1163
0
        numaDestroy(&nab);
1164
0
        numaDestroy(&narc);
1165
0
        numaDestroy(&nagc);
1166
0
        numaDestroy(&nabc);
1167
0
    }
1168
1169
0
    if (ppixdiff)
1170
0
        *ppixdiff = pixCreateRGBImage(pixr, pixg, pixb);
1171
1172
0
    if (prmsdiff) {
1173
0
        if (comptype == L_COMPARE_SUBTRACT) {
1174
0
            pixDestroy(&pixr);
1175
0
            pixDestroy(&pixg);
1176
0
            pixDestroy(&pixb);
1177
0
            pixr = pixAbsDifference(pixr1, pixr2);
1178
0
            pixg = pixAbsDifference(pixg1, pixg2);
1179
0
            pixb = pixAbsDifference(pixb1, pixb2);
1180
0
        }
1181
0
        pixGetAverageMasked(pixr, NULL, 0, 0, 1, L_ROOT_MEAN_SQUARE, &rdiff);
1182
0
        pixGetAverageMasked(pixg, NULL, 0, 0, 1, L_ROOT_MEAN_SQUARE, &gdiff);
1183
0
        pixGetAverageMasked(pixb, NULL, 0, 0, 1, L_ROOT_MEAN_SQUARE, &bdiff);
1184
0
        *prmsdiff = (rdiff + gdiff + bdiff) / 3.0;
1185
0
    }
1186
1187
0
    pixDestroy(&pixr1);
1188
0
    pixDestroy(&pixr2);
1189
0
    pixDestroy(&pixg1);
1190
0
    pixDestroy(&pixg2);
1191
0
    pixDestroy(&pixb1);
1192
0
    pixDestroy(&pixb2);
1193
0
    pixDestroy(&pixr);
1194
0
    pixDestroy(&pixg);
1195
0
    pixDestroy(&pixb);
1196
0
    return 0;
1197
0
}
1198
1199
1200
/*!
1201
 * \brief   pixCompareTiled()
1202
 *
1203
 * \param[in]    pix1       8 bpp or 32 bpp rgb
1204
 * \param[in]    pix2       8 bpp 32 bpp rgb
1205
 * \param[in]    sx, sy     tile size; must be > 1 in each dimension
1206
 * \param[in]    type       L_MEAN_ABSVAL or L_ROOT_MEAN_SQUARE
1207
 * \param[out]   ppixdiff   pix of difference
1208
 * \return  0 if OK; 1 on error
1209
 *
1210
 * <pre>
1211
 * Notes:
1212
 *      (1) With L_MEAN_ABSVAL, we compute for each tile the
1213
 *          average abs value of the pixel component difference between
1214
 *          the two (aligned) images.  With L_ROOT_MEAN_SQUARE, we
1215
 *          compute instead the rms difference over all components.
1216
 *      (2) The two input pix must be the same depth.  Comparison is made
1217
 *          using UL corner alignment.
1218
 *      (3) For 32 bpp, the distance between corresponding tiles
1219
 *          is found by averaging the measured difference over all three
1220
 *          components of each pixel in the tile.
1221
 *      (4) The result, pixdiff, contains one pixel for each source tile.
1222
 * </pre>
1223
 */
1224
l_ok
1225
pixCompareTiled(PIX     *pix1,
1226
                PIX     *pix2,
1227
                l_int32  sx,
1228
                l_int32  sy,
1229
                l_int32  type,
1230
                PIX    **ppixdiff)
1231
0
{
1232
0
l_int32    d1, d2, w, h;
1233
0
PIX       *pixt, *pixr, *pixg, *pixb;
1234
0
PIX       *pixrdiff, *pixgdiff, *pixbdiff;
1235
0
PIXACC    *pixacc;
1236
1237
0
    if (!ppixdiff)
1238
0
        return ERROR_INT("&pixdiff not defined", __func__, 1);
1239
0
    *ppixdiff = NULL;
1240
0
    if (!pix1)
1241
0
        return ERROR_INT("pix1 not defined", __func__, 1);
1242
0
    if (!pix2)
1243
0
        return ERROR_INT("pix2 not defined", __func__, 1);
1244
0
    d1 = pixGetDepth(pix1);
1245
0
    d2 = pixGetDepth(pix2);
1246
0
    if (d1 != d2)
1247
0
        return ERROR_INT("depths not equal", __func__, 1);
1248
0
    if (d1 != 8 && d1 != 32)
1249
0
        return ERROR_INT("pix1 not 8 or 32 bpp", __func__, 1);
1250
0
    if (d2 != 8 && d2 != 32)
1251
0
        return ERROR_INT("pix2 not 8 or 32 bpp", __func__, 1);
1252
0
    if (sx < 2 || sy < 2)
1253
0
        return ERROR_INT("sx and sy not both > 1", __func__, 1);
1254
0
    if (type != L_MEAN_ABSVAL && type != L_ROOT_MEAN_SQUARE)
1255
0
        return ERROR_INT("invalid type", __func__, 1);
1256
1257
0
    pixt = pixAbsDifference(pix1, pix2);
1258
0
    if (d1 == 8) {
1259
0
        *ppixdiff = pixGetAverageTiled(pixt, sx, sy, type);
1260
0
    } else {  /* d1 == 32 */
1261
0
        pixr = pixGetRGBComponent(pixt, COLOR_RED);
1262
0
        pixg = pixGetRGBComponent(pixt, COLOR_GREEN);
1263
0
        pixb = pixGetRGBComponent(pixt, COLOR_BLUE);
1264
0
        pixrdiff = pixGetAverageTiled(pixr, sx, sy, type);
1265
0
        pixgdiff = pixGetAverageTiled(pixg, sx, sy, type);
1266
0
        pixbdiff = pixGetAverageTiled(pixb, sx, sy, type);
1267
0
        pixGetDimensions(pixrdiff, &w, &h, NULL);
1268
0
        pixacc = pixaccCreate(w, h, 0);
1269
0
        pixaccAdd(pixacc, pixrdiff);
1270
0
        pixaccAdd(pixacc, pixgdiff);
1271
0
        pixaccAdd(pixacc, pixbdiff);
1272
0
        pixaccMultConst(pixacc, 1.f / 3.f);
1273
0
        *ppixdiff = pixaccFinal(pixacc, 8);
1274
0
        pixDestroy(&pixr);
1275
0
        pixDestroy(&pixg);
1276
0
        pixDestroy(&pixb);
1277
0
        pixDestroy(&pixrdiff);
1278
0
        pixDestroy(&pixgdiff);
1279
0
        pixDestroy(&pixbdiff);
1280
0
        pixaccDestroy(&pixacc);
1281
0
    }
1282
0
    pixDestroy(&pixt);
1283
0
    return 0;
1284
0
}
1285
1286
1287
/*------------------------------------------------------------------*
1288
 *            Other measures of the difference of two images        *
1289
 *------------------------------------------------------------------*/
1290
/*!
1291
 * \brief   pixCompareRankDifference()
1292
 *
1293
 * \param[in]    pix1      8 bpp gray or 32 bpp rgb, or colormapped
1294
 * \param[in]    pix2      8 bpp gray or 32 bpp rgb, or colormapped
1295
 * \param[in]    factor    subsampling factor; use 0 or 1 for no subsampling
1296
 * \return  narank      numa of rank difference, or NULL on error
1297
 *
1298
 * <pre>
1299
 * Notes:
1300
 *      (1) This answers the question: if the pixel values in each
1301
 *          component are compared by absolute difference, for
1302
 *          any value of difference, what is the fraction of
1303
 *          pixel pairs that have a difference of this magnitude
1304
 *          or greater.  For a difference of 0, the fraction is 1.0.
1305
 *          In this sense, it is a mapping from pixel difference to
1306
 *          rank order of difference.
1307
 *      (2) The two images are aligned at the UL corner, and do not
1308
 *          need to be the same size.  If they are not the same size,
1309
 *          the comparison will be made over overlapping pixels.
1310
 *      (3) If there is a colormap, it is removed and the result
1311
 *          is either gray or RGB depending on the colormap.
1312
 *      (4) If RGB, pixel differences for each component are aggregated
1313
 *          into a single histogram.
1314
 * </pre>
1315
 */
1316
NUMA *
1317
pixCompareRankDifference(PIX     *pix1,
1318
                         PIX     *pix2,
1319
                         l_int32  factor)
1320
0
{
1321
0
l_int32     i;
1322
0
l_float32  *array1, *array2;
1323
0
NUMA       *nah, *nan, *nad;
1324
1325
0
    if (!pix1)
1326
0
        return (NUMA *)ERROR_PTR("pix1 not defined", __func__, NULL);
1327
0
    if (!pix2)
1328
0
        return (NUMA *)ERROR_PTR("pix2 not defined", __func__, NULL);
1329
1330
0
    if ((nah = pixGetDifferenceHistogram(pix1, pix2, factor)) == NULL)
1331
0
        return (NUMA *)ERROR_PTR("na not made", __func__, NULL);
1332
1333
0
    nan = numaNormalizeHistogram(nah, 1.0);
1334
0
    array1 = numaGetFArray(nan, L_NOCOPY);
1335
1336
0
    nad = numaCreate(256);
1337
0
    numaSetCount(nad, 256);  /* all initialized to 0.0 */
1338
0
    array2 = numaGetFArray(nad, L_NOCOPY);
1339
1340
        /* Do rank accumulation on normalized histo of diffs */
1341
0
    array2[0] = 1.0;
1342
0
    for (i = 1; i < 256; i++)
1343
0
        array2[i] = array2[i - 1] - array1[i - 1];
1344
1345
0
    numaDestroy(&nah);
1346
0
    numaDestroy(&nan);
1347
0
    return nad;
1348
0
}
1349
1350
1351
/*!
1352
 * \brief   pixTestForSimilarity()
1353
 *
1354
 * \param[in]    pix1         8 bpp gray or 32 bpp rgb, or colormapped
1355
 * \param[in]    pix2         8 bpp gray or 32 bpp rgb, or colormapped
1356
 * \param[in]    factor       subsampling factor; use 0 or 1 for no subsampling
1357
 * \param[in]    mindiff      minimum pixel difference to be counted; > 0
1358
 * \param[in]    maxfract     maximum fraction of pixels allowed to have
1359
 *                            diff greater than or equal to mindiff
1360
 * \param[in]    maxave       maximum average difference of pixels allowed for
1361
 *                            pixels with diff greater than or equal to
1362
 *                            mindiff, after subtracting mindiff
1363
 * \param[out]   psimilar     1 if similar, 0 otherwise
1364
 * \param[in]    details      use 1 to give normalized histogram and other data
1365
 * \return  0 if OK, 1 on error
1366
 *
1367
 * <pre>
1368
 * Notes:
1369
 *      (1) This takes 2 pix that are the same size and determines using
1370
 *          3 input parameters if they are "similar".  The first parameter
1371
 *          %mindiff establishes a criterion of pixel-to-pixel similarity:
1372
 *          two pixels are not similar if their difference in value is
1373
 *          at least mindiff.  Then %maxfract and %maxave are thresholds
1374
 *          on the number and distribution of dissimilar pixels
1375
 *          allowed for the two pix to be similar.   If the pix are
1376
 *          to be similar, neither threshold can be exceeded.
1377
 *      (2) In setting the %maxfract and %maxave thresholds, you have
1378
 *          these options:
1379
 *            (a) Base the comparison only on %maxfract.  Then set
1380
 *                %maxave = 0.0 or 256.0.  (If 0, we always ignore it.)
1381
 *            (b) Base the comparison only on %maxave.  Then set
1382
 *                %maxfract = 1.0.
1383
 *            (c) Base the comparison on both thresholds.
1384
 *      (3) Example of values that can be expected at mindiff = 15 when
1385
 *          comparing lossless png encoding with jpeg encoding, q=75:
1386
 *             (smoothish bg)       fractdiff = 0.01, avediff = 2.5
1387
 *             (natural scene)      fractdiff = 0.13, avediff = 3.5
1388
 *          To identify these images as 'similar', select maxfract
1389
 *          and maxave to be upper bounds of what you expect.
1390
 *      (4) See pixGetDifferenceStats() for a discussion of why we subtract
1391
 *          mindiff from the computed average diff of the nonsimilar pixels
1392
 *          to get the 'avediff' returned by that function.
1393
 *      (5) If there is a colormap, it is removed and the result
1394
 *          is either gray or RGB depending on the colormap.
1395
 *      (6) If RGB, the maximum difference between pixel components is
1396
 *          saved in the histogram.
1397
 * </pre>
1398
 */
1399
l_ok
1400
pixTestForSimilarity(PIX       *pix1,
1401
                     PIX       *pix2,
1402
                     l_int32    factor,
1403
                     l_int32    mindiff,
1404
                     l_float32  maxfract,
1405
                     l_float32  maxave,
1406
                     l_int32   *psimilar,
1407
                     l_int32    details)
1408
0
{
1409
0
l_float32   fractdiff, avediff;
1410
1411
0
    if (!psimilar)
1412
0
        return ERROR_INT("&similar not defined", __func__, 1);
1413
0
    *psimilar = 0;
1414
0
    if (!pix1)
1415
0
        return ERROR_INT("pix1 not defined", __func__, 1);
1416
0
    if (!pix2)
1417
0
        return ERROR_INT("pix2 not defined", __func__, 1);
1418
0
    if (pixSizesEqual(pix1, pix2) == 0)
1419
0
        return ERROR_INT("pix sizes not equal", __func__, 1);
1420
0
    if (mindiff <= 0)
1421
0
        return ERROR_INT("mindiff must be > 0", __func__, 1);
1422
1423
0
    if (pixGetDifferenceStats(pix1, pix2, factor, mindiff,
1424
0
                              &fractdiff, &avediff, details))
1425
0
        return ERROR_INT("diff stats not found", __func__, 1);
1426
1427
0
    if (maxave <= 0.0) maxave = 256.0;
1428
0
    if (fractdiff <= maxfract && avediff <= maxave)
1429
0
        *psimilar = 1;
1430
0
    return 0;
1431
0
}
1432
1433
1434
/*!
1435
 * \brief   pixGetDifferenceStats()
1436
 *
1437
 * \param[in]    pix1        8 bpp gray or 32 bpp rgb, or colormapped
1438
 * \param[in]    pix2        8 bpp gray or 32 bpp rgb, or colormapped
1439
 * \param[in]    factor      subsampling factor; use 0 or 1 for no subsampling
1440
 * \param[in]    mindiff     minimum pixel difference to be counted; > 0
1441
 * \param[out]   pfractdiff  fraction of pixels with diff greater than or
1442
 *                           equal to mindiff
1443
 * \param[out]   pavediff    average difference of pixels with diff greater
1444
 *                           than or equal to mindiff, less mindiff
1445
 * \param[in]    details     use 1 to give normalized histogram and other data
1446
 * \return  0 if OK, 1 on error
1447
 *
1448
 * <pre>
1449
 * Notes:
1450
 *      (1) This takes a threshold %mindiff and describes the difference
1451
 *          between two images in terms of two numbers:
1452
 *            (a) the fraction of pixels, %fractdiff, whose difference
1453
 *                equals or exceeds the threshold %mindiff, and
1454
 *            (b) the average value %avediff of the difference in pixel value
1455
 *                for the pixels in the set given by (a), after you subtract
1456
 *                %mindiff.  The reason for subtracting %mindiff is that
1457
 *                you then get a useful measure for the rate of falloff
1458
 *                of the distribution for larger differences.  For example,
1459
 *                if %mindiff = 10 and you find that %avediff = 2.5, it
1460
 *                says that of the pixels with diff > 10, the average of
1461
 *                their diffs is just mindiff + 2.5 = 12.5.  This is a
1462
 *                fast falloff in the histogram with increasing difference.
1463
 *      (2) The two images are aligned at the UL corner, and do not
1464
 *          need to be the same size.  If they are not the same size,
1465
 *          the comparison will be made over overlapping pixels.
1466
 *      (3) If there is a colormap, it is removed and the result
1467
 *          is either gray or RGB depending on the colormap.
1468
 *      (4) If RGB, the maximum difference between pixel components is
1469
 *          saved in the histogram.
1470
 *      (5) Set %details == 1 to see the difference histogram and get
1471
 *          an output that shows for each value of %mindiff, what are the
1472
 *          minimum values required for fractdiff and avediff in order
1473
 *          that the two pix will be considered similar.
1474
 * </pre>
1475
 */
1476
l_ok
1477
pixGetDifferenceStats(PIX        *pix1,
1478
                      PIX        *pix2,
1479
                      l_int32     factor,
1480
                      l_int32     mindiff,
1481
                      l_float32  *pfractdiff,
1482
                      l_float32  *pavediff,
1483
                      l_int32     details)
1484
0
{
1485
0
l_int32     i, first, last, diff;
1486
0
l_float32   fract, ave;
1487
0
l_float32  *array;
1488
0
NUMA       *nah, *nan, *nac;
1489
1490
0
    if (pfractdiff) *pfractdiff = 0.0;
1491
0
    if (pavediff) *pavediff = 0.0;
1492
0
    if (!pfractdiff)
1493
0
        return ERROR_INT("&fractdiff not defined", __func__, 1);
1494
0
    if (!pavediff)
1495
0
        return ERROR_INT("&avediff not defined", __func__, 1);
1496
0
    if (!pix1)
1497
0
        return ERROR_INT("pix1 not defined", __func__, 1);
1498
0
    if (!pix2)
1499
0
        return ERROR_INT("pix2 not defined", __func__, 1);
1500
0
    if (mindiff <= 0)
1501
0
        return ERROR_INT("mindiff must be > 0", __func__, 1);
1502
1503
0
    if ((nah = pixGetDifferenceHistogram(pix1, pix2, factor)) == NULL)
1504
0
        return ERROR_INT("na not made", __func__, 1);
1505
1506
0
    if ((nan = numaNormalizeHistogram(nah, 1.0)) == NULL) {
1507
0
        numaDestroy(&nah);
1508
0
        return ERROR_INT("nan not made", __func__, 1);
1509
0
    }
1510
0
    array = numaGetFArray(nan, L_NOCOPY);
1511
1512
0
    if (details) {
1513
0
        lept_mkdir("lept/comp");
1514
0
        numaGetNonzeroRange(nan, 0.0, &first, &last);
1515
0
        nac = numaClipToInterval(nan, first, last);
1516
0
        gplotSimple1(nac, GPLOT_PNG, "/tmp/lept/comp/histo",
1517
0
                     "Difference histogram");
1518
0
        l_fileDisplay("/tmp/lept/comp/histo.png", 500, 0, 1.0);
1519
0
        lept_stderr("\nNonzero values in normalized histogram:");
1520
0
        numaWriteStderr(nac);
1521
0
        numaDestroy(&nac);
1522
0
        lept_stderr(" Mindiff      fractdiff      avediff\n");
1523
0
        lept_stderr(" -----------------------------------\n");
1524
0
        for (diff = 1; diff < L_MIN(2 * mindiff, last); diff++) {
1525
0
            fract = 0.0;
1526
0
            ave = 0.0;
1527
0
            for (i = diff; i <= last; i++) {
1528
0
                fract += array[i];
1529
0
                ave += (l_float32)i * array[i];
1530
0
            }
1531
0
            ave = (fract == 0.0) ? 0.0 : ave / fract;
1532
0
            ave -= diff;
1533
0
            lept_stderr("%5d         %7.4f        %7.4f\n",
1534
0
                        diff, fract, ave);
1535
0
        }
1536
0
        lept_stderr(" -----------------------------------\n");
1537
0
    }
1538
1539
0
    fract = 0.0;
1540
0
    ave = 0.0;
1541
0
    for (i = mindiff; i < 256; i++) {
1542
0
      fract += array[i];
1543
0
      ave += (l_float32)i * array[i];
1544
0
    }
1545
0
    ave = (fract == 0.0) ? 0.0 : ave / fract;
1546
0
    ave -= mindiff;
1547
1548
0
    *pfractdiff = fract;
1549
0
    *pavediff = ave;
1550
1551
0
    numaDestroy(&nah);
1552
0
    numaDestroy(&nan);
1553
0
    return 0;
1554
0
}
1555
1556
1557
/*!
1558
 * \brief   pixGetDifferenceHistogram()
1559
 *
1560
 * \param[in]    pix1      8 bpp gray or 32 bpp rgb, or colormapped
1561
 * \param[in]    pix2      8 bpp gray or 32 bpp rgb, or colormapped
1562
 * \param[in]    factor    subsampling factor; use 0 or 1 for no subsampling
1563
 * \return  na     Numa of histogram of differences, or NULL on error
1564
 *
1565
 * <pre>
1566
 * Notes:
1567
 *      (1) The two images are aligned at the UL corner, and do not
1568
 *          need to be the same size.  If they are not the same size,
1569
 *          the comparison will be made over overlapping pixels.
1570
 *      (2) If there is a colormap, it is removed and the result
1571
 *          is either gray or RGB depending on the colormap.
1572
 *      (3) If RGB, the maximum difference between pixel components is
1573
 *          saved in the histogram.
1574
 * </pre>
1575
 */
1576
NUMA *
1577
pixGetDifferenceHistogram(PIX     *pix1,
1578
                          PIX     *pix2,
1579
                          l_int32  factor)
1580
0
{
1581
0
l_int32     w1, h1, d1, w2, h2, d2, w, h, wpl1, wpl2;
1582
0
l_int32     i, j, val, val1, val2;
1583
0
l_int32     rval1, rval2, gval1, gval2, bval1, bval2;
1584
0
l_int32     rdiff, gdiff, bdiff, maxdiff;
1585
0
l_uint32   *data1, *data2, *line1, *line2;
1586
0
l_float32  *array;
1587
0
NUMA       *na;
1588
0
PIX        *pixt1, *pixt2;
1589
1590
0
    if (!pix1)
1591
0
        return (NUMA *)ERROR_PTR("pix1 not defined", __func__, NULL);
1592
0
    if (!pix2)
1593
0
        return (NUMA *)ERROR_PTR("pix2 not defined", __func__, NULL);
1594
0
    d1 = pixGetDepth(pix1);
1595
0
    d2 = pixGetDepth(pix2);
1596
0
    if (d1 == 16 || d2 == 16)
1597
0
        return (NUMA *)ERROR_PTR("d == 16 not supported", __func__, NULL);
1598
0
    if (d1 < 8 && !pixGetColormap(pix1))
1599
0
        return (NUMA *)ERROR_PTR("pix1 depth < 8 bpp and not cmapped",
1600
0
                                 __func__, NULL);
1601
0
    if (d2 < 8 && !pixGetColormap(pix2))
1602
0
        return (NUMA *)ERROR_PTR("pix2 depth < 8 bpp and not cmapped",
1603
0
                                 __func__, NULL);
1604
0
    pixt1 = pixRemoveColormap(pix1, REMOVE_CMAP_BASED_ON_SRC);
1605
0
    pixt2 = pixRemoveColormap(pix2, REMOVE_CMAP_BASED_ON_SRC);
1606
0
    pixGetDimensions(pixt1, &w1, &h1, &d1);
1607
0
    pixGetDimensions(pixt2, &w2, &h2, &d2);
1608
0
    if (d1 != d2) {
1609
0
        pixDestroy(&pixt1);
1610
0
        pixDestroy(&pixt2);
1611
0
        return (NUMA *)ERROR_PTR("pix depths not equal", __func__, NULL);
1612
0
    }
1613
0
    if (factor < 1) factor = 1;
1614
1615
0
    na = numaCreate(256);
1616
0
    numaSetCount(na, 256);  /* all initialized to 0.0 */
1617
0
    array = numaGetFArray(na, L_NOCOPY);
1618
0
    w = L_MIN(w1, w2);
1619
0
    h = L_MIN(h1, h2);
1620
0
    data1 = pixGetData(pixt1);
1621
0
    data2 = pixGetData(pixt2);
1622
0
    wpl1 = pixGetWpl(pixt1);
1623
0
    wpl2 = pixGetWpl(pixt2);
1624
0
    if (d1 == 8) {
1625
0
        for (i = 0; i < h; i += factor) {
1626
0
            line1 = data1 + i * wpl1;
1627
0
            line2 = data2 + i * wpl2;
1628
0
            for (j = 0; j < w; j += factor) {
1629
0
                val1 = GET_DATA_BYTE(line1, j);
1630
0
                val2 = GET_DATA_BYTE(line2, j);
1631
0
                val = L_ABS(val1 - val2);
1632
0
                array[val]++;
1633
0
            }
1634
0
        }
1635
0
    } else {  /* d1 == 32 */
1636
0
        for (i = 0; i < h; i += factor) {
1637
0
            line1 = data1 + i * wpl1;
1638
0
            line2 = data2 + i * wpl2;
1639
0
            for (j = 0; j < w; j += factor) {
1640
0
                extractRGBValues(line1[j], &rval1, &gval1, &bval1);
1641
0
                extractRGBValues(line2[j], &rval2, &gval2, &bval2);
1642
0
                rdiff = L_ABS(rval1 - rval2);
1643
0
                gdiff = L_ABS(gval1 - gval2);
1644
0
                bdiff = L_ABS(bval1 - bval2);
1645
0
                maxdiff = L_MAX(rdiff, gdiff);
1646
0
                maxdiff = L_MAX(maxdiff, bdiff);
1647
0
                array[maxdiff]++;
1648
0
            }
1649
0
        }
1650
0
    }
1651
1652
0
    pixDestroy(&pixt1);
1653
0
    pixDestroy(&pixt2);
1654
0
    return na;
1655
0
}
1656
1657
1658
/*!
1659
 * \brief   pixGetPerceptualDiff()
1660
 *
1661
 * \param[in]    pixs1       8 bpp gray or 32 bpp rgb, or colormapped
1662
 * \param[in]    pixs2       8 bpp gray or 32 bpp rgb, or colormapped
1663
 * \param[in]    sampling    subsampling factor; use 0 or 1 for no subsampling
1664
 * \param[in]    dilation    size of grayscale or color Sel; odd
1665
 * \param[in]    mindiff     minimum pixel difference to be counted; > 0
1666
 * \param[out]   pfract      fraction of pixels with diff greater than mindiff
1667
 * \param[out]   ppixdiff1   [optional] showing difference (gray or color)
1668
 * \param[out]   ppixdiff2   [optional] showing pixels of sufficient diff
1669
 * \return  0 if OK, 1 on error
1670
 *
1671
 * <pre>
1672
 * Notes:
1673
 *      (1) This takes 2 pix and determines, using 2 input parameters:
1674
 *           * %dilation specifies the amount of grayscale or color
1675
 *             dilation to apply to the images, to compensate for
1676
 *             a small amount of misregistration.  A typical number might
1677
 *             be 5, which uses a 5x5 Sel.  Grayscale dilation expands
1678
 *             lighter pixels into darker pixel regions.
1679
 *           * %mindiff determines the threshold on the difference in
1680
 *             pixel values to be counted -- two pixels are not similar
1681
 *             if their difference in value is at least %mindiff.  For
1682
 *             color pixels, we use the maximum component difference.
1683
 *      (2) The pixelwise comparison is always done with the UL corners
1684
 *          aligned.  The sizes of pix1 and pix2 need not be the same,
1685
 *          although in practice it can be useful to scale to the same size.
1686
 *      (3) If there is a colormap, it is removed and the result
1687
 *          is either gray or RGB depending on the colormap.
1688
 *      (4) Two optional diff images can be retrieved (typ. for debugging):
1689
 *           pixdiff1: the gray or color difference
1690
 *           pixdiff2: thresholded to 1 bpp for pixels exceeding %mindiff
1691
 *      (5) The returned value of fract can be compared to some threshold,
1692
 *          which is application dependent.
1693
 *      (6) This method is in analogy to the two-sided hausdorff transform,
1694
 *          except here it is for d > 1.  For d == 1 (see pixRankHaustest()),
1695
 *          we verify that when one pix1 is dilated, it covers at least a
1696
 *          given fraction of the pixels in pix2, and v.v.; in that
1697
 *          case, the two pix are sufficiently similar.  Here, we
1698
 *          do an analogous thing: subtract the dilated pix1 from pix2 to
1699
 *          get a 1-sided hausdorff-like transform.  Then do it the
1700
 *          other way.  Take the component-wise max of the two results,
1701
 *          and threshold to get the fraction of pixels with a difference
1702
 *          below the threshold.
1703
 * </pre>
1704
 */
1705
l_ok
1706
pixGetPerceptualDiff(PIX        *pixs1,
1707
                     PIX        *pixs2,
1708
                     l_int32     sampling,
1709
                     l_int32     dilation,
1710
                     l_int32     mindiff,
1711
                     l_float32  *pfract,
1712
                     PIX       **ppixdiff1,
1713
                     PIX       **ppixdiff2)
1714
0
{
1715
0
l_int32  d1, d2, w, h, count;
1716
0
PIX     *pix1, *pix2, *pix3, *pix4, *pix5, *pix6, *pix7, *pix8, *pix9;
1717
0
PIX     *pix10, *pix11;
1718
1719
0
    if (ppixdiff1) *ppixdiff1 = NULL;
1720
0
    if (ppixdiff2) *ppixdiff2 = NULL;
1721
0
    if (!pfract)
1722
0
        return ERROR_INT("&fract not defined", __func__, 1);
1723
0
    *pfract = 1.0;  /* init to completely different */
1724
0
    if ((dilation & 1) == 0)
1725
0
        return ERROR_INT("dilation must be odd", __func__, 1);
1726
0
    if (!pixs1)
1727
0
        return ERROR_INT("pixs1 not defined", __func__, 1);
1728
0
    if (!pixs2)
1729
0
        return ERROR_INT("pixs2 not defined", __func__, 1);
1730
0
    d1 = pixGetDepth(pixs1);
1731
0
    d2 = pixGetDepth(pixs2);
1732
0
    if (!pixGetColormap(pixs1) && d1 < 8)
1733
0
        return ERROR_INT("pixs1 not cmapped and < 8 bpp", __func__, 1);
1734
0
    if (!pixGetColormap(pixs2) && d2 < 8)
1735
0
        return ERROR_INT("pixs2 not cmapped and < 8 bpp", __func__, 1);
1736
1737
        /* Integer downsample if requested */
1738
0
    if (sampling > 1) {
1739
0
        pix1 = pixScaleByIntSampling(pixs1, sampling);
1740
0
        pix2 = pixScaleByIntSampling(pixs2, sampling);
1741
0
    } else {
1742
0
        pix1 = pixClone(pixs1);
1743
0
        pix2 = pixClone(pixs2);
1744
0
    }
1745
1746
        /* Remove colormaps */
1747
0
    if (pixGetColormap(pix1)) {
1748
0
        pix3 = pixRemoveColormap(pix1, REMOVE_CMAP_BASED_ON_SRC);
1749
0
        d1 = pixGetDepth(pix3);
1750
0
    } else {
1751
0
        pix3 = pixClone(pix1);
1752
0
    }
1753
0
    if (pixGetColormap(pix2)) {
1754
0
        pix4 = pixRemoveColormap(pix2, REMOVE_CMAP_BASED_ON_SRC);
1755
0
        d2 = pixGetDepth(pix4);
1756
0
    } else {
1757
0
        pix4 = pixClone(pix2);
1758
0
    }
1759
0
    pixDestroy(&pix1);
1760
0
    pixDestroy(&pix2);
1761
0
    if (d1 != d2 || (d1 != 8 && d1 != 32)) {
1762
0
        pixDestroy(&pix3);
1763
0
        pixDestroy(&pix4);
1764
0
        L_INFO("depths unequal or not in {8,32}: d1 = %d, d2 = %d\n",
1765
0
               __func__, d1, d2);
1766
0
        return 1;
1767
0
    }
1768
1769
        /* In each direction, do a small dilation and subtract the dilated
1770
         * image from the other image to get a one-sided difference.
1771
         * Then take the max of the differences for each direction
1772
         * and clipping each component to 255 if necessary.  Note that
1773
         * for RGB images, the dilations and max selection are done
1774
         * component-wise, and the conversion to grayscale also uses the
1775
         * maximum component.  The resulting grayscale images are
1776
         * thresholded using %mindiff. */
1777
0
    if (d1 == 8) {
1778
0
        pix5 = pixDilateGray(pix3, dilation, dilation);
1779
0
        pixCompareGray(pix4, pix5, L_COMPARE_SUBTRACT, 0, NULL, NULL, NULL,
1780
0
                       &pix7);
1781
0
        pix6 = pixDilateGray(pix4, dilation, dilation);
1782
0
        pixCompareGray(pix3, pix6, L_COMPARE_SUBTRACT, 0, NULL, NULL, NULL,
1783
0
                       &pix8);
1784
0
        pix9 = pixMinOrMax(NULL, pix7, pix8, L_CHOOSE_MAX);
1785
0
        pix10 = pixThresholdToBinary(pix9, mindiff);
1786
0
        pixInvert(pix10, pix10);
1787
0
        pixCountPixels(pix10, &count, NULL);
1788
0
        pixGetDimensions(pix10, &w, &h, NULL);
1789
0
        *pfract = (w <= 0 || h <= 0) ? 0.0 :
1790
0
                                       (l_float32)count / (l_float32)(w * h);
1791
0
        pixDestroy(&pix5);
1792
0
        pixDestroy(&pix6);
1793
0
        pixDestroy(&pix7);
1794
0
        pixDestroy(&pix8);
1795
0
        if (ppixdiff1)
1796
0
            *ppixdiff1 = pix9;
1797
0
        else
1798
0
            pixDestroy(&pix9);
1799
0
        if (ppixdiff2)
1800
0
            *ppixdiff2 = pix10;
1801
0
        else
1802
0
            pixDestroy(&pix10);
1803
0
    } else {  /* d1 == 32 */
1804
0
        pix5 = pixColorMorph(pix3, L_MORPH_DILATE, dilation, dilation);
1805
0
        pixCompareRGB(pix4, pix5, L_COMPARE_SUBTRACT, 0, NULL, NULL, NULL,
1806
0
                       &pix7);
1807
0
        pix6 = pixColorMorph(pix4, L_MORPH_DILATE, dilation, dilation);
1808
0
        pixCompareRGB(pix3, pix6, L_COMPARE_SUBTRACT, 0, NULL, NULL, NULL,
1809
0
                      &pix8);
1810
0
        pix9 = pixMinOrMax(NULL, pix7, pix8, L_CHOOSE_MAX);
1811
0
        pix10 = pixConvertRGBToGrayMinMax(pix9, L_CHOOSE_MAX);
1812
0
        pix11 = pixThresholdToBinary(pix10, mindiff);
1813
0
        pixInvert(pix11, pix11);
1814
0
        pixCountPixels(pix11, &count, NULL);
1815
0
        pixGetDimensions(pix11, &w, &h, NULL);
1816
0
        *pfract = (w <= 0 || h <= 0) ? 0.0 :
1817
0
                                       (l_float32)count / (l_float32)(w * h);
1818
0
        pixDestroy(&pix5);
1819
0
        pixDestroy(&pix6);
1820
0
        pixDestroy(&pix7);
1821
0
        pixDestroy(&pix8);
1822
0
        pixDestroy(&pix10);
1823
0
        if (ppixdiff1)
1824
0
            *ppixdiff1 = pix9;
1825
0
        else
1826
0
            pixDestroy(&pix9);
1827
0
        if (ppixdiff2)
1828
0
            *ppixdiff2 = pix11;
1829
0
        else
1830
0
            pixDestroy(&pix11);
1831
1832
0
    }
1833
0
    pixDestroy(&pix3);
1834
0
    pixDestroy(&pix4);
1835
0
    return 0;
1836
0
}
1837
1838
1839
/*!
1840
 * \brief   pixGetPSNR()
1841
 *
1842
 * \param[in]    pix1, pix2     8 or 32 bpp; no colormap
1843
 * \param[in]    factor         sampling factor; >= 1
1844
 * \param[out]   ppsnr          power signal/noise ratio difference
1845
 * \return  0 if OK, 1 on error
1846
 *
1847
 * <pre>
1848
 * Notes:
1849
 *      (1) This computes the power S/N ratio, in dB, for the difference
1850
 *          between two images.  By convention, the power S/N
1851
 *          for a grayscale image is ('log' == log base 10,
1852
 *          and 'ln == log base e):
1853
 *            PSNR = 10 * log((255/MSE)^2)
1854
 *                 = 4.3429 * ln((255/MSE)^2)
1855
 *                 = -4.3429 * ln((MSE/255)^2)
1856
 *          where MSE is the mean squared error.
1857
 *          Here are some examples:
1858
 *             MSE             PSNR
1859
 *             ---             ----
1860
 *             10              28.1
1861
 *             3               38.6
1862
 *             1               48.1
1863
 *             0.1             68.1
1864
 *      (2) If pix1 and pix2 have the same pixel values, the MSE = 0.0
1865
 *          and the PSNR is infinity.  For that case, this returns
1866
 *          PSNR = 1000, which corresponds to the very small MSE of
1867
 *          about 10^(-48).
1868
 * </pre>
1869
 */
1870
l_ok
1871
pixGetPSNR(PIX        *pix1,
1872
           PIX        *pix2,
1873
           l_int32     factor,
1874
           l_float32  *ppsnr)
1875
0
{
1876
0
l_int32    same, i, j, w, h, d, wpl1, wpl2, v1, v2, r1, g1, b1, r2, g2, b2;
1877
0
l_uint32  *data1, *data2, *line1, *line2;
1878
0
l_float32  mse;  /* mean squared error */
1879
1880
0
    if (!ppsnr)
1881
0
        return ERROR_INT("&psnr not defined", __func__, 1);
1882
0
    *ppsnr = 0.0;
1883
0
    if (!pix1 || !pix2)
1884
0
        return ERROR_INT("empty input pix", __func__, 1);
1885
0
    if (!pixSizesEqual(pix1, pix2))
1886
0
        return ERROR_INT("pix sizes unequal", __func__, 1);
1887
0
    if (pixGetColormap(pix1))
1888
0
        return ERROR_INT("pix1 has colormap", __func__, 1);
1889
0
    if (pixGetColormap(pix2))
1890
0
        return ERROR_INT("pix2 has colormap", __func__, 1);
1891
0
    pixGetDimensions(pix1, &w, &h, &d);
1892
0
    if (d != 8 && d != 32)
1893
0
        return ERROR_INT("pix not 8 or 32 bpp", __func__, 1);
1894
0
    if (factor < 1)
1895
0
        return ERROR_INT("invalid sampling factor", __func__, 1);
1896
1897
0
    pixEqual(pix1, pix2, &same);
1898
0
    if (same) {
1899
0
        *ppsnr = 1000.0;  /* crazy big exponent */
1900
0
        return 0;
1901
0
    }
1902
1903
0
    data1 = pixGetData(pix1);
1904
0
    data2 = pixGetData(pix2);
1905
0
    wpl1 = pixGetWpl(pix1);
1906
0
    wpl2 = pixGetWpl(pix2);
1907
0
    mse = 0.0;
1908
0
    if (d == 8) {
1909
0
        for (i = 0; i < h; i += factor) {
1910
0
            line1 = data1 + i * wpl1;
1911
0
            line2 = data2 + i * wpl2;
1912
0
            for (j = 0; j < w; j += factor) {
1913
0
                v1 = GET_DATA_BYTE(line1, j);
1914
0
                v2 = GET_DATA_BYTE(line2, j);
1915
0
                mse += (l_float32)(v1 - v2) * (v1 - v2);
1916
0
            }
1917
0
        }
1918
0
    } else {  /* d == 32 */
1919
0
        for (i = 0; i < h; i += factor) {
1920
0
            line1 = data1 + i * wpl1;
1921
0
            line2 = data2 + i * wpl2;
1922
0
            for (j = 0; j < w; j += factor) {
1923
0
                extractRGBValues(line1[j], &r1, &g1, &b1);
1924
0
                extractRGBValues(line2[j], &r2, &g2, &b2);
1925
0
                mse += ((l_float32)(r1 - r2) * (r1 - r2) +
1926
0
                        (g1 - g2) * (g1 - g2) +
1927
0
                        (b1 - b2) * (b1 - b2)) / 3.0;
1928
0
            }
1929
0
        }
1930
0
    }
1931
0
    mse = mse / ((l_float32)(w) * h);
1932
1933
0
    *ppsnr = -4.3429448 * log(mse / (255 * 255));
1934
0
    return 0;
1935
0
}
1936
1937
1938
/*------------------------------------------------------------------*
1939
 *             Comparison of photo regions by histogram             *
1940
 *------------------------------------------------------------------*/
1941
/*!
1942
 * \brief   pixaComparePhotoRegionsByHisto()
1943
 *
1944
 * \param[in]    pixa        any depth; colormap OK
1945
 * \param[in]    minratio    requiring sizes be compatible; < 1.0
1946
 * \param[in]    textthresh  threshold for text/photo; use 0 for default
1947
 * \param[in]    factor      subsampling; >= 1
1948
 * \param[in]    n           in range {1, ... 7}. n^2 is the maximum number
1949
 *                           of subregions for histograms; typ. n = 3.
1950
 * \param[in]    simthresh   threshold for similarity; use 0 for default
1951
 * \param[out]   pnai array  giving similarity class indices
1952
 * \param[out]   pscores     [optional] score matrix as 1-D array of size N^2
1953
 * \param[out]   ppixd       [optional] pix of similarity classes
1954
 * \param[in]    debug       1 to output histograms; 0 otherwise
1955
 * \return  0 if OK, 1 on error
1956
 *
1957
 * <pre>
1958
 * Notes:
1959
 *      (1) This function takes a pixa of cropped photo images and
1960
 *          compares each one to the others for similarity.
1961
 *          Each image is first tested to see if it is a photo that can
1962
 *          be compared by tiled histograms.  If so, it is padded to put
1963
 *          the centroid in the center of the image, and the histograms
1964
 *          are generated.  The final step of comparing each histogram
1965
 *          with all the others is very fast.
1966
 *      (2) To make the histograms, each image is subdivided in a maximum
1967
 *          of n^2 subimages.  The parameter %n specifies the "side" of
1968
 *          an n x n grid of such subimages.  If the subimages have an
1969
 *          aspect ratio larger than 2, the grid will change, again using n^2
1970
 *          as a maximum for the number of subimages.  For example,
1971
 *          if n == 3, but the image is 600 x 200 pixels, a 3x3 grid
1972
 *          would have subimages of 200 x 67 pixels, which is more
1973
 *          than 2:1, so we change to a 4x2 grid where each subimage
1974
 *          has 150 x 100 pixels.
1975
 *      (3) An initial filter gives %score = 0 if the ratio of widths
1976
 *          and heights (smallest / largest) does not exceed a
1977
 *          threshold %minratio.  If set at 1.0, both images must be
1978
 *          exactly the same size.  A typical value for %minratio is 0.9.
1979
 *      (4) The comparison score between two images is a value in [0.0 .. 1.0].
1980
 *          If the comparison score >= %simthresh, the images are placed in
1981
 *          the same similarity class.  Default value for %simthresh is 0.25.
1982
 *      (5) An array %nai of similarity class indices for pix in the
1983
 *          input pixa is returned.
1984
 *      (6) There are two debugging options:
1985
 *          * An optional 2D matrix of scores is returned as a 1D array.
1986
 *            A visualization of this is written to a temp file.
1987
 *          * An optional pix showing the similarity classes can be
1988
 *            returned.  Text in each input pix is reproduced.
1989
 *      (7) See the notes in pixComparePhotoRegionsByHisto() for details
1990
 *          on the implementation.
1991
 * </pre>
1992
 */
1993
l_ok
1994
pixaComparePhotoRegionsByHisto(PIXA        *pixa,
1995
                               l_float32    minratio,
1996
                               l_float32    textthresh,
1997
                               l_int32      factor,
1998
                               l_int32      n,
1999
                               l_float32    simthresh,
2000
                               NUMA       **pnai,
2001
                               l_float32  **pscores,
2002
                               PIX        **ppixd,
2003
                               l_int32      debug)
2004
0
{
2005
0
char       *text;
2006
0
l_int32     i, j, nim, w, h, w1, h1, w2, h2, ival, index, classid;
2007
0
l_float32   score;
2008
0
l_float32  *scores;
2009
0
NUMA       *nai, *naw, *nah;
2010
0
NUMAA      *naa;
2011
0
NUMAA     **n3a;  /* array of naa */
2012
0
PIX        *pix;
2013
2014
0
    if (pscores) *pscores = NULL;
2015
0
    if (ppixd) *ppixd = NULL;
2016
0
    if (!pnai)
2017
0
        return ERROR_INT("&na not defined", __func__, 1);
2018
0
    *pnai = NULL;
2019
0
    if (!pixa)
2020
0
        return ERROR_INT("pixa not defined", __func__, 1);
2021
0
    if (minratio < 0.0 || minratio > 1.0)
2022
0
        return ERROR_INT("minratio not in [0.0 ... 1.0]", __func__, 1);
2023
0
    if (textthresh <= 0.0) textthresh = 1.3f;
2024
0
    if (factor < 1)
2025
0
        return ERROR_INT("subsampling factor must be >= 1", __func__, 1);
2026
0
    if (n < 1 || n > 7) {
2027
0
        L_WARNING("n = %d is invalid; setting to 4\n", __func__, n);
2028
0
        n = 4;
2029
0
    }
2030
0
    if (simthresh <= 0.0) simthresh = 0.25;
2031
0
    if (simthresh > 1.0)
2032
0
        return ERROR_INT("simthresh invalid; should be near 0.25", __func__, 1);
2033
2034
        /* Prepare the histograms */
2035
0
    nim = pixaGetCount(pixa);
2036
0
    if ((n3a = (NUMAA **)LEPT_CALLOC(nim, sizeof(NUMAA *))) == NULL)
2037
0
        return ERROR_INT("calloc fail for n3a", __func__, 1);
2038
0
    naw = numaCreate(0);
2039
0
    nah = numaCreate(0);
2040
0
    for (i = 0; i < nim; i++) {
2041
0
        pix = pixaGetPix(pixa, i, L_CLONE);
2042
0
        text = pixGetText(pix);
2043
0
        pixSetResolution(pix, 150, 150);
2044
0
        index = (debug) ? i : 0;
2045
0
        pixGenPhotoHistos(pix, NULL, factor, textthresh, n,
2046
0
                          &naa, &w, &h, index);
2047
0
        n3a[i] = naa;
2048
0
        numaAddNumber(naw, w);
2049
0
        numaAddNumber(nah, h);
2050
0
        if (naa)
2051
0
            lept_stderr("Image %s is photo\n", text);
2052
0
        else
2053
0
            lept_stderr("Image %s is NOT photo\n", text);
2054
0
        pixDestroy(&pix);
2055
0
    }
2056
2057
        /* Do the comparisons.  We are making a set of classes, where
2058
         * all similar images are placed in the same class.  There are
2059
         * 'nim' input images.  The classes are labeled by 'classid' (all
2060
         * similar images get the same 'classid' value), and 'nai' maps
2061
         * the classid of the image in the input array to the classid
2062
         * of the similarity class.  */
2063
0
    if ((scores =
2064
0
               (l_float32 *)LEPT_CALLOC((size_t)nim * nim, sizeof(l_float32)))
2065
0
                == NULL) {
2066
0
        L_ERROR("calloc fail for scores\n", __func__);
2067
0
        goto cleanup;
2068
0
    }
2069
0
    nai = numaMakeConstant(-1, nim);  /* classid array */
2070
0
    for (i = 0, classid = 0; i < nim; i++) {
2071
0
        scores[nim * i + i] = 1.0;
2072
0
        numaGetIValue(nai, i, &ival);
2073
0
        if (ival != -1)  /* already set */
2074
0
            continue;
2075
0
        numaSetValue(nai, i, classid);
2076
0
        if (n3a[i] == NULL) {  /* not a photo */
2077
0
            classid++;
2078
0
            continue;
2079
0
        }
2080
0
        numaGetIValue(naw, i, &w1);
2081
0
        numaGetIValue(nah, i, &h1);
2082
0
        for (j = i + 1; j < nim; j++) {
2083
0
            numaGetIValue(nai, j, &ival);
2084
0
            if (ival != -1)  /* already set */
2085
0
                continue;
2086
0
            if (n3a[j] == NULL)  /* not a photo */
2087
0
                continue;
2088
0
            numaGetIValue(naw, j, &w2);
2089
0
            numaGetIValue(nah, j, &h2);
2090
0
            compareTilesByHisto(n3a[i], n3a[j], minratio, w1, h1, w2, h2,
2091
0
                                &score, NULL);
2092
0
            scores[nim * i + j] = score;
2093
0
            scores[nim * j + i] = score;  /* the score array is symmetric */
2094
/*            lept_stderr("score = %5.3f\n", score); */
2095
0
            if (score > simthresh) {
2096
0
                numaSetValue(nai, j, classid);
2097
0
                lept_stderr(
2098
0
                        "Setting %d similar to %d, in class %d; score %5.3f\n",
2099
0
                        j, i, classid, score);
2100
0
            }
2101
0
        }
2102
0
        classid++;
2103
0
    }
2104
0
    *pnai = nai;
2105
2106
        /* Debug: optionally save and display the score array.
2107
         * All images that are photos are represented by a point on
2108
         * the diagonal. Other images in the same similarity class
2109
         * are on the same horizontal raster line to the right.
2110
         * The array has been symmetrized, so images in the same
2111
         * same similarity class also appear on the same column below. */
2112
0
    if (pscores) {
2113
0
        l_int32    wpl, fact;
2114
0
        l_uint32  *line, *data;
2115
0
        PIX       *pix2, *pix3;
2116
0
        pix2 = pixCreate(nim, nim, 8);
2117
0
        data = pixGetData(pix2);
2118
0
        wpl = pixGetWpl(pix2);
2119
0
        for (i = 0; i < nim; i++) {
2120
0
            line = data + i * wpl;
2121
0
            for (j = 0; j < nim; j++) {
2122
0
                SET_DATA_BYTE(line, j,
2123
0
                              L_MIN(255, 4.0 * 255 * scores[nim * i + j]));
2124
0
            }
2125
0
        }
2126
0
        fact = L_MAX(2, 1000 / nim);
2127
0
        pix3 = pixExpandReplicate(pix2, fact);
2128
0
        lept_stderr("Writing to /tmp/lept/comp/scorearray.png\n");
2129
0
        lept_mkdir("lept/comp");
2130
0
        pixWrite("/tmp/lept/comp/scorearray.png", pix3, IFF_PNG);
2131
0
        pixDestroy(&pix2);
2132
0
        pixDestroy(&pix3);
2133
0
        *pscores = scores;
2134
0
    } else {
2135
0
        LEPT_FREE(scores);
2136
0
    }
2137
2138
        /* Debug: optionally display and save the image comparisons.
2139
         * Image similarity classes are displayed by column; similar
2140
         * images are displayed in the same column. */
2141
0
    if (ppixd)
2142
0
        *ppixd = pixaDisplayTiledByIndex(pixa, nai, 200, 20, 2, 6, 0x0000ff00);
2143
2144
0
cleanup:
2145
0
    numaDestroy(&naw);
2146
0
    numaDestroy(&nah);
2147
0
    for (i = 0; i < nim; i++)
2148
0
        numaaDestroy(&n3a[i]);
2149
0
    LEPT_FREE(n3a);
2150
0
    return 0;
2151
0
}
2152
2153
2154
/*!
2155
 * \brief   pixComparePhotoRegionsByHisto()
2156
 *
2157
 * \param[in]    pix1, pix2    any depth; colormap OK
2158
 * \param[in]    box1, box2    [optional] photo regions from each; can be null
2159
 * \param[in]    minratio      requiring sizes be compatible; < 1.0
2160
 * \param[in]    factor        subsampling factor; >= 1
2161
 * \param[in]    n             in range {1, ... 7}. n^2 is the maximum number
2162
 *                             of subregions for histograms; typ. n = 3.
2163
 * \param[out]   pscore        similarity score of histograms
2164
 * \param[in]    debugflag     1 for debug output; 0 for no debugging
2165
 * \return  0 if OK, 1 on error
2166
 *
2167
 * <pre>
2168
 * Notes:
2169
 *      (1) This function compares two grayscale photo regions.  If a
2170
 *          box is given, the region is clipped; otherwise assume
2171
 *          the entire images are photo regions.  This is done with a
2172
 *          set of not more than n^2 spatially aligned histograms, which are
2173
 *          aligned using the centroid of the inverse image.
2174
 *      (2) The parameter %n specifies the "side" of an n x n grid
2175
 *          of subimages.  If the subimages have an aspect ratio larger
2176
 *          than 2, the grid will change, using n^2 as a maximum for
2177
 *          the number of subimages.  For example, if n == 3, but the
2178
 *          image is 600 x 200 pixels, a 3x3 grid would have subimages
2179
 *          of 200 x 67 pixels, which is more than 2:1, so we change
2180
 *          to a 4x2 grid where each subimage has 150 x 100 pixels.
2181
 *      (3) An initial filter gives %score = 0 if the ratio of widths
2182
 *          and heights (smallest / largest) does not exceed a
2183
 *          threshold %minratio.  This must be between 0.5 and 1.0.
2184
 *          If set at 1.0, both images must be exactly the same size.
2185
 *          A typical value for %minratio is 0.9.
2186
 *      (4) Because this function should not be used on text or
2187
 *          line graphics, which can give false positive results
2188
 *          (i.e., high scores for different images), filter the images
2189
 *          using pixGenPhotoHistos(), which returns tiled histograms
2190
 *          only if an image is not text and comparison is expected
2191
 *          to work with histograms.  If either image fails the test,
2192
 *          the comparison returns a score of 0.0.
2193
 *      (5) The white value counts in the histograms are removed; they
2194
 *          are typically pixels that were padded to achieve alignment.
2195
 *      (6) For an efficient representation of the histogram, normalize
2196
 *          using a multiplicative factor so that the number in the
2197
 *          maximum bucket is 255.  It then takes 256 bytes to store.
2198
 *      (7) When comparing the histograms of two regions, use the
2199
 *          Earth Mover distance (EMD), with the histograms normalized
2200
 *          so that the sum over bins is the same.  Further normalize
2201
 *          by dividing by 255, so that the result is in [0.0 ... 1.0].
2202
 *      (8) Get a similarity score S = 1.0 - k * D, where
2203
 *            k is a constant, say in the range 5-10
2204
 *            D = normalized EMD
2205
 *          and for multiple tiles, take the Min(S) to be the final score.
2206
 *          Using aligned tiles gives protection against accidental
2207
 *          similarity of the overall grayscale histograms.
2208
 *          A small number of aligned tiles works well.
2209
 *      (9) With debug on, you get a pdf that shows, for each tile,
2210
 *          the images, histograms and score.
2211
 * </pre>
2212
 */
2213
l_ok
2214
pixComparePhotoRegionsByHisto(PIX        *pix1,
2215
                              PIX        *pix2,
2216
                              BOX        *box1,
2217
                              BOX        *box2,
2218
                              l_float32   minratio,
2219
                              l_int32     factor,
2220
                              l_int32     n,
2221
                              l_float32  *pscore,
2222
                              l_int32     debugflag)
2223
0
{
2224
0
l_int32    w1, h1, w2, h2, w1c, h1c, w2c, h2c, debugindex;
2225
0
l_float32  wratio, hratio;
2226
0
NUMAA     *naa1, *naa2;
2227
0
PIX       *pix3, *pix4;
2228
0
PIXA      *pixa;
2229
2230
0
    if (!pscore)
2231
0
        return ERROR_INT("&score not defined", __func__, 1);
2232
0
    *pscore = 0.0;
2233
0
    if (!pix1 || !pix2)
2234
0
        return ERROR_INT("pix1 and pix2 not both defined", __func__, 1);
2235
0
    if (minratio < 0.5 || minratio > 1.0)
2236
0
        return ERROR_INT("minratio not in [0.5 ... 1.0]", __func__, 1);
2237
0
    if (factor < 1)
2238
0
        return ERROR_INT("subsampling factor must be >= 1", __func__, 1);
2239
0
    if (n < 1 || n > 7) {
2240
0
        L_WARNING("n = %d is invalid; setting to 4\n", __func__, n);
2241
0
        n = 4;
2242
0
    }
2243
2244
0
    debugindex = 0;
2245
0
    if (debugflag) {
2246
0
        lept_mkdir("lept/comp");
2247
0
        debugindex = 666;  /* arbitrary number used for naming output */
2248
0
    }
2249
2250
        /* Initial filter by size */
2251
0
    if (box1)
2252
0
        boxGetGeometry(box1, NULL, NULL, &w1, &h1);
2253
0
    else
2254
0
        pixGetDimensions(pix1, &w1, &h1, NULL);
2255
0
    if (box2)
2256
0
        boxGetGeometry(box2, NULL, NULL, &w2, &h2);
2257
0
    else
2258
0
        pixGetDimensions(pix1, &w2, &h2, NULL);
2259
0
    wratio = (w1 < w2) ? (l_float32)w1 / (l_float32)w2 :
2260
0
             (l_float32)w2 / (l_float32)w1;
2261
0
    hratio = (h1 < h2) ? (l_float32)h1 / (l_float32)h2 :
2262
0
             (l_float32)h2 / (l_float32)h1;
2263
0
    if (wratio < minratio || hratio < minratio)
2264
0
        return 0;
2265
2266
        /* Initial crop, if necessary, and make histos */
2267
0
    if (box1)
2268
0
        pix3 = pixClipRectangle(pix1, box1, NULL);
2269
0
    else
2270
0
        pix3 = pixClone(pix1);
2271
0
    pixGenPhotoHistos(pix3, NULL, factor, 0, n, &naa1, &w1c, &h1c, debugindex);
2272
0
    pixDestroy(&pix3);
2273
0
    if (!naa1) return 0;
2274
0
    if (box2)
2275
0
        pix4 = pixClipRectangle(pix2, box2, NULL);
2276
0
    else
2277
0
        pix4 = pixClone(pix2);
2278
0
    pixGenPhotoHistos(pix4, NULL, factor, 0, n, &naa2, &w2c, &h2c, debugindex);
2279
0
    pixDestroy(&pix4);
2280
0
    if (!naa2) return 0;
2281
2282
        /* Compare histograms */
2283
0
    pixa = (debugflag) ? pixaCreate(0) : NULL;
2284
0
    compareTilesByHisto(naa1, naa2, minratio, w1c, h1c, w2c, h2c, pscore, pixa);
2285
0
    pixaDestroy(&pixa);
2286
0
    return 0;
2287
0
}
2288
2289
2290
/*!
2291
 * \brief   pixGenPhotoHistos()
2292
 *
2293
 * \param[in]    pixs      depth > 1 bpp; colormap OK
2294
 * \param[in]    box       [optional] region to be selected; can be null
2295
 * \param[in]    factor    subsampling; >= 1
2296
 * \param[in]    thresh    threshold for photo/text; use 0 for default
2297
 * \param[in]    n         in range {1, ... 7}. n^2 is the maximum number
2298
 *                         of subregions for histograms; typ. n = 3.
2299
 * \param[out]   pnaa      nx * ny 256-entry gray histograms
2300
 * \param[out]   pw        width of image used to make histograms
2301
 * \param[out]   ph        height of image used to make histograms
2302
 * \param[in]    debugindex  0 for no debugging; positive integer otherwise
2303
 * \return  0 if OK, 1 on error
2304
 *
2305
 * <pre>
2306
 * Notes:
2307
 *      (1) This crops and converts to 8 bpp if necessary.  It adds a
2308
 *          minimal white boundary such that the centroid of the
2309
 *          photo-inverted image is in the center. This allows
2310
 *          automatic alignment with histograms of other image regions.
2311
 *      (2) The parameter %n specifies the "side" of the n x n grid
2312
 *          of subimages.  If the subimages have an aspect ratio larger
2313
 *          than 2, the grid will change, using n^2 as a maximum for
2314
 *          the number of subimages.  For example, if n == 3, but the
2315
 *          image is 600 x 200 pixels, a 3x3 grid would have subimages
2316
 *          of 200 x 67 pixels, which is more than 2:1, so we change
2317
 *          to a 4x2 grid where each subimage has 150 x 100 pixels.
2318
 *      (3) The white value in the histogram is removed, because of
2319
 *          the padding.
2320
 *      (4) Use 0 for conservative default (1.3) for thresh.
2321
 *      (5) For an efficient representation of the histogram, normalize
2322
 *          using a multiplicative factor so that the number in the
2323
 *          maximum bucket is 255.  It then takes 256 bytes to store.
2324
 *      (6) With %debugindex > 0, this makes a pdf that shows, for each tile,
2325
 *          the images and histograms.
2326
 * </pre>
2327
 */
2328
l_ok
2329
pixGenPhotoHistos(PIX        *pixs,
2330
                  BOX        *box,
2331
                  l_int32     factor,
2332
                  l_float32   thresh,
2333
                  l_int32     n,
2334
                  NUMAA     **pnaa,
2335
                  l_int32    *pw,
2336
                  l_int32    *ph,
2337
                  l_int32     debugindex)
2338
0
{
2339
0
char    buf[64];
2340
0
NUMAA  *naa;
2341
0
PIX    *pix1, *pix2, *pix3, *pixm;
2342
0
PIXA   *pixa;
2343
2344
0
    if (pnaa) *pnaa = NULL;
2345
0
    if (pw) *pw = 0;
2346
0
    if (ph) *ph = 0;
2347
0
    if (!pnaa)
2348
0
        return ERROR_INT("&naa not defined", __func__, 1);
2349
0
    if (!pw || !ph)
2350
0
        return ERROR_INT("&w and &h not both defined", __func__, 1);
2351
0
    if (!pixs || pixGetDepth(pixs) == 1)
2352
0
        return ERROR_INT("pixs not defined or 1 bpp", __func__, 1);
2353
0
    if (factor < 1)
2354
0
        return ERROR_INT("subsampling factor must be >= 1", __func__, 1);
2355
0
    if (thresh <= 0.0) thresh = 1.3f;  /* default */
2356
0
    if (n < 1 || n > 7) {
2357
0
        L_WARNING("n = %d is invalid; setting to 4\n", __func__, n);
2358
0
        n = 4;
2359
0
    }
2360
2361
0
    pixa = NULL;
2362
0
    if (debugindex > 0) {
2363
0
        pixa = pixaCreate(0);
2364
0
        lept_mkdir("lept/comp");
2365
0
    }
2366
2367
        /* Initial crop, if necessary */
2368
0
    if (box)
2369
0
        pix1 = pixClipRectangle(pixs, box, NULL);
2370
0
    else
2371
0
        pix1 = pixClone(pixs);
2372
2373
        /* Convert to 8 bpp and pad to center the centroid */
2374
0
    pix2 = pixConvertTo8(pix1, FALSE);
2375
0
    pix3 = pixPadToCenterCentroid(pix2, factor);
2376
2377
        /* Set to 255 all pixels above 230.  Do this so that light gray
2378
         * pixels do not enter into the comparison. */
2379
0
    pixm = pixThresholdToBinary(pix3, 230);
2380
0
    pixInvert(pixm, pixm);
2381
0
    pixSetMaskedGeneral(pix3, pixm, 255, 0, 0);
2382
0
    pixDestroy(&pixm);
2383
2384
0
    if (debugindex > 0) {
2385
0
        PIX   *pix4, *pix5, *pix6, *pix7, *pix8;
2386
0
        PIXA  *pixa2;
2387
0
        pix4 = pixConvertTo32(pix2);
2388
0
        pix5 = pixConvertTo32(pix3);
2389
0
        pix6 = pixScaleToSize(pix4, 400, 0);
2390
0
        pix7 = pixScaleToSize(pix5, 400, 0);
2391
0
        pixa2 = pixaCreate(2);
2392
0
        pixaAddPix(pixa2, pix6, L_INSERT);
2393
0
        pixaAddPix(pixa2, pix7, L_INSERT);
2394
0
        pix8 = pixaDisplayTiledInRows(pixa2, 32, 1000, 1.0, 0, 50, 3);
2395
0
        pixaAddPix(pixa, pix8, L_INSERT);
2396
0
        pixDestroy(&pix4);
2397
0
        pixDestroy(&pix5);
2398
0
        pixaDestroy(&pixa2);
2399
0
    }
2400
0
    pixDestroy(&pix1);
2401
0
    pixDestroy(&pix2);
2402
2403
        /* Test if this is a photoimage */
2404
0
    pixDecideIfPhotoImage(pix3, factor, thresh, n, &naa, pixa);
2405
0
    if (naa) {
2406
0
        *pnaa = naa;
2407
0
        *pw = pixGetWidth(pix3);
2408
0
        *ph = pixGetHeight(pix3);
2409
0
    }
2410
2411
0
    if (pixa) {
2412
0
        snprintf(buf, sizeof(buf), "/tmp/lept/comp/tiledhistos.%d.pdf",
2413
0
                 debugindex);
2414
0
        lept_stderr("Writing to %s\n", buf);
2415
0
        pixaConvertToPdf(pixa, 300, 1.0, L_FLATE_ENCODE, 0, NULL, buf);
2416
0
        pixaDestroy(&pixa);
2417
0
    }
2418
2419
0
    pixDestroy(&pix3);
2420
0
    return 0;
2421
0
}
2422
2423
2424
/*!
2425
 * \brief   pixPadToCenterCentroid()
2426
 *
2427
 * \param[in]    pixs     any depth, colormap OK
2428
 * \param[in]    factor   subsampling for centroid; >= 1
2429
 * \return  pixd padded with white pixels, or NULL on error.
2430
 *
2431
 * <pre>
2432
 * Notes:
2433
 *      (1) This add minimum white padding to an 8 bpp pix, such that
2434
 *          the centroid of the photometric inverse is in the center of
2435
 *          the resulting image.  Thus in computing the centroid,
2436
 *          black pixels have weight 255, and white pixels have weight 0.
2437
 * </pre>
2438
 */
2439
PIX *
2440
pixPadToCenterCentroid(PIX     *pixs,
2441
                       l_int32  factor)
2442
2443
0
{
2444
0
l_float32  cx, cy;
2445
0
l_int32    xs, ys, delx, dely, icx, icy, ws, hs, wd, hd;
2446
0
PIX       *pix1, *pixd;
2447
2448
0
    if (!pixs)
2449
0
        return (PIX *)ERROR_PTR("pixs not defined", __func__, NULL);
2450
0
    if (factor < 1)
2451
0
        return (PIX *)ERROR_PTR("invalid sampling factor", __func__, NULL);
2452
2453
0
    pix1 = pixConvertTo8(pixs, FALSE);
2454
0
    pixCentroid8(pix1, factor, &cx, &cy);
2455
0
    icx = (l_int32)(cx + 0.5);
2456
0
    icy = (l_int32)(cy + 0.5);
2457
0
    pixGetDimensions(pix1, &ws, &hs, NULL);
2458
0
    delx = ws - 2 * icx;
2459
0
    dely = hs - 2 * icy;
2460
0
    xs = L_MAX(0, delx);
2461
0
    ys = L_MAX(0, dely);
2462
0
    wd = 2 * L_MAX(icx, ws - icx);
2463
0
    hd = 2 * L_MAX(icy, hs - icy);
2464
0
    pixd = pixCreate(wd, hd, 8);
2465
0
    pixSetAll(pixd);  /* to white */
2466
0
    pixCopyResolution(pixd, pixs);
2467
0
    pixRasterop(pixd, xs, ys, ws, hs, PIX_SRC, pix1, 0, 0);
2468
0
    pixDestroy(&pix1);
2469
0
    return pixd;
2470
0
}
2471
2472
2473
/*!
2474
 * \brief   pixCentroid8()
2475
 *
2476
 * \param[in]    pixs    8 bpp
2477
 * \param[in]    factor  subsampling factor; >= 1
2478
 * \param[out]   pcx     x value of centroid
2479
 * \param[out]   pcy     y value of centroid
2480
 * \return  0 if OK, 1 on error
2481
 *
2482
 * <pre>
2483
 * Notes:
2484
 *      (1) This first does a photometric inversion (black = 255, white = 0).
2485
 *          It then finds the centroid of the result.  The inversion is
2486
 *          done because white is usually background, so the centroid
2487
 *          is computed based on the "foreground" gray pixels, and the
2488
 *          darker the pixel, the more weight it is given.
2489
 * </pre>
2490
 */
2491
l_ok
2492
pixCentroid8(PIX        *pixs,
2493
             l_int32     factor,
2494
             l_float32  *pcx,
2495
             l_float32  *pcy)
2496
0
{
2497
0
l_int32    i, j, w, h, wpl, val;
2498
0
l_float32  sumx, sumy, sumv;
2499
0
l_uint32  *data, *line;
2500
0
PIX       *pix1;
2501
2502
0
    if (pcx) *pcx = 0.0;
2503
0
    if (pcy) *pcy = 0.0;
2504
0
    if (!pixs || pixGetDepth(pixs) != 8)
2505
0
        return ERROR_INT("pixs undefined or not 8 bpp", __func__, 1);
2506
0
    if (factor < 1)
2507
0
        return ERROR_INT("subsampling factor must be >= 1", __func__, 1);
2508
0
    if (!pcx || !pcy)
2509
0
        return ERROR_INT("&cx and &cy not both defined", __func__, 1);
2510
2511
0
    pix1 = pixInvert(NULL, pixs);
2512
0
    pixGetDimensions(pix1, &w, &h, NULL);
2513
0
    data = pixGetData(pix1);
2514
0
    wpl = pixGetWpl(pix1);
2515
0
    sumx = sumy = sumv = 0.0;
2516
0
    for (i = 0; i < h; i++) {
2517
0
        line = data + i * wpl;
2518
0
        for (j = 0; j < w; j++) {
2519
0
            val = GET_DATA_BYTE(line, j);
2520
0
            sumx += val * j;
2521
0
            sumy += val * i;
2522
0
            sumv += val;
2523
0
        }
2524
0
    }
2525
0
    pixDestroy(&pix1);
2526
2527
0
    if (sumv == 0) {
2528
0
        L_INFO("input image is white\n", __func__);
2529
0
        *pcx = (l_float32)(w) / 2;
2530
0
        *pcy = (l_float32)(h) / 2;
2531
0
    } else {
2532
0
        *pcx = sumx / sumv;
2533
0
        *pcy = sumy / sumv;
2534
0
    }
2535
2536
0
    return 0;
2537
0
}
2538
2539
2540
/*!
2541
 * \brief   pixDecideIfPhotoImage()
2542
 *
2543
 * \param[in]    pix         8 bpp, centroid in center
2544
 * \param[in]    factor      subsampling for histograms; >= 1
2545
 * \param[in]    thresh      threshold for photo/text; use 0 for default
2546
 * \param[in]    n           in range {1, ... 7}. n^2 is the maximum number
2547
 *                           of subregions for histograms; typ. n = 3.
2548
 * \param[out]   pnaa        array of normalized histograms
2549
 * \param[in]    pixadebug   [optional] use only for debug output
2550
 * \return  0 if OK, 1 on error
2551
 *
2552
 * <pre>
2553
 * Notes:
2554
 *      (1) The input image must be 8 bpp (no colormap), and padded with
2555
 *          white pixels so the centroid of photo-inverted pixels is at
2556
 *          the center of the image.
2557
 *      (2) The parameter %n specifies the "side" of the n x n grid
2558
 *          of subimages.  If the subimages have an aspect ratio larger
2559
 *          than 2, the grid will change, using n^2 as a maximum for
2560
 *          the number of subimages.  For example, if n == 3, but the
2561
 *          image is 600 x 200 pixels, a 3x3 grid would have subimages
2562
 *          of 200 x 67 pixels, which is more than 2:1, so we change
2563
 *          to a 4x2 grid where each subimage has 150 x 100 pixels.
2564
 *      (3) If the pix is not almost certainly a photoimage, the returned
2565
 *          histograms (%naa) are null.
2566
 *      (4) If histograms are generated, the white (255) count is set
2567
 *          to 0.  This removes all pixels values above 230, including
2568
 *          white padding from the centroid matching operation, from
2569
 *          consideration.  The resulting histograms are then normalized
2570
 *          so the maximum count is 255.
2571
 *      (5) Default for %thresh is 1.3; this seems sufficiently conservative.
2572
 *      (6) Use %pixadebug == NULL unless debug output is requested.
2573
 * </pre>
2574
 */
2575
l_ok
2576
pixDecideIfPhotoImage(PIX       *pix,
2577
                      l_int32    factor,
2578
                      l_float32  thresh,
2579
                      l_int32    n,
2580
                      NUMAA    **pnaa,
2581
                      PIXA      *pixadebug)
2582
0
{
2583
0
char       buf[64];
2584
0
l_int32    i, w, h, nx, ny, ngrids, istext, isphoto;
2585
0
l_float32  maxval, sum1, sum2, ratio;
2586
0
L_BMF     *bmf;
2587
0
NUMA      *na1, *na2, *na3, *narv;
2588
0
NUMAA     *naa;
2589
0
PIX       *pix1;
2590
0
PIXA      *pixa1, *pixa2, *pixa3;
2591
2592
0
    if (!pnaa)
2593
0
        return ERROR_INT("&naa not defined", __func__, 1);
2594
0
    *pnaa = NULL;
2595
0
    if (!pix || pixGetDepth(pix) != 8 || pixGetColormap(pix))
2596
0
        return ERROR_INT("pix undefined or invalid", __func__, 1);
2597
0
    if (n < 1 || n > 7) {
2598
0
        L_WARNING("n = %d is invalid; setting to 4\n", __func__, n);
2599
0
        n = 4;
2600
0
    }
2601
0
    if (thresh <= 0.0) thresh = 1.3f;  /* default */
2602
2603
        /* Look for text lines */
2604
0
    pixDecideIfText(pix, NULL, &istext, pixadebug);
2605
0
    if (istext) {
2606
0
        L_INFO("Image is text\n", __func__);
2607
0
        return 0;
2608
0
    }
2609
2610
        /* Determine grid from n */
2611
0
    pixGetDimensions(pix, &w, &h, NULL);
2612
0
    if (w == 0 || h == 0)
2613
0
        return ERROR_INT("invalid pix dimension", __func__, 1);
2614
0
    findHistoGridDimensions(n, w, h, &nx, &ny, 1);
2615
2616
        /* Evaluate histograms in each tile */
2617
0
    pixa1 = pixaSplitPix(pix, nx, ny, 0, 0);
2618
0
    ngrids = nx * ny;
2619
0
    bmf = (pixadebug) ? bmfCreate(NULL, 6) : NULL;
2620
0
    naa = numaaCreate(ngrids);
2621
0
    if (pixadebug) {
2622
0
        lept_rmdir("lept/compplot");
2623
0
        lept_mkdir("lept/compplot");
2624
0
    }
2625
0
    for (i = 0; i < ngrids; i++) {
2626
0
        pix1 = pixaGetPix(pixa1, i, L_CLONE);
2627
2628
            /* Get histograms, set white count to 0, normalize max to 255 */
2629
0
        na1 = pixGetGrayHistogram(pix1, factor);
2630
0
        numaSetValue(na1, 255, 0);
2631
0
        na2 = numaWindowedMean(na1, 5);  /* do some smoothing */
2632
0
        numaGetMax(na2, &maxval, NULL);
2633
0
        na3 = numaTransform(na2, 0, 255.0 / maxval);
2634
0
        if (pixadebug) {
2635
0
            snprintf(buf, sizeof(buf), "/tmp/lept/compplot/plot.%d", i);
2636
0
            gplotSimple1(na3, GPLOT_PNG, buf, "Histos");
2637
0
        }
2638
2639
0
        numaaAddNuma(naa, na3, L_INSERT);
2640
0
        numaDestroy(&na1);
2641
0
        numaDestroy(&na2);
2642
0
        pixDestroy(&pix1);
2643
0
    }
2644
0
    if (pixadebug) {
2645
0
        pix1 = pixaDisplayTiledInColumns(pixa1, nx, 1.0, 30, 2);
2646
0
        pixaAddPix(pixadebug, pix1, L_INSERT);
2647
0
        pixa2 = pixaReadFiles("/tmp/lept/compplot", ".png");
2648
0
        pixa3 = pixaScale(pixa2, 0.4f, 0.4f);
2649
0
        pix1 = pixaDisplayTiledInColumns(pixa3, nx, 1.0, 30, 2);
2650
0
        pixaAddPix(pixadebug, pix1, L_INSERT);
2651
0
        pixaDestroy(&pixa2);
2652
0
        pixaDestroy(&pixa3);
2653
0
    }
2654
2655
        /* Compute the standard deviation between these histos to decide
2656
         * if the image is photo or something more like line art,
2657
         * which does not support good comparison by tiled histograms.  */
2658
0
    grayInterHistogramStats(naa, 5, NULL, NULL, NULL, &narv);
2659
2660
        /* For photos, the root variance has a larger weight of
2661
         * values in the range [50 ... 150] compared to [200 ... 230],
2662
         * than text or line art.  For the latter, most of the variance
2663
         * between tiles is in the lightest parts of the image, well
2664
         * above 150.  */
2665
0
    numaGetSumOnInterval(narv, 50, 150, &sum1);
2666
0
    numaGetSumOnInterval(narv, 200, 230, &sum2);
2667
0
    if (sum2 == 0.0) {  /* shouldn't happen */
2668
0
        ratio = 0.001f;  /* anything very small for debug output */
2669
0
        isphoto = 0;  /* be conservative */
2670
0
    } else {
2671
0
        ratio = sum1 / sum2;
2672
0
        isphoto = (ratio > thresh) ? 1 : 0;
2673
0
    }
2674
0
    if (pixadebug) {
2675
0
        if (isphoto)
2676
0
            L_INFO("ratio %f > %f; isphoto is true\n",
2677
0
                   __func__, ratio, thresh);
2678
0
        else
2679
0
            L_INFO("ratio %f < %f; isphoto is false\n",
2680
0
                   __func__, ratio, thresh);
2681
0
    }
2682
0
    if (isphoto)
2683
0
        *pnaa = naa;
2684
0
    else
2685
0
        numaaDestroy(&naa);
2686
0
    bmfDestroy(&bmf);
2687
0
    numaDestroy(&narv);
2688
0
    pixaDestroy(&pixa1);
2689
0
    return 0;
2690
0
}
2691
2692
2693
/*!
2694
 * \brief   findHistoGridDimensions()
2695
 *
2696
 * \param[in]    n         max number of grid elements is n^2; typ. n = 3
2697
 * \param[in]    w         width of image to be subdivided
2698
 * \param[in]    h         height of image to be subdivided
2699
 * \param[out]   pnx       number of grid elements in x direction
2700
 * \param[out]   pny       number of grid elements in y direction
2701
 * \param[in]    debug     1 for debug output to stderr
2702
 * \return  0 if OK, 1 on error
2703
 *
2704
 * <pre>
2705
 * Notes:
2706
 *      (1) This determines the number of subdivisions to be used on
2707
 *          the image in each direction.  A histogram will be built
2708
 *          for each subimage.
2709
 *      (2) The parameter %n specifies the "side" of the n x n grid
2710
 *          of subimages.  If the subimages have an aspect ratio larger
2711
 *          than 2, the grid will change, using n^2 as a maximum for
2712
 *          the number of subimages.  For example, if n == 3, but the
2713
 *          image is 600 x 200 pixels, a 3x3 grid would have subimages
2714
 *          of 200 x 67 pixels, which is more than 2:1, so we change
2715
 *          to a 4x2 grid where each subimage has 150 x 100 pixels.
2716
 * </pre>
2717
 */
2718
static l_ok
2719
findHistoGridDimensions(l_int32   n,
2720
                        l_int32   w,
2721
                        l_int32   h,
2722
                        l_int32  *pnx,
2723
                        l_int32  *pny,
2724
                        l_int32   debug)
2725
0
{
2726
0
l_int32    nx, ny, max;
2727
0
l_float32  ratio;
2728
2729
0
    ratio = (l_float32)w / (l_float32)h;
2730
0
    max = n * n;
2731
0
    nx = ny = n;
2732
0
    while (nx > 1 && ny > 1) {
2733
0
        if (ratio > 2.0) {  /* reduce ny */
2734
0
            ny--;
2735
0
            nx = max / ny;
2736
0
            if (debug)
2737
0
                lept_stderr("nx = %d, ny = %d, ratio w/h = %4.2f\n",
2738
0
                            nx, ny, ratio);
2739
0
        } else if (ratio < 0.5) {  /* reduce nx */
2740
0
            nx--;
2741
0
            ny = max / nx;
2742
0
            if (debug)
2743
0
                lept_stderr("nx = %d, ny = %d, ratio w/h = %4.2f\n",
2744
0
                            nx, ny, ratio);
2745
0
        } else {  /* we're ok */
2746
0
            if (debug)
2747
0
                lept_stderr("nx = %d, ny = %d, ratio w/h = %4.2f\n",
2748
0
                            nx, ny, ratio);
2749
0
            break;
2750
0
        }
2751
0
        ratio = (l_float32)(ny * w) / (l_float32)(nx * h);
2752
0
    }
2753
0
    *pnx = nx;
2754
0
    *pny = ny;
2755
0
    return 0;
2756
0
}
2757
2758
2759
/*!
2760
 * \brief   compareTilesByHisto()
2761
 *
2762
 * \param[in]    naa1, naa2      each is a set of 256 entry histograms
2763
 * \param[in]    minratio        requiring image sizes be compatible; < 1.0
2764
 * \param[in]    w1, h1, w2, h2  image sizes from which histograms were made
2765
 * \param[out]   pscore          similarity score of histograms
2766
 * \param[in]    pixadebug       [optional] use only for debug output
2767
 * \return  0 if OK, 1 on error
2768
 *
2769
 * <pre>
2770
 * Notes:
2771
 *      (1) naa1 and naa2 must be generated using pixGenPhotoHistos(),
2772
 *          using the same tile sizes.
2773
 *      (2) The image dimensions must be similar.  The score is 0.0
2774
 *          if the ratio of widths and heights (smallest / largest)
2775
 *          exceeds a threshold %minratio, which must be between
2776
 *          0.5 and 1.0.  If set at 1.0, both images must be exactly
2777
 *          the same size.  A typical value for %minratio is 0.9.
2778
 *      (3) The input pixadebug is null unless debug output is requested.
2779
 * </pre>
2780
 */
2781
l_ok
2782
compareTilesByHisto(NUMAA      *naa1,
2783
                    NUMAA      *naa2,
2784
                    l_float32   minratio,
2785
                    l_int32     w1,
2786
                    l_int32     h1,
2787
                    l_int32     w2,
2788
                    l_int32     h2,
2789
                    l_float32  *pscore,
2790
                    PIXA       *pixadebug)
2791
0
{
2792
0
char       buf1[128], buf2[128];
2793
0
l_int32    i, n;
2794
0
l_float32  wratio, hratio, score, minscore, dist;
2795
0
L_BMF     *bmf;
2796
0
NUMA      *na1, *na2, *nadist, *nascore;
2797
2798
0
    if (!pscore)
2799
0
        return ERROR_INT("&score not defined", __func__, 1);
2800
0
    *pscore = 0.0;
2801
0
    if (!naa1 || !naa2)
2802
0
        return ERROR_INT("naa1 and naa2 not both defined", __func__, 1);
2803
2804
        /* Filter for different sizes */
2805
0
    wratio = (w1 < w2) ? (l_float32)w1 / (l_float32)w2 :
2806
0
             (l_float32)w2 / (l_float32)w1;
2807
0
    hratio = (h1 < h2) ? (l_float32)h1 / (l_float32)h2 :
2808
0
             (l_float32)h2 / (l_float32)h1;
2809
0
    if (wratio < minratio || hratio < minratio) {
2810
0
        if (pixadebug)
2811
0
            L_INFO("Sizes differ: wratio = %f, hratio = %f\n",
2812
0
                   __func__, wratio, hratio);
2813
0
        return 0;
2814
0
    }
2815
0
    n = numaaGetCount(naa1);
2816
0
    if (n != numaaGetCount(naa2)) {  /* due to differing w/h ratio */
2817
0
        L_INFO("naa1 and naa2 sizes are different\n", __func__);
2818
0
        return 0;
2819
0
    }
2820
2821
0
    if (pixadebug) {
2822
0
        lept_rmdir("lept/comptile");
2823
0
        lept_mkdir("lept/comptile");
2824
0
    }
2825
2826
2827
        /* Evaluate histograms in each tile.  Remove white before
2828
         * computing EMD, because there are may be a lot of white
2829
         * pixels due to padding, and we don't want to include them.
2830
         * This also makes the debug histo plots more informative. */
2831
0
    minscore = 1.0;
2832
0
    nadist = numaCreate(n);
2833
0
    nascore = numaCreate(n);
2834
0
    bmf = (pixadebug) ? bmfCreate(NULL, 6) : NULL;
2835
0
    for (i = 0; i < n; i++) {
2836
0
        na1 = numaaGetNuma(naa1, i, L_CLONE);
2837
0
        na2 = numaaGetNuma(naa2, i, L_CLONE);
2838
0
        numaSetValue(na1, 255, 0.0);
2839
0
        numaSetValue(na2, 255, 0.0);
2840
2841
            /* To compare histograms, use the normalized earthmover distance.
2842
             * Further normalize to get the EM distance as a fraction of the
2843
             * maximum distance in the histogram (255).  Finally, scale this
2844
             * up by 10.0, and subtract from 1.0 to get a similarity score. */
2845
0
        numaEarthMoverDistance(na1, na2, &dist);
2846
0
        score = L_MAX(0.0, 1.0 - 10.0 * (dist / 255.));
2847
0
        numaAddNumber(nadist, dist);
2848
0
        numaAddNumber(nascore, score);
2849
0
        minscore = L_MIN(minscore, score);
2850
0
        if (pixadebug) {
2851
0
            snprintf(buf1, sizeof(buf1), "/tmp/lept/comptile/plot.%d", i);
2852
0
            gplotSimple2(na1, na2, GPLOT_PNG, buf1, "Histos");
2853
0
        }
2854
0
        numaDestroy(&na1);
2855
0
        numaDestroy(&na2);
2856
0
    }
2857
0
    *pscore = minscore;
2858
2859
0
    if (pixadebug) {
2860
0
        for (i = 0; i < n; i++) {
2861
0
            PIX  *pix1, *pix2;
2862
0
            snprintf(buf1, sizeof(buf1), "/tmp/lept/comptile/plot.%d.png", i);
2863
0
            pix1 = pixRead(buf1);
2864
0
            numaGetFValue(nadist, i, &dist);
2865
0
            numaGetFValue(nascore, i, &score);
2866
0
            snprintf(buf2, sizeof(buf2),
2867
0
                 "Image %d\ndist = %5.3f, score = %5.3f", i, dist, score);
2868
0
            pix2 = pixAddTextlines(pix1, bmf, buf2, 0x0000ff00, L_ADD_BELOW);
2869
0
            pixaAddPix(pixadebug, pix2, L_INSERT);
2870
0
            pixDestroy(&pix1);
2871
0
        }
2872
0
        lept_stderr("Writing to /tmp/lept/comptile/comparegray.pdf\n");
2873
0
        pixaConvertToPdf(pixadebug, 300, 1.0, L_FLATE_ENCODE, 0, NULL,
2874
0
                         "/tmp/lept/comptile/comparegray.pdf");
2875
0
        numaWriteDebug("/tmp/lept/comptile/scores.na", nascore);
2876
0
        numaWriteDebug("/tmp/lept/comptile/dists.na", nadist);
2877
0
    }
2878
2879
0
    bmfDestroy(&bmf);
2880
0
    numaDestroy(&nadist);
2881
0
    numaDestroy(&nascore);
2882
0
    return 0;
2883
0
}
2884
2885
2886
/*!
2887
 * \brief   pixCompareGrayByHisto()
2888
 *
2889
 * \param[in]    pix1, pix2  any depth; colormap OK
2890
 * \param[in]    box1, box2  [optional] region selected from each; can be null
2891
 * \param[in]    minratio    requiring sizes be compatible; < 1.0
2892
 * \param[in]    maxgray     max value to keep in histo; >= 200, 255 to keep all
2893
 * \param[in]    factor      subsampling factor; >= 1
2894
 * \param[in]    n           in range {1, ... 7}. n^2 is the maximum number
2895
 *                           of subregions for histograms; typ. n = 3.
2896
 * \param[out]   pscore      similarity score of histograms
2897
 * \param[in]    debugflag   1 for debug output; 0 for no debugging
2898
 * \return  0 if OK, 1 on error
2899
 *
2900
 * <pre>
2901
 * Notes:
2902
 *      (1) This function compares two grayscale photo regions.  It can
2903
 *          do it with a single histogram from each region, or with a
2904
 *          set of spatially aligned histograms.  For both cases,
2905
 *          align the regions using the centroid of the inverse image,
2906
 *          and crop to the smallest of the two.
2907
 *      (2) The parameter %n specifies the "side" of an n x n grid
2908
 *          of subimages.  If the subimages have an aspect ratio larger
2909
 *          than 2, the grid will change, using n^2 as a maximum for
2910
 *          the number of subimages.  For example, if n == 3, but the
2911
 *          image is 600 x 200 pixels, a 3x3 grid would have subimages
2912
 *          of 200 x 67 pixels, which is more than 2:1, so we change
2913
 *          to a 4x2 grid where each subimage has 150 x 100 pixels.
2914
 *      (3) An initial filter gives %score = 0 if the ratio of widths
2915
 *          and heights (smallest / largest) does not exceed a
2916
 *          threshold %minratio.  This must be between 0.5 and 1.0.
2917
 *          If set at 1.0, both images must be exactly the same size.
2918
 *          A typical value for %minratio is 0.9.
2919
 *      (4) The lightest values in the histogram can be disregarded.
2920
 *          Set %maxgray to the lightest value to be kept.  For example,
2921
 *          to eliminate white (255), set %maxgray = 254.  %maxgray must
2922
 *          be >= 200.
2923
 *      (5) For an efficient representation of the histogram, normalize
2924
 *          using a multiplicative factor so that the number in the
2925
 *          maximum bucket is 255.  It then takes 256 bytes to store.
2926
 *      (6) When comparing the histograms of two regions:
2927
 *          ~ Use %maxgray = 254 to ignore the white pixels, the number
2928
 *            of which may be sensitive to the crop region if the pixels
2929
 *            outside that region are white.
2930
 *          ~ Use the Earth Mover distance (EMD), with the histograms
2931
 *            normalized so that the sum over bins is the same.
2932
 *            Further normalize by dividing by 255, so that the result
2933
 *            is in [0.0 ... 1.0].
2934
 *      (7) Get a similarity score S = 1.0 - k * D, where
2935
 *            k is a constant, say in the range 5-10
2936
 *            D = normalized EMD
2937
 *          and for multiple tiles, take the Min(S) to be the final score.
2938
 *          Using aligned tiles gives protection against accidental
2939
 *          similarity of the overall grayscale histograms.
2940
 *          A small number of aligned tiles works well.
2941
 *      (8) With debug on, you get a pdf that shows, for each tile,
2942
 *          the images, histograms and score.
2943
 *      (9) When to use:
2944
 *          (a) Because this function should not be used on text or
2945
 *              line graphics, which can give false positive results
2946
 *              (i.e., high scores for different images), the input
2947
 *              images should be filtered.
2948
 *          (b) To filter, first use pixDecideIfText().  If that function
2949
 *              says the image is text, do not use it.  If the function
2950
 *              says it is not text, it still may be line graphics, and
2951
 *              in that case, use:
2952
 *                 pixGetGrayHistogramTiled()
2953
 *                 grayInterHistogramStats()
2954
 *              to determine whether it is photo or line graphics.
2955
 * </pre>
2956
 */
2957
l_ok
2958
pixCompareGrayByHisto(PIX        *pix1,
2959
                      PIX        *pix2,
2960
                      BOX        *box1,
2961
                      BOX        *box2,
2962
                      l_float32   minratio,
2963
                      l_int32     maxgray,
2964
                      l_int32     factor,
2965
                      l_int32     n,
2966
                      l_float32  *pscore,
2967
                      l_int32     debugflag)
2968
0
{
2969
0
l_int32    w1, h1, w2, h2;
2970
0
l_float32  wratio, hratio;
2971
0
BOX       *box3, *box4;
2972
0
PIX       *pix3, *pix4, *pix5, *pix6, *pix7, *pix8;
2973
0
PIXA      *pixa;
2974
2975
0
    if (!pscore)
2976
0
        return ERROR_INT("&score not defined", __func__, 1);
2977
0
    *pscore = 0.0;
2978
0
    if (!pix1 || !pix2)
2979
0
        return ERROR_INT("pix1 and pix2 not both defined", __func__, 1);
2980
0
    if (minratio < 0.5 || minratio > 1.0)
2981
0
        return ERROR_INT("minratio not in [0.5 ... 1.0]", __func__, 1);
2982
0
    if (maxgray < 200)
2983
0
        return ERROR_INT("invalid maxgray; should be >= 200", __func__, 1);
2984
0
    maxgray = L_MIN(255, maxgray);
2985
0
    if (factor < 1)
2986
0
        return ERROR_INT("subsampling factor must be >= 1", __func__, 1);
2987
0
    if (n < 1 || n > 7) {
2988
0
        L_WARNING("n = %d is invalid; setting to 4\n", __func__, n);
2989
0
        n = 4;
2990
0
    }
2991
2992
0
    if (debugflag)
2993
0
        lept_mkdir("lept/comp");
2994
2995
        /* Initial filter by size */
2996
0
    if (box1)
2997
0
        boxGetGeometry(box1, NULL, NULL, &w1, &h1);
2998
0
    else
2999
0
        pixGetDimensions(pix1, &w1, &h1, NULL);
3000
0
    if (box2)
3001
0
        boxGetGeometry(box2, NULL, NULL, &w2, &h2);
3002
0
    else
3003
0
        pixGetDimensions(pix1, &w2, &h2, NULL);
3004
0
    wratio = (w1 < w2) ? (l_float32)w1 / (l_float32)w2 :
3005
0
             (l_float32)w2 / (l_float32)w1;
3006
0
    hratio = (h1 < h2) ? (l_float32)h1 / (l_float32)h2 :
3007
0
             (l_float32)h2 / (l_float32)h1;
3008
0
    if (wratio < minratio || hratio < minratio)
3009
0
        return 0;
3010
3011
        /* Initial crop, if necessary */
3012
0
    if (box1)
3013
0
        pix3 = pixClipRectangle(pix1, box1, NULL);
3014
0
    else
3015
0
        pix3 = pixClone(pix1);
3016
0
    if (box2)
3017
0
        pix4 = pixClipRectangle(pix2, box2, NULL);
3018
0
    else
3019
0
        pix4 = pixClone(pix2);
3020
3021
        /* Convert to 8 bpp, align centroids and do maximal crop */
3022
0
    pix5 = pixConvertTo8(pix3, FALSE);
3023
0
    pix6 = pixConvertTo8(pix4, FALSE);
3024
0
    pixCropAlignedToCentroid(pix5, pix6, factor, &box3, &box4);
3025
0
    pix7 = pixClipRectangle(pix5, box3, NULL);
3026
0
    pix8 = pixClipRectangle(pix6, box4, NULL);
3027
0
    pixa = (debugflag) ? pixaCreate(0) : NULL;
3028
0
    if (debugflag) {
3029
0
        PIX     *pix9, *pix10, *pix11, *pix12, *pix13;
3030
0
        PIXA    *pixa2;
3031
0
        pix9 = pixConvertTo32(pix5);
3032
0
        pix10 = pixConvertTo32(pix6);
3033
0
        pixRenderBoxArb(pix9, box3, 2, 255, 0, 0);
3034
0
        pixRenderBoxArb(pix10, box4, 2, 255, 0, 0);
3035
0
        pix11 = pixScaleToSize(pix9, 400, 0);
3036
0
        pix12 = pixScaleToSize(pix10, 400, 0);
3037
0
        pixa2 = pixaCreate(2);
3038
0
        pixaAddPix(pixa2, pix11, L_INSERT);
3039
0
        pixaAddPix(pixa2, pix12, L_INSERT);
3040
0
        pix13 = pixaDisplayTiledInRows(pixa2, 32, 1000, 1.0, 0, 50, 0);
3041
0
        pixaAddPix(pixa, pix13, L_INSERT);
3042
0
        pixDestroy(&pix9);
3043
0
        pixDestroy(&pix10);
3044
0
        pixaDestroy(&pixa2);
3045
0
    }
3046
0
    pixDestroy(&pix3);
3047
0
    pixDestroy(&pix4);
3048
0
    pixDestroy(&pix5);
3049
0
    pixDestroy(&pix6);
3050
0
    boxDestroy(&box3);
3051
0
    boxDestroy(&box4);
3052
3053
        /* Tile and compare histograms */
3054
0
    pixCompareTilesByHisto(pix7, pix8, maxgray, factor, n, pscore, pixa);
3055
0
    pixaDestroy(&pixa);
3056
0
    pixDestroy(&pix7);
3057
0
    pixDestroy(&pix8);
3058
0
    return 0;
3059
0
}
3060
3061
3062
/*!
3063
 * \brief   pixCompareTilesByHisto()
3064
 *
3065
 * \param[in]    pix1, pix2     8 bpp
3066
 * \param[in]    maxgray        max value to keep in histo; 255 to keep all
3067
 * \param[in]    factor         subsampling factor; >= 1
3068
 * \param[in]    n              see pixCompareGrayByHisto()
3069
 * \param[out]   pscore         similarity score of histograms
3070
 * \param[in]    pixadebug      [optional] use only for debug output
3071
 * \return  0 if OK, 1 on error
3072
 *
3073
 * <pre>
3074
 * Notes:
3075
 *      (1) This static function is only called from pixCompareGrayByHisto().
3076
 *          The input images have been converted to 8 bpp if necessary,
3077
 *          aligned and cropped.
3078
 *      (2) The input pixadebug is null unless debug output is requested.
3079
 *      (3) See pixCompareGrayByHisto() for details.
3080
 * </pre>
3081
 */
3082
static l_ok
3083
pixCompareTilesByHisto(PIX        *pix1,
3084
                       PIX        *pix2,
3085
                       l_int32     maxgray,
3086
                       l_int32     factor,
3087
                       l_int32     n,
3088
                       l_float32  *pscore,
3089
                       PIXA       *pixadebug)
3090
0
{
3091
0
char       buf[64];
3092
0
l_int32    w, h, i, j, nx, ny, ngr;
3093
0
l_float32  score, minscore, maxval1, maxval2, dist;
3094
0
L_BMF     *bmf;
3095
0
NUMA      *na1, *na2, *na3, *na4, *na5, *na6, *na7;
3096
0
PIX       *pix3, *pix4;
3097
0
PIXA      *pixa1, *pixa2;
3098
3099
0
    if (!pscore)
3100
0
        return ERROR_INT("&score not defined", __func__, 1);
3101
0
    *pscore = 0.0;
3102
0
    if (!pix1 || !pix2)
3103
0
        return ERROR_INT("pix1 and pix2 not both defined", __func__, 1);
3104
3105
        /* Determine grid from n */
3106
0
    pixGetDimensions(pix1, &w, &h, NULL);
3107
0
    findHistoGridDimensions(n, w, h, &nx, &ny, 1);
3108
0
    ngr = nx * ny;
3109
3110
        /* Evaluate histograms in each tile */
3111
0
    pixa1 = pixaSplitPix(pix1, nx, ny, 0, 0);
3112
0
    pixa2 = pixaSplitPix(pix2, nx, ny, 0, 0);
3113
0
    na7 = (pixadebug) ? numaCreate(ngr) : NULL;
3114
0
    bmf = (pixadebug) ? bmfCreate(NULL, 6) : NULL;
3115
0
    minscore = 1.0;
3116
0
    for (i = 0; i < ngr; i++) {
3117
0
        pix3 = pixaGetPix(pixa1, i, L_CLONE);
3118
0
        pix4 = pixaGetPix(pixa2, i, L_CLONE);
3119
3120
            /* Get histograms, set white count to 0, normalize max to 255 */
3121
0
        na1 = pixGetGrayHistogram(pix3, factor);
3122
0
        na2 = pixGetGrayHistogram(pix4, factor);
3123
0
        if (maxgray < 255) {
3124
0
            for (j = maxgray + 1; j <= 255; j++) {
3125
0
                numaSetValue(na1, j, 0);
3126
0
                numaSetValue(na2, j, 0);
3127
0
            }
3128
0
        }
3129
0
        na3 = numaWindowedMean(na1, 5);
3130
0
        na4 = numaWindowedMean(na2, 5);
3131
0
        numaGetMax(na3, &maxval1, NULL);
3132
0
        numaGetMax(na4, &maxval2, NULL);
3133
0
        na5 = numaTransform(na3, 0, 255.0 / maxval1);
3134
0
        na6 = numaTransform(na4, 0, 255.0 / maxval2);
3135
0
        if (pixadebug) {
3136
0
            gplotSimple2(na5, na6, GPLOT_PNG, "/tmp/lept/comp/plot1", "Histos");
3137
0
        }
3138
3139
            /* To compare histograms, use the normalized earthmover distance.
3140
             * Further normalize to get the EM distance as a fraction of the
3141
             * maximum distance in the histogram (255).  Finally, scale this
3142
             * up by 10.0, and subtract from 1.0 to get a similarity score. */
3143
0
        numaEarthMoverDistance(na5, na6, &dist);
3144
0
        score = L_MAX(0.0, 1.0 - 8.0 * (dist / 255.));
3145
0
        if (pixadebug) numaAddNumber(na7, score);
3146
0
        minscore = L_MIN(minscore, score);
3147
0
        if (pixadebug) {
3148
0
            PIX     *pix5, *pix6, *pix7, *pix8, *pix9, *pix10;
3149
0
            PIXA    *pixa3;
3150
0
            l_int32  w, h, wscale;
3151
0
            pixa3 = pixaCreate(3);
3152
0
            pixGetDimensions(pix3, &w, &h, NULL);
3153
0
            wscale = (w > h) ? 700 : 400;
3154
0
            pix5 = pixScaleToSize(pix3, wscale, 0);
3155
0
            pix6 = pixScaleToSize(pix4, wscale, 0);
3156
0
            pixaAddPix(pixa3, pix5, L_INSERT);
3157
0
            pixaAddPix(pixa3, pix6, L_INSERT);
3158
0
            pix7 = pixRead("/tmp/lept/comp/plot1.png");
3159
0
            pix8 = pixScaleToSize(pix7, 700, 0);
3160
0
            snprintf(buf, sizeof(buf), "%5.3f", score);
3161
0
            pix9 = pixAddTextlines(pix8, bmf, buf, 0x0000ff00, L_ADD_RIGHT);
3162
0
            pixaAddPix(pixa3, pix9, L_INSERT);
3163
0
            pix10 = pixaDisplayTiledInRows(pixa3, 32, 1000, 1.0, 0, 50, 0);
3164
0
            pixaAddPix(pixadebug, pix10, L_INSERT);
3165
0
            pixDestroy(&pix7);
3166
0
            pixDestroy(&pix8);
3167
0
            pixaDestroy(&pixa3);
3168
0
        }
3169
0
        numaDestroy(&na1);
3170
0
        numaDestroy(&na2);
3171
0
        numaDestroy(&na3);
3172
0
        numaDestroy(&na4);
3173
0
        numaDestroy(&na5);
3174
0
        numaDestroy(&na6);
3175
0
        pixDestroy(&pix3);
3176
0
        pixDestroy(&pix4);
3177
0
    }
3178
0
    *pscore = minscore;
3179
3180
0
    if (pixadebug) {
3181
0
        pixaConvertToPdf(pixadebug, 300, 1.0, L_FLATE_ENCODE, 0, NULL,
3182
0
                         "/tmp/lept/comp/comparegray.pdf");
3183
0
        numaWriteDebug("/tmp/lept/comp/tilescores.na", na7);
3184
0
    }
3185
3186
0
    bmfDestroy(&bmf);
3187
0
    numaDestroy(&na7);
3188
0
    pixaDestroy(&pixa1);
3189
0
    pixaDestroy(&pixa2);
3190
0
    return 0;
3191
0
}
3192
3193
3194
/*!
3195
 * \brief   pixCropAlignedToCentroid()
3196
 *
3197
 * \param[in]    pix1, pix2   any depth; colormap OK
3198
 * \param[in]    factor       subsampling; >= 1
3199
 * \param[out]   pbox1        crop box for pix1
3200
 * \param[out]   pbox2        crop box for pix2
3201
 * \return  0 if OK, 1 on error
3202
 *
3203
 * <pre>
3204
 * Notes:
3205
 *      (1) This finds the maximum crop boxes for two 8 bpp images when
3206
 *          their centroids of their photometric inverses are aligned.
3207
 *          Black pixels have weight 255; white pixels have weight 0.
3208
 * </pre>
3209
 */
3210
l_ok
3211
pixCropAlignedToCentroid(PIX     *pix1,
3212
                         PIX     *pix2,
3213
                         l_int32  factor,
3214
                         BOX    **pbox1,
3215
                         BOX    **pbox2)
3216
0
{
3217
0
l_float32  cx1, cy1, cx2, cy2;
3218
0
l_int32    w1, h1, w2, h2, icx1, icy1, icx2, icy2;
3219
0
l_int32    xm, xm1, xm2, xp, xp1, xp2, ym, ym1, ym2, yp, yp1, yp2;
3220
0
PIX       *pix3, *pix4;
3221
3222
0
    if (pbox1) *pbox1 = NULL;
3223
0
    if (pbox2) *pbox2 = NULL;
3224
0
    if (!pix1 || !pix2)
3225
0
        return ERROR_INT("pix1 and pix2 not both defined", __func__, 1);
3226
0
    if (factor < 1)
3227
0
        return ERROR_INT("subsampling factor must be >= 1", __func__, 1);
3228
0
    if (!pbox1 || !pbox2)
3229
0
        return ERROR_INT("&box1 and &box2 not both defined", __func__, 1);
3230
3231
0
    pix3 = pixConvertTo8(pix1, FALSE);
3232
0
    pix4 = pixConvertTo8(pix2, FALSE);
3233
0
    pixCentroid8(pix3, factor, &cx1, &cy1);
3234
0
    pixCentroid8(pix4, factor, &cx2, &cy2);
3235
0
    pixGetDimensions(pix3, &w1, &h1, NULL);
3236
0
    pixGetDimensions(pix4, &w2, &h2, NULL);
3237
0
    pixDestroy(&pix3);
3238
0
    pixDestroy(&pix4);
3239
3240
0
    icx1 = (l_int32)(cx1 + 0.5);
3241
0
    icy1 = (l_int32)(cy1 + 0.5);
3242
0
    icx2 = (l_int32)(cx2 + 0.5);
3243
0
    icy2 = (l_int32)(cy2 + 0.5);
3244
0
    xm = L_MIN(icx1, icx2);
3245
0
    xm1 = icx1 - xm;
3246
0
    xm2 = icx2 - xm;
3247
0
    xp = L_MIN(w1 - icx1, w2 - icx2);  /* one pixel beyond to the right */
3248
0
    xp1 = icx1 + xp;
3249
0
    xp2 = icx2 + xp;
3250
0
    ym = L_MIN(icy1, icy2);
3251
0
    ym1 = icy1 - ym;
3252
0
    ym2 = icy2 - ym;
3253
0
    yp = L_MIN(h1 - icy1, h2 - icy2);  /* one pixel below the bottom */
3254
0
    yp1 = icy1 + yp;
3255
0
    yp2 = icy2 + yp;
3256
0
    *pbox1 = boxCreate(xm1, ym1, xp1 - xm1, yp1 - ym1);
3257
0
    *pbox2 = boxCreate(xm2, ym2, xp2 - xm2, yp2 - ym2);
3258
0
    return 0;
3259
0
}
3260
3261
3262
/*!
3263
 * \brief   l_compressGrayHistograms()
3264
 *
3265
 * \param[in]    naa     set of 256-entry histograms
3266
 * \param[in]    w, h    size of image
3267
 * \param[out]   psize   size of byte array
3268
 * \return  0 if OK, 1 on error
3269
 *
3270
 * <pre>
3271
 * Notes:
3272
 *      (1) This first writes w and h to the byte array as 4 byte ints.
3273
 *      (2) Then it normalizes each histogram to a max value of 255,
3274
 *          and saves each value as a byte.  If there are
3275
 *          N histograms, the output bytearray has 8 + 256 * N bytes.
3276
 *      (3) Further compression of the array with zlib yields only about
3277
 *          a 25% decrease in size, so we don't bother.  If size reduction
3278
 *          were important, a lossy transform using a 1-dimensional DCT
3279
 *          would be effective, because we don't care about the fine
3280
 *          details of these histograms.
3281
 * </pre>
3282
 */
3283
l_uint8 *
3284
l_compressGrayHistograms(NUMAA   *naa,
3285
                         l_int32  w,
3286
                         l_int32  h,
3287
                         size_t  *psize)
3288
0
{
3289
0
l_uint8   *bytea;
3290
0
l_int32    i, j, n, nn, ival;
3291
0
l_float32  maxval;
3292
0
NUMA      *na1, *na2;
3293
3294
0
    if (!psize)
3295
0
        return (l_uint8 *)ERROR_PTR("&size not defined", __func__, NULL);
3296
0
    *psize = 0;
3297
0
    if (!naa)
3298
0
        return (l_uint8 *)ERROR_PTR("naa not defined", __func__, NULL);
3299
0
    n = numaaGetCount(naa);
3300
0
    for (i = 0; i < n; i++) {
3301
0
        nn = numaaGetNumaCount(naa, i);
3302
0
        if (nn != 256) {
3303
0
            L_ERROR("%d numbers in numa[%d]\n", __func__, nn, i);
3304
0
            return NULL;
3305
0
        }
3306
0
    }
3307
3308
0
    if ((bytea = (l_uint8 *)LEPT_CALLOC(8 + 256 * n, sizeof(l_uint8))) == NULL)
3309
0
        return (l_uint8 *)ERROR_PTR("bytea not made", __func__, NULL);
3310
0
    *psize = 8 + 256 * n;
3311
0
    l_setDataFourBytes(bytea, 0, w);
3312
0
    l_setDataFourBytes(bytea, 1, h);
3313
0
    for (i = 0; i < n; i++) {
3314
0
        na1 = numaaGetNuma(naa, i, L_COPY);
3315
0
        numaGetMax(na1, &maxval, NULL);
3316
0
        na2 = numaTransform(na1, 0, 255.0 / maxval);
3317
0
        for (j = 0; j < 256; j++) {
3318
0
            numaGetIValue(na2, j, &ival);
3319
0
            bytea[8 + 256 * i + j] = ival;
3320
0
        }
3321
0
        numaDestroy(&na1);
3322
0
        numaDestroy(&na2);
3323
0
    }
3324
3325
0
    return bytea;
3326
0
}
3327
3328
3329
/*!
3330
 * \brief   l_uncompressGrayHistograms()
3331
 *
3332
 * \param[in]    bytea    byte array of size 8 + 256 * N, N an integer
3333
 * \param[in]    size     size of byte array
3334
 * \param[out]   pw       width of the image that generated the histograms
3335
 * \param[out]   ph       height of the image
3336
 * \return  numaa     representing N histograms, each with 256 bins,
3337
 *                    or NULL on error.
3338
 *
3339
 * <pre>
3340
 * Notes:
3341
 *      (1) The first 8 bytes are read as two 32-bit ints.
3342
 *      (2) Then this constructs a numaa representing some number of
3343
 *          gray histograms that are normalized such that the max value
3344
 *          in each histogram is 255.  The data is stored as a byte
3345
 *          array, with 256 bytes holding the data for each histogram.
3346
 *          Each gray histogram was computed from a tile of a grayscale image.
3347
 * </pre>
3348
 */
3349
NUMAA *
3350
l_uncompressGrayHistograms(l_uint8  *bytea,
3351
                           size_t    size,
3352
                           l_int32  *pw,
3353
                           l_int32  *ph)
3354
0
{
3355
0
l_int32  i, j, n;
3356
0
NUMA    *na;
3357
0
NUMAA   *naa;
3358
3359
0
    if (pw) *pw = 0;
3360
0
    if (ph) *ph = 0;
3361
0
    if (!pw || !ph)
3362
0
        return (NUMAA *)ERROR_PTR("&w and &h not both defined", __func__, NULL);
3363
0
    if (!bytea)
3364
0
        return (NUMAA *)ERROR_PTR("bytea not defined", __func__, NULL);
3365
0
    n = (size - 8) / 256;
3366
0
    if ((size - 8) % 256 != 0)
3367
0
        return (NUMAA *)ERROR_PTR("bytea size is invalid", __func__, NULL);
3368
3369
0
    *pw = l_getDataFourBytes(bytea, 0);
3370
0
    *ph = l_getDataFourBytes(bytea, 1);
3371
0
    naa = numaaCreate(n);
3372
0
    for (i = 0; i < n; i++) {
3373
0
        na = numaCreate(256);
3374
0
        for (j = 0; j < 256; j++)
3375
0
            numaAddNumber(na, bytea[8 + 256 * i + j]);
3376
0
        numaaAddNuma(naa, na, L_INSERT);
3377
0
    }
3378
3379
0
    return naa;
3380
0
}
3381
3382
3383
/*------------------------------------------------------------------*
3384
 *             Translated images at the same resolution             *
3385
 *------------------------------------------------------------------*/
3386
/*!
3387
 * \brief   pixCompareWithTranslation()
3388
 *
3389
 * \param[in]    pix1, pix2    any depth; colormap OK
3390
 * \param[in]    thresh        threshold for converting to 1 bpp
3391
 * \param[out]   pdelx         x translation on pix2 to align with pix1
3392
 * \param[out]   pdely         y translation on pix2 to align with pix1
3393
 * \param[out]   pscore        correlation score at best alignment
3394
 * \param[in]    debugflag     1 for debug output; 0 for no debugging
3395
 * \return  0 if OK, 1 on error
3396
 *
3397
 * <pre>
3398
 * Notes:
3399
 *      (1) This does a coarse-to-fine search for best translational
3400
 *          alignment of two images, measured by a scoring function
3401
 *          that is the correlation between the fg pixels.
3402
 *      (2) The threshold is used if the images aren't 1 bpp.
3403
 *      (3) With debug on, you get a pdf that shows, as a grayscale
3404
 *          image, the score as a function of shift from the initial
3405
 *          estimate, for each of the four levels.  The shift is 0 at
3406
 *          the center of the image.
3407
 *      (4) With debug on, you also get a pdf that shows the
3408
 *          difference at the best alignment between the two images,
3409
 *          at each of the four levels.  The red and green pixels
3410
 *          show locations where one image has a fg pixel and the
3411
 *          other doesn't.  The black pixels are where both images
3412
 *          have fg pixels, and white pixels are where neither image
3413
 *          has fg pixels.
3414
 * </pre>
3415
 */
3416
l_ok
3417
pixCompareWithTranslation(PIX        *pix1,
3418
                          PIX        *pix2,
3419
                          l_int32     thresh,
3420
                          l_int32    *pdelx,
3421
                          l_int32    *pdely,
3422
                          l_float32  *pscore,
3423
                          l_int32     debugflag)
3424
0
{
3425
0
l_uint8   *subtab;
3426
0
l_int32    i, level, area1, area2, delx, dely;
3427
0
l_int32    etransx, etransy, maxshift, dbint;
3428
0
l_int32   *stab, *ctab;
3429
0
l_float32  cx1, cx2, cy1, cy2, score;
3430
0
PIX       *pixb1, *pixb2, *pixt1, *pixt2, *pixt3, *pixt4;
3431
0
PIXA      *pixa1, *pixa2, *pixadb = NULL;
3432
3433
0
    if (pdelx) *pdelx = 0;
3434
0
    if (pdely) *pdely = 0;
3435
0
    if (pscore) *pscore = 0.0;
3436
0
    if (!pdelx || !pdely)
3437
0
        return ERROR_INT("&delx and &dely not defined", __func__, 1);
3438
0
    if (!pscore)
3439
0
        return ERROR_INT("&score not defined", __func__, 1);
3440
0
    if (!pix1)
3441
0
        return ERROR_INT("pix1 not defined", __func__, 1);
3442
0
    if (!pix2)
3443
0
        return ERROR_INT("pix2 not defined", __func__, 1);
3444
3445
        /* Make tables */
3446
0
    subtab = makeSubsampleTab2x();
3447
0
    stab = makePixelSumTab8();
3448
0
    ctab = makePixelCentroidTab8();
3449
3450
        /* Binarize each image */
3451
0
    pixb1 = pixConvertTo1(pix1, thresh);
3452
0
    pixb2 = pixConvertTo1(pix2, thresh);
3453
3454
        /* Make a cascade of 2x reduced images for each, thresholding
3455
         * with level 2 (neutral), down to 8x reduction */
3456
0
    pixa1 = pixaCreate(4);
3457
0
    pixa2 = pixaCreate(4);
3458
0
    if (debugflag)
3459
0
        pixadb = pixaCreate(4);
3460
0
    pixaAddPix(pixa1, pixb1, L_INSERT);
3461
0
    pixaAddPix(pixa2, pixb2, L_INSERT);
3462
0
    for (i = 0; i < 3; i++) {
3463
0
        pixt1 = pixReduceRankBinary2(pixb1, 2, subtab);
3464
0
        pixt2 = pixReduceRankBinary2(pixb2, 2, subtab);
3465
0
        pixaAddPix(pixa1, pixt1, L_INSERT);
3466
0
        pixaAddPix(pixa2, pixt2, L_INSERT);
3467
0
        pixb1 = pixt1;
3468
0
        pixb2 = pixt2;
3469
0
    }
3470
3471
        /* At the lowest level, use the centroids with a maxshift of 6
3472
         * to search for the best alignment.  Then at higher levels,
3473
         * use the result from the level below as the initial approximation
3474
         * for the alignment, and search with a maxshift of 2. */
3475
0
    for (level = 3; level >= 0; level--) {
3476
0
        pixt1 = pixaGetPix(pixa1, level, L_CLONE);
3477
0
        pixt2 = pixaGetPix(pixa2, level, L_CLONE);
3478
0
        pixCountPixels(pixt1, &area1, stab);
3479
0
        pixCountPixels(pixt2, &area2, stab);
3480
0
        if (level == 3) {
3481
0
            pixCentroid(pixt1, ctab, stab, &cx1, &cy1);
3482
0
            pixCentroid(pixt2, ctab, stab, &cx2, &cy2);
3483
0
            etransx = lept_roundftoi(cx1 - cx2);
3484
0
            etransy = lept_roundftoi(cy1 - cy2);
3485
0
            maxshift = 6;
3486
0
        } else {
3487
0
            etransx = 2 * delx;
3488
0
            etransy = 2 * dely;
3489
0
            maxshift = 2;
3490
0
        }
3491
0
        dbint = (debugflag) ? level + 1 : 0;
3492
0
        pixBestCorrelation(pixt1, pixt2, area1, area2, etransx, etransy,
3493
0
                           maxshift, stab, &delx, &dely, &score, dbint);
3494
0
        if (debugflag) {
3495
0
            lept_stderr("Level %d: delx = %d, dely = %d, score = %7.4f\n",
3496
0
                        level, delx, dely, score);
3497
0
            pixRasteropIP(pixt2, delx, dely, L_BRING_IN_WHITE);
3498
0
            pixt3 = pixDisplayDiffBinary(pixt1, pixt2);
3499
0
            pixt4 = pixExpandReplicate(pixt3, 8 / (1 << (3 - level)));
3500
0
            pixaAddPix(pixadb, pixt4, L_INSERT);
3501
0
            pixDestroy(&pixt3);
3502
0
        }
3503
0
        pixDestroy(&pixt1);
3504
0
        pixDestroy(&pixt2);
3505
0
    }
3506
3507
0
    if (debugflag) {
3508
0
        pixaConvertToPdf(pixadb, 300, 1.0, L_FLATE_ENCODE, 0, NULL,
3509
0
                         "/tmp/lept/comp/compare.pdf");
3510
0
        convertFilesToPdf("/tmp/lept/comp", "correl_", 30, 1.0, L_FLATE_ENCODE,
3511
0
                          0, "Correlation scores at levels 1 through 5",
3512
0
                          "/tmp/lept/comp/correl.pdf");
3513
0
        pixaDestroy(&pixadb);
3514
0
    }
3515
3516
0
    *pdelx = delx;
3517
0
    *pdely = dely;
3518
0
    *pscore = score;
3519
0
    pixaDestroy(&pixa1);
3520
0
    pixaDestroy(&pixa2);
3521
0
    LEPT_FREE(subtab);
3522
0
    LEPT_FREE(stab);
3523
0
    LEPT_FREE(ctab);
3524
0
    return 0;
3525
0
}
3526
3527
3528
/*!
3529
 * \brief   pixBestCorrelation()
3530
 *
3531
 * \param[in]    pix1      1 bpp
3532
 * \param[in]    pix2      1 bpp
3533
 * \param[in]    area1     number of on pixels in pix1
3534
 * \param[in]    area2     number of on pixels in pix2
3535
 * \param[in]    etransx   estimated x translation of pix2 to align with pix1
3536
 * \param[in]    etransy   estimated y translation of pix2 to align with pix1
3537
 * \param[in]    maxshift  max x and y shift of pix2, around the estimated
3538
 *                         alignment location, relative to pix1
3539
 * \param[in]    tab8      [optional] sum tab for ON pixels in byte; can be NULL
3540
 * \param[out]   pdelx     [optional] best x shift of pix2 relative to pix1
3541
 * \param[out]   pdely     [optional] best y shift of pix2 relative to pix1
3542
 * \param[out]   pscore    [optional] maximum score found; can be NULL
3543
 * \param[in]    debugflag   <= 0 to skip; positive to generate output.
3544
 *                           The integer is used to label the debug image.
3545
 * \return  0 if OK, 1 on error
3546
 *
3547
 * <pre>
3548
 * Notes:
3549
 *      (1) This maximizes the correlation score between two 1 bpp images,
3550
 *          by starting with an estimate of the alignment
3551
 *          (%etransx, %etransy) and computing the correlation around this.
3552
 *          It optionally returns the shift (%delx, %dely) that maximizes
3553
 *          the correlation score when pix2 is shifted by this amount
3554
 *          relative to pix1.
3555
 *      (2) Get the centroids of pix1 and pix2, using pixCentroid(),
3556
 *          to compute (%etransx, %etransy).  Get the areas using
3557
 *          pixCountPixels().
3558
 *      (3) The centroid of pix2 is shifted with respect to the centroid
3559
 *          of pix1 by all values between -maxshiftx and maxshiftx,
3560
 *          and likewise for the y shifts.  Therefore, the number of
3561
 *          correlations computed is:
3562
 *               (2 * maxshiftx + 1) * (2 * maxshifty + 1)
3563
 *          Consequently, if pix1 and pix2 are large, you should do this
3564
 *          in a coarse-to-fine sequence.  See the use of this function
3565
 *          in pixCompareWithTranslation().
3566
 * </pre>
3567
 */
3568
l_ok
3569
pixBestCorrelation(PIX        *pix1,
3570
                   PIX        *pix2,
3571
                   l_int32     area1,
3572
                   l_int32     area2,
3573
                   l_int32     etransx,
3574
                   l_int32     etransy,
3575
                   l_int32     maxshift,
3576
                   l_int32    *tab8,
3577
                   l_int32    *pdelx,
3578
                   l_int32    *pdely,
3579
                   l_float32  *pscore,
3580
                   l_int32     debugflag)
3581
0
{
3582
0
l_int32    shiftx, shifty, delx, dely;
3583
0
l_int32   *tab;
3584
0
l_float32  maxscore, score;
3585
0
FPIX      *fpix = NULL;
3586
0
PIX       *pix3, *pix4;
3587
3588
0
    if (pdelx) *pdelx = 0;
3589
0
    if (pdely) *pdely = 0;
3590
0
    if (pscore) *pscore = 0.0;
3591
0
    if (!pix1 || pixGetDepth(pix1) != 1)
3592
0
        return ERROR_INT("pix1 not defined or not 1 bpp", __func__, 1);
3593
0
    if (!pix2 || pixGetDepth(pix2) != 1)
3594
0
        return ERROR_INT("pix2 not defined or not 1 bpp", __func__, 1);
3595
0
    if (!area1 || !area2)
3596
0
        return ERROR_INT("areas must be > 0", __func__, 1);
3597
3598
0
    if (debugflag > 0)
3599
0
        fpix = fpixCreate(2 * maxshift + 1, 2 * maxshift + 1);
3600
3601
0
    if (!tab8)
3602
0
        tab = makePixelSumTab8();
3603
0
    else
3604
0
        tab = tab8;
3605
3606
        /* Search over a set of {shiftx, shifty} for the max */
3607
0
    maxscore = 0;
3608
0
    delx = etransx;
3609
0
    dely = etransy;
3610
0
    for (shifty = -maxshift; shifty <= maxshift; shifty++) {
3611
0
        for (shiftx = -maxshift; shiftx <= maxshift; shiftx++) {
3612
0
            pixCorrelationScoreShifted(pix1, pix2, area1, area2,
3613
0
                                       etransx + shiftx,
3614
0
                                       etransy + shifty, tab, &score);
3615
0
            if (debugflag > 0) {
3616
0
                fpixSetPixel(fpix, maxshift + shiftx, maxshift + shifty,
3617
0
                             1000.0 * score);
3618
/*                lept_stderr("(sx, sy) = (%d, %d): score = %6.4f\n",
3619
                              shiftx, shifty, score); */
3620
0
            }
3621
0
            if (score > maxscore) {
3622
0
                maxscore = score;
3623
0
                delx = etransx + shiftx;
3624
0
                dely = etransy + shifty;
3625
0
            }
3626
0
        }
3627
0
    }
3628
3629
0
    if (debugflag > 0) {
3630
0
        char  buf[128];
3631
0
        lept_mkdir("lept/comp");
3632
0
        pix3 = fpixDisplayMaxDynamicRange(fpix);
3633
0
        pix4 = pixExpandReplicate(pix3, 20);
3634
0
        snprintf(buf, sizeof(buf), "/tmp/lept/comp/correl_%d.png",
3635
0
                 debugflag);
3636
0
        pixWrite(buf, pix4, IFF_PNG);
3637
0
        pixDestroy(&pix3);
3638
0
        pixDestroy(&pix4);
3639
0
        fpixDestroy(&fpix);
3640
0
    }
3641
3642
0
    if (pdelx) *pdelx = delx;
3643
0
    if (pdely) *pdely = dely;
3644
0
    if (pscore) *pscore = maxscore;
3645
0
    if (!tab8) LEPT_FREE(tab);
3646
0
    return 0;
3647
0
}