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;
145 PROCNAME(
"pixCorrelationScore");
148 return ERROR_INT(
"&score not defined", procName, 1);
150 if (!pix1 || pixGetDepth(pix1) != 1)
151 return ERROR_INT(
"pix1 undefined or not 1 bpp", procName, 1);
152 if (!pix2 || pixGetDepth(pix2) != 1)
153 return ERROR_INT(
"pix2 undefined or not 1 bpp", procName, 1);
155 return ERROR_INT(
"tab not defined", procName, 1);
156 if (area1 <= 0 || area2 <= 0)
157 return ERROR_INT(
"areas must be > 0", procName, 1);
162 delw = L_ABS(wi - wt);
165 delh = L_ABS(hi - ht);
171 idelx = (l_int32)(delx + 0.5);
173 idelx = (l_int32)(delx - 0.5);
175 idely = (l_int32)(dely + 0.5);
177 idely = (l_int32)(dely - 0.5);
180 wpl1 = pixGetWpl(pix1);
181 wpl2 = pixGetWpl(pix2);
186 lorow = L_MAX(idely, 0);
187 hirow = L_MIN(ht + idely, hi);
192 row2 =
pixGetData(pix2) + wpl2 * (lorow - idely);
195 locol = L_MAX(idelx, 0);
196 hicol = L_MIN(wt + idelx, wi);
203 pix1lskip = idelx >> 5;
205 locol -= pix1lskip << 5;
206 hicol -= pix1lskip << 5;
208 }
else if (idelx <= -32) {
213 pix2lskip = -((idelx + 31) >> 5);
215 rowwords2 -= pix2lskip;
216 idelx += pix2lskip << 5;
219 if ((locol >= hicol) || (lorow >= hirow)) {
223 rowwords1 = (hicol + 31) >> 5;
227 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
228 for (x = 0; x < rowwords1; x++) {
229 andw = row1[x] & row2[x];
230 count += tab[andw & 0xff] +
231 tab[(andw >> 8) & 0xff] +
232 tab[(andw >> 16) & 0xff] +
236 }
else if (idelx > 0) {
253 if (rowwords2 < rowwords1) {
254 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
258 word2 = row2[0] >> idelx;
259 andw = word1 & word2;
260 count += tab[andw & 0xff] +
261 tab[(andw >> 8) & 0xff] +
262 tab[(andw >> 16) & 0xff] +
265 for (x = 1; x < rowwords2; x++) {
267 word2 = (row2[x] >> idelx) |
268 (row2[x - 1] << (32 - idelx));
269 andw = word1 & word2;
270 count += tab[andw & 0xff] +
271 tab[(andw >> 8) & 0xff] +
272 tab[(andw >> 16) & 0xff] +
281 word2 = row2[x - 1] << (32 - idelx);
282 andw = word1 & word2;
283 count += tab[andw & 0xff] +
284 tab[(andw >> 8) & 0xff] +
285 tab[(andw >> 16) & 0xff] +
289 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
296 word2 = row2[0] >> idelx;
297 andw = word1 & word2;
298 count += tab[andw & 0xff] +
299 tab[(andw >> 8) & 0xff] +
300 tab[(andw >> 16) & 0xff] +
303 for (x = 1; x < rowwords1; x++) {
305 word2 = (row2[x] >> idelx) |
306 (row2[x - 1] << (32 - idelx));
307 andw = word1 & word2;
308 count += tab[andw & 0xff] +
309 tab[(andw >> 8) & 0xff] +
310 tab[(andw >> 16) & 0xff] +
321 if (rowwords1 < rowwords2) {
324 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
325 for (x = 0; x < rowwords1; x++) {
327 word2 = row2[x] << -idelx;
328 word2 |= row2[x + 1] >> (32 + idelx);
329 andw = word1 & word2;
330 count += tab[andw & 0xff] +
331 tab[(andw >> 8) & 0xff] +
332 tab[(andw >> 16) & 0xff] +
339 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
340 for (x = 0; x < rowwords1 - 1; x++) {
342 word2 = row2[x] << -idelx;
343 word2 |= row2[x + 1] >> (32 + idelx);
344 andw = word1 & word2;
345 count += tab[andw & 0xff] +
346 tab[(andw >> 8) & 0xff] +
347 tab[(andw >> 16) & 0xff] +
352 word2 = row2[x] << -idelx;
353 andw = word1 & word2;
354 count += tab[andw & 0xff] +
355 tab[(andw >> 8) & 0xff] +
356 tab[(andw >> 16) & 0xff] +
363 *pscore = (l_float32)count * (l_float32)count /
364 ((l_float32)area1 * (l_float32)area2);
426 pixCorrelationScoreThresholded(
PIX *pix1,
436 l_float32 score_threshold)
438 l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count;
439 l_int32 wpl1, wpl2, lorow, hirow, locol, hicol, untouchable;
440 l_int32 x,
y, pix1lskip, pix2lskip, rowwords1, rowwords2;
441 l_uint32 word1, word2, andw;
442 l_uint32 *row1, *row2;
446 PROCNAME(
"pixCorrelationScoreThresholded");
448 if (!pix1 || pixGetDepth(pix1) != 1)
449 return ERROR_INT(
"pix1 undefined or not 1 bpp", procName, 0);
450 if (!pix2 || pixGetDepth(pix2) != 1)
451 return ERROR_INT(
"pix2 undefined or not 1 bpp", procName, 0);
453 return ERROR_INT(
"tab not defined", procName, 0);
454 if (area1 <= 0 || area2 <= 0)
455 return ERROR_INT(
"areas must be > 0", procName, 0);
460 delw = L_ABS(wi - wt);
463 delh = L_ABS(hi - ht);
469 idelx = (l_int32)(delx + 0.5);
471 idelx = (l_int32)(delx - 0.5);
473 idely = (l_int32)(dely + 0.5);
475 idely = (l_int32)(dely - 0.5);
479 threshold = (l_int32)ceil(sqrt((l_float64)score_threshold * area1 * area2));
482 wpl1 = pixGetWpl(pix1);
483 wpl2 = pixGetWpl(pix2);
488 lorow = L_MAX(idely, 0);
489 hirow = L_MIN(ht + idely, hi);
494 row2 =
pixGetData(pix2) + wpl2 * (lorow - idely);
497 untouchable = downcount[hirow - 1];
501 locol = L_MAX(idelx, 0);
502 hicol = L_MIN(wt + idelx, wi);
509 pix1lskip = idelx >> 5;
511 locol -= pix1lskip << 5;
512 hicol -= pix1lskip << 5;
514 }
else if (idelx <= -32) {
519 pix2lskip = -((idelx + 31) >> 5);
521 rowwords2 -= pix2lskip;
522 idelx += pix2lskip << 5;
525 if ((locol >= hicol) || (lorow >= hirow)) {
529 rowwords1 = (hicol + 31) >> 5;
533 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
534 for (x = 0; x < rowwords1; x++) {
535 andw = row1[x] & row2[x];
536 count += tab[andw & 0xff] +
537 tab[(andw >> 8) & 0xff] +
538 tab[(andw >> 16) & 0xff] +
546 if (count >= threshold)
return TRUE;
547 if (count + downcount[
y] - untouchable < threshold) {
551 }
else if (idelx > 0) {
568 if (rowwords2 < rowwords1) {
569 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
573 word2 = row2[0] >> idelx;
574 andw = word1 & word2;
575 count += tab[andw & 0xff] +
576 tab[(andw >> 8) & 0xff] +
577 tab[(andw >> 16) & 0xff] +
580 for (x = 1; x < rowwords2; x++) {
582 word2 = (row2[x] >> idelx) |
583 (row2[x - 1] << (32 - idelx));
584 andw = word1 & word2;
585 count += tab[andw & 0xff] +
586 tab[(andw >> 8) & 0xff] +
587 tab[(andw >> 16) & 0xff] +
596 word2 = row2[x - 1] << (32 - idelx);
597 andw = word1 & word2;
598 count += tab[andw & 0xff] +
599 tab[(andw >> 8) & 0xff] +
600 tab[(andw >> 16) & 0xff] +
603 if (count >= threshold)
return TRUE;
604 if (count + downcount[
y] - untouchable < threshold) {
609 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
616 word2 = row2[0] >> idelx;
617 andw = word1 & word2;
618 count += tab[andw & 0xff] +
619 tab[(andw >> 8) & 0xff] +
620 tab[(andw >> 16) & 0xff] +
623 for (x = 1; x < rowwords1; x++) {
625 word2 = (row2[x] >> idelx) |
626 (row2[x - 1] << (32 - idelx));
627 andw = word1 & word2;
628 count += tab[andw & 0xff] +
629 tab[(andw >> 8) & 0xff] +
630 tab[(andw >> 16) & 0xff] +
634 if (count >= threshold)
return TRUE;
635 if (count + downcount[
y] - untouchable < threshold) {
646 if (rowwords1 < rowwords2) {
649 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
650 for (x = 0; x < rowwords1; x++) {
652 word2 = row2[x] << -idelx;
653 word2 |= row2[x + 1] >> (32 + idelx);
654 andw = word1 & word2;
655 count += tab[andw & 0xff] +
656 tab[(andw >> 8) & 0xff] +
657 tab[(andw >> 16) & 0xff] +
661 if (count >= threshold)
return TRUE;
662 if (count + downcount[
y] - untouchable < threshold) {
669 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
670 for (x = 0; x < rowwords1 - 1; x++) {
672 word2 = row2[x] << -idelx;
673 word2 |= row2[x + 1] >> (32 + idelx);
674 andw = word1 & word2;
675 count += tab[andw & 0xff] +
676 tab[(andw >> 8) & 0xff] +
677 tab[(andw >> 16) & 0xff] +
682 word2 = row2[x] << -idelx;
683 andw = word1 & word2;
684 count += tab[andw & 0xff] +
685 tab[(andw >> 8) & 0xff] +
686 tab[(andw >> 16) & 0xff] +
689 if (count >= threshold)
return TRUE;
690 if (count + downcount[
y] - untouchable < threshold) {
698 score = (l_float32)count * (l_float32)count /
699 ((l_float32)area1 * (l_float32)area2);
700 if (score >= score_threshold) {
702 "count %d < threshold %d but score %g >= score_threshold %g\n",
703 count, threshold, score, score_threshold);
736 pixCorrelationScoreSimple(
PIX *pix1,
747 l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count;
750 PROCNAME(
"pixCorrelationScoreSimple");
753 return ERROR_INT(
"&score not defined", procName, 1);
755 if (!pix1 || pixGetDepth(pix1) != 1)
756 return ERROR_INT(
"pix1 undefined or not 1 bpp", procName, 1);
757 if (!pix2 || pixGetDepth(pix2) != 1)
758 return ERROR_INT(
"pix2 undefined or not 1 bpp", procName, 1);
760 return ERROR_INT(
"tab not defined", procName, 1);
761 if (!area1 || !area2)
762 return ERROR_INT(
"areas must be > 0", procName, 1);
767 delw = L_ABS(wi - wt);
770 delh = L_ABS(hi - ht);
776 idelx = (l_int32)(delx + 0.5);
778 idelx = (l_int32)(delx - 0.5);
780 idely = (l_int32)(dely + 0.5);
782 idely = (l_int32)(dely - 0.5);
797 *pscore = (l_float32)count * (l_float32)count /
798 ((l_float32)area1 * (l_float32)area2);
839 pixCorrelationScoreShifted(
PIX *pix1,
848 l_int32 w1, h1, w2, h2, count;
851 PROCNAME(
"pixCorrelationScoreShifted");
854 return ERROR_INT(
"&score not defined", procName, 1);
856 if (!pix1 || pixGetDepth(pix1) != 1)
857 return ERROR_INT(
"pix1 undefined or not 1 bpp", procName, 1);
858 if (!pix2 || pixGetDepth(pix2) != 1)
859 return ERROR_INT(
"pix2 undefined or not 1 bpp", procName, 1);
861 return ERROR_INT(
"tab not defined", procName, 1);
862 if (!area1 || !area2)
863 return ERROR_INT(
"areas must be > 0", procName, 1);
880 *pscore = (l_float32)count * (l_float32)count /
881 ((l_float32)area1 * (l_float32)area2);
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
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()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()