52 #include "allheaders.h" 125 pixCorrelationScore(
PIX *pix1,
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;
142 PROCNAME(
"pixCorrelationScore");
145 return ERROR_INT(
"&score not defined", procName, 1);
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);
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);
159 delw = L_ABS(wi - wt);
162 delh = L_ABS(hi - ht);
168 idelx = (l_int32)(delx + 0.5);
170 idelx = (l_int32)(delx - 0.5);
172 idely = (l_int32)(dely + 0.5);
174 idely = (l_int32)(dely - 0.5);
177 wpl1 = pixGetWpl(pix1);
178 wpl2 = pixGetWpl(pix2);
183 lorow = L_MAX(idely, 0);
184 hirow = L_MIN(ht + idely, hi);
189 row2 =
pixGetData(pix2) + wpl2 * (lorow - idely);
192 locol = L_MAX(idelx, 0);
193 hicol = L_MIN(wt + idelx, wi);
200 pix1lskip = idelx >> 5;
202 locol -= pix1lskip << 5;
203 hicol -= pix1lskip << 5;
205 }
else if (idelx <= -32) {
210 pix2lskip = -((idelx + 31) >> 5);
212 rowwords2 -= pix2lskip;
213 idelx += pix2lskip << 5;
216 if ((locol >= hicol) || (lorow >= hirow)) {
220 rowwords1 = (hicol + 31) >> 5;
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] +
233 }
else if (idelx > 0) {
250 if (rowwords2 < rowwords1) {
251 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
255 word2 = row2[0] >> idelx;
256 andw = word1 & word2;
257 count += tab[andw & 0xff] +
258 tab[(andw >> 8) & 0xff] +
259 tab[(andw >> 16) & 0xff] +
262 for (x = 1; x < rowwords2; 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] +
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] +
286 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
293 word2 = row2[0] >> idelx;
294 andw = word1 & word2;
295 count += tab[andw & 0xff] +
296 tab[(andw >> 8) & 0xff] +
297 tab[(andw >> 16) & 0xff] +
300 for (x = 1; x < rowwords1; 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] +
318 if (rowwords1 < rowwords2) {
321 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
322 for (x = 0; x < rowwords1; 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] +
336 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
337 for (x = 0; x < rowwords1 - 1; 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] +
349 word2 = row2[x] << -idelx;
350 andw = word1 & word2;
351 count += tab[andw & 0xff] +
352 tab[(andw >> 8) & 0xff] +
353 tab[(andw >> 16) & 0xff] +
360 *pscore = (l_float32)count * (l_float32)count /
361 ((l_float32)area1 * (l_float32)area2);
423 pixCorrelationScoreThresholded(
PIX *pix1,
433 l_float32 score_threshold)
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;
443 PROCNAME(
"pixCorrelationScoreThresholded");
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);
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);
457 delw = L_ABS(wi - wt);
460 delh = L_ABS(hi - ht);
466 idelx = (l_int32)(delx + 0.5);
468 idelx = (l_int32)(delx - 0.5);
470 idely = (l_int32)(dely + 0.5);
472 idely = (l_int32)(dely - 0.5);
476 threshold = (l_int32)ceil(sqrt((l_float64)score_threshold * area1 * area2));
479 wpl1 = pixGetWpl(pix1);
480 wpl2 = pixGetWpl(pix2);
485 lorow = L_MAX(idely, 0);
486 hirow = L_MIN(ht + idely, hi);
491 row2 =
pixGetData(pix2) + wpl2 * (lorow - idely);
494 untouchable = downcount[hirow - 1];
498 locol = L_MAX(idelx, 0);
499 hicol = L_MIN(wt + idelx, wi);
506 pix1lskip = idelx >> 5;
508 locol -= pix1lskip << 5;
509 hicol -= pix1lskip << 5;
511 }
else if (idelx <= -32) {
516 pix2lskip = -((idelx + 31) >> 5);
518 rowwords2 -= pix2lskip;
519 idelx += pix2lskip << 5;
522 if ((locol >= hicol) || (lorow >= hirow)) {
526 rowwords1 = (hicol + 31) >> 5;
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] +
543 if (count >= threshold)
return TRUE;
544 if (count + downcount[
y] - untouchable < threshold) {
548 }
else if (idelx > 0) {
565 if (rowwords2 < rowwords1) {
566 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
570 word2 = row2[0] >> idelx;
571 andw = word1 & word2;
572 count += tab[andw & 0xff] +
573 tab[(andw >> 8) & 0xff] +
574 tab[(andw >> 16) & 0xff] +
577 for (x = 1; x < rowwords2; 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] +
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] +
600 if (count >= threshold)
return TRUE;
601 if (count + downcount[
y] - untouchable < threshold) {
606 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
613 word2 = row2[0] >> idelx;
614 andw = word1 & word2;
615 count += tab[andw & 0xff] +
616 tab[(andw >> 8) & 0xff] +
617 tab[(andw >> 16) & 0xff] +
620 for (x = 1; x < rowwords1; 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] +
631 if (count >= threshold)
return TRUE;
632 if (count + downcount[
y] - untouchable < threshold) {
643 if (rowwords1 < rowwords2) {
646 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
647 for (x = 0; x < rowwords1; 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] +
658 if (count >= threshold)
return TRUE;
659 if (count + downcount[
y] - untouchable < threshold) {
666 for (
y = lorow;
y < hirow;
y++, row1 += wpl1, row2 += wpl2) {
667 for (x = 0; x < rowwords1 - 1; 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] +
679 word2 = row2[x] << -idelx;
680 andw = word1 & word2;
681 count += tab[andw & 0xff] +
682 tab[(andw >> 8) & 0xff] +
683 tab[(andw >> 16) & 0xff] +
686 if (count >= threshold)
return TRUE;
687 if (count + downcount[
y] - untouchable < threshold) {
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);
732 pixCorrelationScoreSimple(
PIX *pix1,
743 l_int32 wi, hi, wt, ht, delw, delh, idelx, idely, count;
746 PROCNAME(
"pixCorrelationScoreSimple");
749 return ERROR_INT(
"&score not defined", procName, 1);
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);
756 return ERROR_INT(
"tab not defined", procName, 1);
757 if (!area1 || !area2)
758 return ERROR_INT(
"areas must be > 0", procName, 1);
763 delw = L_ABS(wi - wt);
766 delh = L_ABS(hi - ht);
772 idelx = (l_int32)(delx + 0.5);
774 idelx = (l_int32)(delx - 0.5);
776 idely = (l_int32)(dely + 0.5);
778 idely = (l_int32)(dely - 0.5);
793 *pscore = (l_float32)count * (l_float32)count /
794 ((l_float32)area1 * (l_float32)area2);
835 pixCorrelationScoreShifted(
PIX *pix1,
844 l_int32 w1, h1, w2, h2, count;
847 PROCNAME(
"pixCorrelationScoreShifted");
850 return ERROR_INT(
"&score not defined", procName, 1);
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);
857 return ERROR_INT(
"tab not defined", procName, 1);
858 if (!area1 || !area2)
859 return ERROR_INT(
"areas must be > 0", procName, 1);
876 *pscore = (l_float32)count * (l_float32)count /
877 ((l_float32)area1 * (l_float32)area2);
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()
l_uint32 * pixGetData(PIX *pix)
pixGetData()
PIX * pixCreateTemplate(PIX *pixs)
pixCreateTemplate()
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()