Leptonica  1.77.0
Image processing and image analysis suite
correlscore.c
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  * correlscore.c
29  *
30  * These are functions for computing correlation between
31  * pairs of 1 bpp images.
32  *
33  * Optimized 2 pix correlators (for jbig2 clustering)
34  * l_int32 pixCorrelationScore()
35  * l_int32 pixCorrelationScoreThresholded()
36  *
37  * Simple 2 pix correlators
38  * l_int32 pixCorrelationScoreSimple()
39  * l_int32 pixCorrelationScoreShifted()
40  *
41  * There are other, more application-oriented functions, that
42  * compute the correlation between two binary images, taking into
43  * account small translational shifts, between two binary images.
44  * These are:
45  * compare.c: pixBestCorrelation()
46  * Uses coarse-to-fine translations of full image
47  * recogident.c: pixCorrelationBestShift()
48  * Uses small shifts between c.c. centroids.
49  */
50 
51 #include <math.h>
52 #include "allheaders.h"
53 
54 
55 /* -------------------------------------------------------------------- *
56  * Optimized 2 pix correlators (for jbig2 clustering) *
57  * -------------------------------------------------------------------- */
124 l_ok
125 pixCorrelationScore(PIX *pix1,
126  PIX *pix2,
127  l_int32 area1,
128  l_int32 area2,
129  l_float32 delx, /* x(1) - x(3) */
130  l_float32 dely, /* y(1) - y(3) */
131  l_int32 maxdiffw,
132  l_int32 maxdiffh,
133  l_int32 *tab,
134  l_float32 *pscore)
135 {
136 l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count;
137 l_int32 wpl1, wpl2, lorow, hirow, locol, hicol;
138 l_int32 x, y, pix1lskip, pix2lskip, rowwords1, rowwords2;
139 l_uint32 word1, word2, andw;
140 l_uint32 *row1, *row2;
141 
142  PROCNAME("pixCorrelationScore");
143 
144  if (!pscore)
145  return ERROR_INT("&score not defined", procName, 1);
146  *pscore = 0.0;
147  if (!pix1 || pixGetDepth(pix1) != 1)
148  return ERROR_INT("pix1 undefined or not 1 bpp", procName, 1);
149  if (!pix2 || pixGetDepth(pix2) != 1)
150  return ERROR_INT("pix2 undefined or not 1 bpp", procName, 1);
151  if (!tab)
152  return ERROR_INT("tab not defined", procName, 1);
153  if (area1 <= 0 || area2 <= 0)
154  return ERROR_INT("areas must be > 0", procName, 1);
155 
156  /* Eliminate based on size difference */
157  pixGetDimensions(pix1, &wi, &hi, NULL);
158  pixGetDimensions(pix2, &wt, &ht, NULL);
159  delw = L_ABS(wi - wt);
160  if (delw > maxdiffw)
161  return 0;
162  delh = L_ABS(hi - ht);
163  if (delh > maxdiffh)
164  return 0;
165 
166  /* Round difference to nearest integer */
167  if (delx >= 0)
168  idelx = (l_int32)(delx + 0.5);
169  else
170  idelx = (l_int32)(delx - 0.5);
171  if (dely >= 0)
172  idely = (l_int32)(dely + 0.5);
173  else
174  idely = (l_int32)(dely - 0.5);
175 
176  count = 0;
177  wpl1 = pixGetWpl(pix1);
178  wpl2 = pixGetWpl(pix2);
179  rowwords2 = wpl2;
180 
181  /* What rows of pix1 need to be considered? Only those underlying the
182  * shifted pix2. */
183  lorow = L_MAX(idely, 0);
184  hirow = L_MIN(ht + idely, hi);
185 
186  /* Get the pointer to the first row of each image that will be
187  * considered. */
188  row1 = pixGetData(pix1) + wpl1 * lorow;
189  row2 = pixGetData(pix2) + wpl2 * (lorow - idely);
190 
191  /* Similarly, figure out which columns of pix1 will be considered. */
192  locol = L_MAX(idelx, 0);
193  hicol = L_MIN(wt + idelx, wi);
194 
195  if (idelx >= 32) {
196  /* pix2 is shifted far enough to the right that pix1's first
197  * word(s) won't contribute to the count. Increment its
198  * pointer to point to the first word that will contribute,
199  * and adjust other values accordingly. */
200  pix1lskip = idelx >> 5; /* # of words to skip on left */
201  row1 += pix1lskip;
202  locol -= pix1lskip << 5;
203  hicol -= pix1lskip << 5;
204  idelx &= 31;
205  } else if (idelx <= -32) {
206  /* pix2 is shifted far enough to the left that its first word(s)
207  * won't contribute to the count. Increment its pointer
208  * to point to the first word that will contribute,
209  * and adjust other values accordingly. */
210  pix2lskip = -((idelx + 31) >> 5); /* # of words to skip on left */
211  row2 += pix2lskip;
212  rowwords2 -= pix2lskip;
213  idelx += pix2lskip << 5;
214  }
215 
216  if ((locol >= hicol) || (lorow >= hirow)) { /* there is no overlap */
217  count = 0;
218  } else {
219  /* How many words of each row of pix1 need to be considered? */
220  rowwords1 = (hicol + 31) >> 5;
221 
222  if (idelx == 0) {
223  /* There's no lateral offset; simple case. */
224  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
225  for (x = 0; x < rowwords1; x++) {
226  andw = row1[x] & row2[x];
227  count += tab[andw & 0xff] +
228  tab[(andw >> 8) & 0xff] +
229  tab[(andw >> 16) & 0xff] +
230  tab[andw >> 24];
231  }
232  }
233  } else if (idelx > 0) {
234  /* pix2 is shifted to the right. word 0 of pix1 is touched by
235  * word 0 of pix2; word 1 of pix1 is touched by word 0 and word
236  * 1 of pix2, and so on up to the last word of pix1 (word N),
237  * which is touched by words N-1 and N of pix1... if there is a
238  * word N. Handle the two cases (pix2 has N-1 words and pix2
239  * has at least N words) separately.
240  *
241  * Note: we know that pix2 has at least N-1 words (i.e.,
242  * rowwords2 >= rowwords1 - 1) by the following logic.
243  * We can pretend that idelx <= 31 because the >= 32 logic
244  * above adjusted everything appropriately. Then
245  * hicol <= wt + idelx <= wt + 31, so
246  * hicol + 31 <= wt + 62
247  * rowwords1 = (hicol + 31) >> 5 <= (wt + 62) >> 5
248  * rowwords2 == (wt + 31) >> 5, so
249  * rowwords1 <= rowwords2 + 1 */
250  if (rowwords2 < rowwords1) {
251  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
252  /* Do the first iteration so the loop can be
253  * branch-free. */
254  word1 = row1[0];
255  word2 = row2[0] >> idelx;
256  andw = word1 & word2;
257  count += tab[andw & 0xff] +
258  tab[(andw >> 8) & 0xff] +
259  tab[(andw >> 16) & 0xff] +
260  tab[andw >> 24];
261 
262  for (x = 1; x < rowwords2; x++) {
263  word1 = row1[x];
264  word2 = (row2[x] >> idelx) |
265  (row2[x - 1] << (32 - idelx));
266  andw = word1 & word2;
267  count += tab[andw & 0xff] +
268  tab[(andw >> 8) & 0xff] +
269  tab[(andw >> 16) & 0xff] +
270  tab[andw >> 24];
271  }
272 
273  /* Now the last iteration - we know that this is safe
274  * (i.e. rowwords1 >= 2) because rowwords1 > rowwords2
275  * > 0 (if it was 0, we'd have taken the "count = 0"
276  * fast-path out of here). */
277  word1 = row1[x];
278  word2 = row2[x - 1] << (32 - idelx);
279  andw = word1 & word2;
280  count += tab[andw & 0xff] +
281  tab[(andw >> 8) & 0xff] +
282  tab[(andw >> 16) & 0xff] +
283  tab[andw >> 24];
284  }
285  } else {
286  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
287  /* Do the first iteration so the loop can be
288  * branch-free. This section is the same as above
289  * except for the different limit on the loop, since
290  * the last iteration is the same as all the other
291  * iterations (beyond the first). */
292  word1 = row1[0];
293  word2 = row2[0] >> idelx;
294  andw = word1 & word2;
295  count += tab[andw & 0xff] +
296  tab[(andw >> 8) & 0xff] +
297  tab[(andw >> 16) & 0xff] +
298  tab[andw >> 24];
299 
300  for (x = 1; x < rowwords1; x++) {
301  word1 = row1[x];
302  word2 = (row2[x] >> idelx) |
303  (row2[x - 1] << (32 - idelx));
304  andw = word1 & word2;
305  count += tab[andw & 0xff] +
306  tab[(andw >> 8) & 0xff] +
307  tab[(andw >> 16) & 0xff] +
308  tab[andw >> 24];
309  }
310  }
311  }
312  } else {
313  /* pix2 is shifted to the left. word 0 of pix1 is touched by
314  * word 0 and word 1 of pix2, and so on up to the last word of
315  * pix1 (word N), which is touched by words N and N+1 of
316  * pix2... if there is a word N+1. Handle the two cases (pix2
317  * has N words and pix2 has at least N+1 words) separately. */
318  if (rowwords1 < rowwords2) {
319  /* pix2 has at least N+1 words, so every iteration through
320  * the loop can be the same. */
321  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
322  for (x = 0; x < rowwords1; x++) {
323  word1 = row1[x];
324  word2 = row2[x] << -idelx;
325  word2 |= row2[x + 1] >> (32 + idelx);
326  andw = word1 & word2;
327  count += tab[andw & 0xff] +
328  tab[(andw >> 8) & 0xff] +
329  tab[(andw >> 16) & 0xff] +
330  tab[andw >> 24];
331  }
332  }
333  } else {
334  /* pix2 has only N words, so the last iteration is broken
335  * out. */
336  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
337  for (x = 0; x < rowwords1 - 1; x++) {
338  word1 = row1[x];
339  word2 = row2[x] << -idelx;
340  word2 |= row2[x + 1] >> (32 + idelx);
341  andw = word1 & word2;
342  count += tab[andw & 0xff] +
343  tab[(andw >> 8) & 0xff] +
344  tab[(andw >> 16) & 0xff] +
345  tab[andw >> 24];
346  }
347 
348  word1 = row1[x];
349  word2 = row2[x] << -idelx;
350  andw = word1 & word2;
351  count += tab[andw & 0xff] +
352  tab[(andw >> 8) & 0xff] +
353  tab[(andw >> 16) & 0xff] +
354  tab[andw >> 24];
355  }
356  }
357  }
358  }
359 
360  *pscore = (l_float32)count * (l_float32)count /
361  ((l_float32)area1 * (l_float32)area2);
362 /* fprintf(stderr, "score = %5.3f, count = %d, area1 = %d, area2 = %d\n",
363  *pscore, count, area1, area2); */
364  return 0;
365 }
366 
367 
422 l_int32
423 pixCorrelationScoreThresholded(PIX *pix1,
424  PIX *pix2,
425  l_int32 area1,
426  l_int32 area2,
427  l_float32 delx, /* x(1) - x(3) */
428  l_float32 dely, /* y(1) - y(3) */
429  l_int32 maxdiffw,
430  l_int32 maxdiffh,
431  l_int32 *tab,
432  l_int32 *downcount,
433  l_float32 score_threshold)
434 {
435 l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count;
436 l_int32 wpl1, wpl2, lorow, hirow, locol, hicol, untouchable;
437 l_int32 x, y, pix1lskip, pix2lskip, rowwords1, rowwords2;
438 l_uint32 word1, word2, andw;
439 l_uint32 *row1, *row2;
440 l_float32 score;
441 l_int32 threshold;
442 
443  PROCNAME("pixCorrelationScoreThresholded");
444 
445  if (!pix1 || pixGetDepth(pix1) != 1)
446  return ERROR_INT("pix1 undefined or not 1 bpp", procName, 0);
447  if (!pix2 || pixGetDepth(pix2) != 1)
448  return ERROR_INT("pix2 undefined or not 1 bpp", procName, 0);
449  if (!tab)
450  return ERROR_INT("tab not defined", procName, 0);
451  if (area1 <= 0 || area2 <= 0)
452  return ERROR_INT("areas must be > 0", procName, 0);
453 
454  /* Eliminate based on size difference */
455  pixGetDimensions(pix1, &wi, &hi, NULL);
456  pixGetDimensions(pix2, &wt, &ht, NULL);
457  delw = L_ABS(wi - wt);
458  if (delw > maxdiffw)
459  return FALSE;
460  delh = L_ABS(hi - ht);
461  if (delh > maxdiffh)
462  return FALSE;
463 
464  /* Round difference to nearest integer */
465  if (delx >= 0)
466  idelx = (l_int32)(delx + 0.5);
467  else
468  idelx = (l_int32)(delx - 0.5);
469  if (dely >= 0)
470  idely = (l_int32)(dely + 0.5);
471  else
472  idely = (l_int32)(dely - 0.5);
473 
474  /* Compute the correlation count that is needed so that
475  * count * count / (area1 * area2) >= score_threshold */
476  threshold = (l_int32)ceil(sqrt((l_float64)score_threshold * area1 * area2));
477 
478  count = 0;
479  wpl1 = pixGetWpl(pix1);
480  wpl2 = pixGetWpl(pix2);
481  rowwords2 = wpl2;
482 
483  /* What rows of pix1 need to be considered? Only those underlying the
484  * shifted pix2. */
485  lorow = L_MAX(idely, 0);
486  hirow = L_MIN(ht + idely, hi);
487 
488  /* Get the pointer to the first row of each image that will be
489  * considered. */
490  row1 = pixGetData(pix1) + wpl1 * lorow;
491  row2 = pixGetData(pix2) + wpl2 * (lorow - idely);
492  if (hirow <= hi) {
493  /* Some rows of pix1 will never contribute to count */
494  untouchable = downcount[hirow - 1];
495  }
496 
497  /* Similarly, figure out which columns of pix1 will be considered. */
498  locol = L_MAX(idelx, 0);
499  hicol = L_MIN(wt + idelx, wi);
500 
501  if (idelx >= 32) {
502  /* pix2 is shifted far enough to the right that pix1's first
503  * word(s) won't contribute to the count. Increment its
504  * pointer to point to the first word that will contribute,
505  * and adjust other values accordingly. */
506  pix1lskip = idelx >> 5; /* # of words to skip on left */
507  row1 += pix1lskip;
508  locol -= pix1lskip << 5;
509  hicol -= pix1lskip << 5;
510  idelx &= 31;
511  } else if (idelx <= -32) {
512  /* pix2 is shifted far enough to the left that its first word(s)
513  * won't contribute to the count. Increment its pointer
514  * to point to the first word that will contribute,
515  * and adjust other values accordingly. */
516  pix2lskip = -((idelx + 31) >> 5); /* # of words to skip on left */
517  row2 += pix2lskip;
518  rowwords2 -= pix2lskip;
519  idelx += pix2lskip << 5;
520  }
521 
522  if ((locol >= hicol) || (lorow >= hirow)) { /* there is no overlap */
523  count = 0;
524  } else {
525  /* How many words of each row of pix1 need to be considered? */
526  rowwords1 = (hicol + 31) >> 5;
527 
528  if (idelx == 0) {
529  /* There's no lateral offset; simple case. */
530  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
531  for (x = 0; x < rowwords1; x++) {
532  andw = row1[x] & row2[x];
533  count += tab[andw & 0xff] +
534  tab[(andw >> 8) & 0xff] +
535  tab[(andw >> 16) & 0xff] +
536  tab[andw >> 24];
537  }
538 
539  /* If the count is over the threshold, no need to
540  * calculate any further. Likewise, return early if the
541  * count plus the maximum count attainable from further
542  * rows is below the threshold. */
543  if (count >= threshold) return TRUE;
544  if (count + downcount[y] - untouchable < threshold) {
545  return FALSE;
546  }
547  }
548  } else if (idelx > 0) {
549  /* pix2 is shifted to the right. word 0 of pix1 is touched by
550  * word 0 of pix2; word 1 of pix1 is touched by word 0 and word
551  * 1 of pix2, and so on up to the last word of pix1 (word N),
552  * which is touched by words N-1 and N of pix1... if there is a
553  * word N. Handle the two cases (pix2 has N-1 words and pix2
554  * has at least N words) separately.
555  *
556  * Note: we know that pix2 has at least N-1 words (i.e.,
557  * rowwords2 >= rowwords1 - 1) by the following logic.
558  * We can pretend that idelx <= 31 because the >= 32 logic
559  * above adjusted everything appropriately. Then
560  * hicol <= wt + idelx <= wt + 31, so
561  * hicol + 31 <= wt + 62
562  * rowwords1 = (hicol + 31) >> 5 <= (wt + 62) >> 5
563  * rowwords2 == (wt + 31) >> 5, so
564  * rowwords1 <= rowwords2 + 1 */
565  if (rowwords2 < rowwords1) {
566  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
567  /* Do the first iteration so the loop can be
568  * branch-free. */
569  word1 = row1[0];
570  word2 = row2[0] >> idelx;
571  andw = word1 & word2;
572  count += tab[andw & 0xff] +
573  tab[(andw >> 8) & 0xff] +
574  tab[(andw >> 16) & 0xff] +
575  tab[andw >> 24];
576 
577  for (x = 1; x < rowwords2; x++) {
578  word1 = row1[x];
579  word2 = (row2[x] >> idelx) |
580  (row2[x - 1] << (32 - idelx));
581  andw = word1 & word2;
582  count += tab[andw & 0xff] +
583  tab[(andw >> 8) & 0xff] +
584  tab[(andw >> 16) & 0xff] +
585  tab[andw >> 24];
586  }
587 
588  /* Now the last iteration - we know that this is safe
589  * (i.e. rowwords1 >= 2) because rowwords1 > rowwords2
590  * > 0 (if it was 0, we'd have taken the "count = 0"
591  * fast-path out of here). */
592  word1 = row1[x];
593  word2 = row2[x - 1] << (32 - idelx);
594  andw = word1 & word2;
595  count += tab[andw & 0xff] +
596  tab[(andw >> 8) & 0xff] +
597  tab[(andw >> 16) & 0xff] +
598  tab[andw >> 24];
599 
600  if (count >= threshold) return TRUE;
601  if (count + downcount[y] - untouchable < threshold) {
602  return FALSE;
603  }
604  }
605  } else {
606  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
607  /* Do the first iteration so the loop can be
608  * branch-free. This section is the same as above
609  * except for the different limit on the loop, since
610  * the last iteration is the same as all the other
611  * iterations (beyond the first). */
612  word1 = row1[0];
613  word2 = row2[0] >> idelx;
614  andw = word1 & word2;
615  count += tab[andw & 0xff] +
616  tab[(andw >> 8) & 0xff] +
617  tab[(andw >> 16) & 0xff] +
618  tab[andw >> 24];
619 
620  for (x = 1; x < rowwords1; x++) {
621  word1 = row1[x];
622  word2 = (row2[x] >> idelx) |
623  (row2[x - 1] << (32 - idelx));
624  andw = word1 & word2;
625  count += tab[andw & 0xff] +
626  tab[(andw >> 8) & 0xff] +
627  tab[(andw >> 16) & 0xff] +
628  tab[andw >> 24];
629  }
630 
631  if (count >= threshold) return TRUE;
632  if (count + downcount[y] - untouchable < threshold) {
633  return FALSE;
634  }
635  }
636  }
637  } else {
638  /* pix2 is shifted to the left. word 0 of pix1 is touched by
639  * word 0 and word 1 of pix2, and so on up to the last word of
640  * pix1 (word N), which is touched by words N and N+1 of
641  * pix2... if there is a word N+1. Handle the two cases (pix2
642  * has N words and pix2 has at least N+1 words) separately. */
643  if (rowwords1 < rowwords2) {
644  /* pix2 has at least N+1 words, so every iteration through
645  * the loop can be the same. */
646  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
647  for (x = 0; x < rowwords1; x++) {
648  word1 = row1[x];
649  word2 = row2[x] << -idelx;
650  word2 |= row2[x + 1] >> (32 + idelx);
651  andw = word1 & word2;
652  count += tab[andw & 0xff] +
653  tab[(andw >> 8) & 0xff] +
654  tab[(andw >> 16) & 0xff] +
655  tab[andw >> 24];
656  }
657 
658  if (count >= threshold) return TRUE;
659  if (count + downcount[y] - untouchable < threshold) {
660  return FALSE;
661  }
662  }
663  } else {
664  /* pix2 has only N words, so the last iteration is broken
665  * out. */
666  for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
667  for (x = 0; x < rowwords1 - 1; x++) {
668  word1 = row1[x];
669  word2 = row2[x] << -idelx;
670  word2 |= row2[x + 1] >> (32 + idelx);
671  andw = word1 & word2;
672  count += tab[andw & 0xff] +
673  tab[(andw >> 8) & 0xff] +
674  tab[(andw >> 16) & 0xff] +
675  tab[andw >> 24];
676  }
677 
678  word1 = row1[x];
679  word2 = row2[x] << -idelx;
680  andw = word1 & word2;
681  count += tab[andw & 0xff] +
682  tab[(andw >> 8) & 0xff] +
683  tab[(andw >> 16) & 0xff] +
684  tab[andw >> 24];
685 
686  if (count >= threshold) return TRUE;
687  if (count + downcount[y] - untouchable < threshold) {
688  return FALSE;
689  }
690  }
691  }
692  }
693  }
694 
695  score = (l_float32)count * (l_float32)count /
696  ((l_float32)area1 * (l_float32)area2);
697  if (score >= score_threshold) {
698  fprintf(stderr, "count %d < threshold %d but score %g >= score_threshold %g\n",
699  count, threshold, score, score_threshold);
700  }
701  return FALSE;
702 }
703 
704 
705 /* -------------------------------------------------------------------- *
706  * Simple 2 pix correlators (for jbig2 clustering) *
707  * -------------------------------------------------------------------- */
731 l_ok
732 pixCorrelationScoreSimple(PIX *pix1,
733  PIX *pix2,
734  l_int32 area1,
735  l_int32 area2,
736  l_float32 delx, /* x(1) - x(3) */
737  l_float32 dely, /* y(1) - y(3) */
738  l_int32 maxdiffw,
739  l_int32 maxdiffh,
740  l_int32 *tab,
741  l_float32 *pscore)
742 {
743 l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count;
744 PIX *pixt;
745 
746  PROCNAME("pixCorrelationScoreSimple");
747 
748  if (!pscore)
749  return ERROR_INT("&score not defined", procName, 1);
750  *pscore = 0.0;
751  if (!pix1 || pixGetDepth(pix1) != 1)
752  return ERROR_INT("pix1 undefined or not 1 bpp", procName, 1);
753  if (!pix2 || pixGetDepth(pix2) != 1)
754  return ERROR_INT("pix2 undefined or not 1 bpp", procName, 1);
755  if (!tab)
756  return ERROR_INT("tab not defined", procName, 1);
757  if (!area1 || !area2)
758  return ERROR_INT("areas must be > 0", procName, 1);
759 
760  /* Eliminate based on size difference */
761  pixGetDimensions(pix1, &wi, &hi, NULL);
762  pixGetDimensions(pix2, &wt, &ht, NULL);
763  delw = L_ABS(wi - wt);
764  if (delw > maxdiffw)
765  return 0;
766  delh = L_ABS(hi - ht);
767  if (delh > maxdiffh)
768  return 0;
769 
770  /* Round difference to nearest integer */
771  if (delx >= 0)
772  idelx = (l_int32)(delx + 0.5);
773  else
774  idelx = (l_int32)(delx - 0.5);
775  if (dely >= 0)
776  idely = (l_int32)(dely + 0.5);
777  else
778  idely = (l_int32)(dely - 0.5);
779 
780  /* pixt = pixAnd(NULL, pix1, pix2), including shift.
781  * To insure that pixels are ON only within the
782  * intersection of pix1 and the shifted pix2:
783  * (1) Start with pixt cleared and equal in size to pix1.
784  * (2) Blit the shifted pix2 onto pixt. Then all ON pixels
785  * are within the intersection of pix1 and the shifted pix2.
786  * (3) AND pix1 with pixt. */
787  pixt = pixCreateTemplate(pix1);
788  pixRasterop(pixt, idelx, idely, wt, ht, PIX_SRC, pix2, 0, 0);
789  pixRasterop(pixt, 0, 0, wi, hi, PIX_SRC & PIX_DST, pix1, 0, 0);
790  pixCountPixels(pixt, &count, tab);
791  pixDestroy(&pixt);
792 
793  *pscore = (l_float32)count * (l_float32)count /
794  ((l_float32)area1 * (l_float32)area2);
795 /* fprintf(stderr, "score = %5.3f, count = %d, area1 = %d, area2 = %d\n",
796  *pscore, count, area1, area2); */
797  return 0;
798 }
799 
800 
834 l_ok
835 pixCorrelationScoreShifted(PIX *pix1,
836  PIX *pix2,
837  l_int32 area1,
838  l_int32 area2,
839  l_int32 delx,
840  l_int32 dely,
841  l_int32 *tab,
842  l_float32 *pscore)
843 {
844 l_int32 w1, h1, w2, h2, count;
845 PIX *pixt;
846 
847  PROCNAME("pixCorrelationScoreShifted");
848 
849  if (!pscore)
850  return ERROR_INT("&score not defined", procName, 1);
851  *pscore = 0.0;
852  if (!pix1 || pixGetDepth(pix1) != 1)
853  return ERROR_INT("pix1 undefined or not 1 bpp", procName, 1);
854  if (!pix2 || pixGetDepth(pix2) != 1)
855  return ERROR_INT("pix2 undefined or not 1 bpp", procName, 1);
856  if (!tab)
857  return ERROR_INT("tab not defined", procName, 1);
858  if (!area1 || !area2)
859  return ERROR_INT("areas must be > 0", procName, 1);
860 
861  pixGetDimensions(pix1, &w1, &h1, NULL);
862  pixGetDimensions(pix2, &w2, &h2, NULL);
863 
864  /* To insure that pixels are ON only within the
865  * intersection of pix1 and the shifted pix2:
866  * (1) Start with pixt cleared and equal in size to pix1.
867  * (2) Blit the shifted pix2 onto pixt. Then all ON pixels
868  * are within the intersection of pix1 and the shifted pix2.
869  * (3) AND pix1 with pixt. */
870  pixt = pixCreateTemplate(pix1);
871  pixRasterop(pixt, delx, dely, w2, h2, PIX_SRC, pix2, 0, 0);
872  pixRasterop(pixt, 0, 0, w1, h1, PIX_SRC & PIX_DST, pix1, 0, 0);
873  pixCountPixels(pixt, &count, tab);
874  pixDestroy(&pixt);
875 
876  *pscore = (l_float32)count * (l_float32)count /
877  ((l_float32)area1 * (l_float32)area2);
878  return 0;
879 }
l_ok pixRasterop(PIX *pixd, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, PIX *pixs, l_int32 sx, l_int32 sy)
pixRasterop()
Definition: rop.c:193
l_uint32 * pixGetData(PIX *pix)
pixGetData()
Definition: pix1.c:1624
l_int32 y
Definition: conncomp.c:103
PIX * pixCreateTemplate(PIX *pixs)
pixCreateTemplate()
Definition: pix1.c:367
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
Definition: pix3.c:1823
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:543
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1065
Definition: pix.h:134
#define PIX_SRC
Definition: pix.h:327
#define PIX_DST
Definition: pix.h:328