52 #include <config_auto.h>
56 #include "allheaders.h"
128 pixCorrelationScore(
PIX *pix1,
139 l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count;
140 l_int32 wpl1, wpl2, lorow, hirow, locol, hicol;
141 l_int32 x, y, pix1lskip, pix2lskip, rowwords1, rowwords2;
142 l_uint32 word1, word2, andw;
143 l_uint32 *row1, *row2;
146 return ERROR_INT(
"&score not defined", __func__, 1);
148 if (!pix1 || pixGetDepth(pix1) != 1)
149 return ERROR_INT(
"pix1 undefined or not 1 bpp", __func__, 1);
150 if (!pix2 || pixGetDepth(pix2) != 1)
151 return ERROR_INT(
"pix2 undefined or not 1 bpp", __func__, 1);
153 return ERROR_INT(
"tab not defined", __func__, 1);
154 if (area1 <= 0 || area2 <= 0)
155 return ERROR_INT(
"areas must be > 0", __func__, 1);
160 delw = L_ABS(wi - wt);
163 delh = L_ABS(hi - ht);
169 idelx = (l_int32)(delx + 0.5);
171 idelx = (l_int32)(delx - 0.5);
173 idely = (l_int32)(dely + 0.5);
175 idely = (l_int32)(dely - 0.5);
178 wpl1 = pixGetWpl(pix1);
179 wpl2 = pixGetWpl(pix2);
184 lorow = L_MAX(idely, 0);
185 hirow = L_MIN(ht + idely, hi);
190 row2 =
pixGetData(pix2) + wpl2 * (lorow - idely);
193 locol = L_MAX(idelx, 0);
194 hicol = L_MIN(wt + idelx, wi);
201 pix1lskip = idelx >> 5;
203 locol -= pix1lskip << 5;
204 hicol -= pix1lskip << 5;
206 }
else if (idelx <= -32) {
211 pix2lskip = -((idelx + 31) >> 5);
213 rowwords2 -= pix2lskip;
214 idelx += pix2lskip << 5;
217 if ((locol >= hicol) || (lorow >= hirow)) {
221 rowwords1 = (hicol + 31) >> 5;
225 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
226 for (x = 0; x < rowwords1; x++) {
227 andw = row1[x] & row2[x];
228 count += tab[andw & 0xff] +
229 tab[(andw >> 8) & 0xff] +
230 tab[(andw >> 16) & 0xff] +
234 }
else if (idelx > 0) {
251 if (rowwords2 < rowwords1) {
252 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
256 word2 = row2[0] >> idelx;
257 andw = word1 & word2;
258 count += tab[andw & 0xff] +
259 tab[(andw >> 8) & 0xff] +
260 tab[(andw >> 16) & 0xff] +
263 for (x = 1; x < rowwords2; x++) {
265 word2 = (row2[x] >> idelx) |
266 (row2[x - 1] << (32 - idelx));
267 andw = word1 & word2;
268 count += tab[andw & 0xff] +
269 tab[(andw >> 8) & 0xff] +
270 tab[(andw >> 16) & 0xff] +
279 word2 = row2[x - 1] << (32 - idelx);
280 andw = word1 & word2;
281 count += tab[andw & 0xff] +
282 tab[(andw >> 8) & 0xff] +
283 tab[(andw >> 16) & 0xff] +
287 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
294 word2 = row2[0] >> idelx;
295 andw = word1 & word2;
296 count += tab[andw & 0xff] +
297 tab[(andw >> 8) & 0xff] +
298 tab[(andw >> 16) & 0xff] +
301 for (x = 1; x < rowwords1; x++) {
303 word2 = (row2[x] >> idelx) |
304 (row2[x - 1] << (32 - idelx));
305 andw = word1 & word2;
306 count += tab[andw & 0xff] +
307 tab[(andw >> 8) & 0xff] +
308 tab[(andw >> 16) & 0xff] +
319 if (rowwords1 < rowwords2) {
322 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
323 for (x = 0; x < rowwords1; x++) {
325 word2 = row2[x] << -idelx;
326 word2 |= row2[x + 1] >> (32 + idelx);
327 andw = word1 & word2;
328 count += tab[andw & 0xff] +
329 tab[(andw >> 8) & 0xff] +
330 tab[(andw >> 16) & 0xff] +
337 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
338 for (x = 0; x < rowwords1 - 1; x++) {
340 word2 = row2[x] << -idelx;
341 word2 |= row2[x + 1] >> (32 + idelx);
342 andw = word1 & word2;
343 count += tab[andw & 0xff] +
344 tab[(andw >> 8) & 0xff] +
345 tab[(andw >> 16) & 0xff] +
350 word2 = row2[x] << -idelx;
351 andw = word1 & word2;
352 count += tab[andw & 0xff] +
353 tab[(andw >> 8) & 0xff] +
354 tab[(andw >> 16) & 0xff] +
361 *pscore = (l_float32)count * (l_float32)count /
362 ((l_float32)area1 * (l_float32)area2);
424 pixCorrelationScoreThresholded(
PIX *pix1,
434 l_float32 score_threshold)
436 l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count;
437 l_int32 wpl1, wpl2, lorow, hirow, locol, hicol, untouchable;
438 l_int32 x, y, pix1lskip, pix2lskip, rowwords1, rowwords2;
439 l_uint32 word1, word2, andw;
440 l_uint32 *row1, *row2;
444 if (!pix1 || pixGetDepth(pix1) != 1)
445 return ERROR_INT(
"pix1 undefined or not 1 bpp", __func__, 0);
446 if (!pix2 || pixGetDepth(pix2) != 1)
447 return ERROR_INT(
"pix2 undefined or not 1 bpp", __func__, 0);
449 return ERROR_INT(
"tab not defined", __func__, 0);
450 if (area1 <= 0 || area2 <= 0)
451 return ERROR_INT(
"areas must be > 0", __func__, 0);
456 delw = L_ABS(wi - wt);
459 delh = L_ABS(hi - ht);
465 idelx = (l_int32)(delx + 0.5);
467 idelx = (l_int32)(delx - 0.5);
469 idely = (l_int32)(dely + 0.5);
471 idely = (l_int32)(dely - 0.5);
475 threshold = (l_int32)ceil(sqrt((l_float64)score_threshold * area1 * area2));
478 wpl1 = pixGetWpl(pix1);
479 wpl2 = pixGetWpl(pix2);
484 lorow = L_MAX(idely, 0);
485 hirow = L_MIN(ht + idely, hi);
490 row2 =
pixGetData(pix2) + wpl2 * (lorow - idely);
493 untouchable = downcount[hirow - 1];
497 locol = L_MAX(idelx, 0);
498 hicol = L_MIN(wt + idelx, wi);
505 pix1lskip = idelx >> 5;
507 locol -= pix1lskip << 5;
508 hicol -= pix1lskip << 5;
510 }
else if (idelx <= -32) {
515 pix2lskip = -((idelx + 31) >> 5);
517 rowwords2 -= pix2lskip;
518 idelx += pix2lskip << 5;
521 if ((locol >= hicol) || (lorow >= hirow)) {
525 rowwords1 = (hicol + 31) >> 5;
529 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
530 for (x = 0; x < rowwords1; x++) {
531 andw = row1[x] & row2[x];
532 count += tab[andw & 0xff] +
533 tab[(andw >> 8) & 0xff] +
534 tab[(andw >> 16) & 0xff] +
542 if (count >= threshold)
return TRUE;
543 if (count + downcount[y] - untouchable < threshold) {
547 }
else if (idelx > 0) {
564 if (rowwords2 < rowwords1) {
565 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
569 word2 = row2[0] >> idelx;
570 andw = word1 & word2;
571 count += tab[andw & 0xff] +
572 tab[(andw >> 8) & 0xff] +
573 tab[(andw >> 16) & 0xff] +
576 for (x = 1; x < rowwords2; x++) {
578 word2 = (row2[x] >> idelx) |
579 (row2[x - 1] << (32 - idelx));
580 andw = word1 & word2;
581 count += tab[andw & 0xff] +
582 tab[(andw >> 8) & 0xff] +
583 tab[(andw >> 16) & 0xff] +
592 word2 = row2[x - 1] << (32 - idelx);
593 andw = word1 & word2;
594 count += tab[andw & 0xff] +
595 tab[(andw >> 8) & 0xff] +
596 tab[(andw >> 16) & 0xff] +
599 if (count >= threshold)
return TRUE;
600 if (count + downcount[y] - untouchable < threshold) {
605 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
612 word2 = row2[0] >> idelx;
613 andw = word1 & word2;
614 count += tab[andw & 0xff] +
615 tab[(andw >> 8) & 0xff] +
616 tab[(andw >> 16) & 0xff] +
619 for (x = 1; x < rowwords1; x++) {
621 word2 = (row2[x] >> idelx) |
622 (row2[x - 1] << (32 - idelx));
623 andw = word1 & word2;
624 count += tab[andw & 0xff] +
625 tab[(andw >> 8) & 0xff] +
626 tab[(andw >> 16) & 0xff] +
630 if (count >= threshold)
return TRUE;
631 if (count + downcount[y] - untouchable < threshold) {
642 if (rowwords1 < rowwords2) {
645 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
646 for (x = 0; x < rowwords1; x++) {
648 word2 = row2[x] << -idelx;
649 word2 |= row2[x + 1] >> (32 + idelx);
650 andw = word1 & word2;
651 count += tab[andw & 0xff] +
652 tab[(andw >> 8) & 0xff] +
653 tab[(andw >> 16) & 0xff] +
657 if (count >= threshold)
return TRUE;
658 if (count + downcount[y] - untouchable < threshold) {
665 for (y = lorow; y < hirow; y++, row1 += wpl1, row2 += wpl2) {
666 for (x = 0; x < rowwords1 - 1; x++) {
668 word2 = row2[x] << -idelx;
669 word2 |= row2[x + 1] >> (32 + idelx);
670 andw = word1 & word2;
671 count += tab[andw & 0xff] +
672 tab[(andw >> 8) & 0xff] +
673 tab[(andw >> 16) & 0xff] +
678 word2 = row2[x] << -idelx;
679 andw = word1 & word2;
680 count += tab[andw & 0xff] +
681 tab[(andw >> 8) & 0xff] +
682 tab[(andw >> 16) & 0xff] +
685 if (count >= threshold)
return TRUE;
686 if (count + downcount[y] - untouchable < threshold) {
694 score = (l_float32)count * (l_float32)count /
695 ((l_float32)area1 * (l_float32)area2);
696 if (score >= score_threshold) {
698 "count %d < threshold %d but score %g >= score_threshold %g\n",
699 count, threshold, score, score_threshold);
732 pixCorrelationScoreSimple(
PIX *pix1,
743 l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count;
747 return ERROR_INT(
"&score not defined", __func__, 1);
749 if (!pix1 || pixGetDepth(pix1) != 1)
750 return ERROR_INT(
"pix1 undefined or not 1 bpp", __func__, 1);
751 if (!pix2 || pixGetDepth(pix2) != 1)
752 return ERROR_INT(
"pix2 undefined or not 1 bpp", __func__, 1);
754 return ERROR_INT(
"tab not defined", __func__, 1);
755 if (!area1 || !area2)
756 return ERROR_INT(
"areas must be > 0", __func__, 1);
761 delw = L_ABS(wi - wt);
764 delh = L_ABS(hi - ht);
770 idelx = (l_int32)(delx + 0.5);
772 idelx = (l_int32)(delx - 0.5);
774 idely = (l_int32)(dely + 0.5);
776 idely = (l_int32)(dely - 0.5);
791 *pscore = (l_float32)count * (l_float32)count /
792 ((l_float32)area1 * (l_float32)area2);
833 pixCorrelationScoreShifted(
PIX *pix1,
842 l_int32 w1, h1, w2, h2, count;
846 return ERROR_INT(
"&score not defined", __func__, 1);
848 if (!pix1 || pixGetDepth(pix1) != 1)
849 return ERROR_INT(
"pix1 undefined or not 1 bpp", __func__, 1);
850 if (!pix2 || pixGetDepth(pix2) != 1)
851 return ERROR_INT(
"pix2 undefined or not 1 bpp", __func__, 1);
853 return ERROR_INT(
"tab not defined", __func__, 1);
854 if (!area1 || !area2)
855 return ERROR_INT(
"areas must be > 0", __func__, 1);
872 *pscore = (l_float32)count * (l_float32)count /
873 ((l_float32)area1 * (l_float32)area2);
l_uint32 * pixGetData(PIX *pix)
pixGetData()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
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()
void lept_stderr(const char *fmt,...)
lept_stderr()