Leptonica 1.68
C Image Processing Library

correlscore.c File Reference

Correlation between pairs of binary images. More...

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "allheaders.h"

Go to the source code of this file.

Functions

l_float32 pixCorrelationScore (PIX *pix1, PIX *pix2, l_int32 area1, l_int32 area2, l_float32 delx, l_float32 dely, l_int32 maxdiffw, l_int32 maxdiffh, l_int32 *tab)
l_int32 pixCorrelationScoreThresholded (PIX *pix1, PIX *pix2, l_int32 area1, l_int32 area2, l_float32 delx, l_float32 dely, l_int32 maxdiffw, l_int32 maxdiffh, l_int32 *tab, l_int32 *downcount, l_float32 score_threshold)
l_float32 pixCorrelationScoreSimple (PIX *pix1, PIX *pix2, l_int32 area1, l_int32 area2, l_float32 delx, l_float32 dely, l_int32 maxdiffw, l_int32 maxdiffh, l_int32 *tab)

Detailed Description

Correlation between pairs of binary images.

   These are functions for computing correlation between
   pairs of 1 bpp images.

       l_float32   pixCorrelationScore()
       l_int32     pixCorrelationScoreThresholded()
       l_float32   pixCorrelationScoreSimple()

Definition in file correlscore.c.


Function Documentation

l_float32 pixCorrelationScore ( PIX pix1,
PIX pix2,
l_int32  area1,
l_int32  area2,
l_float32  delx,
l_float32  dely,
l_int32  maxdiffw,
l_int32  maxdiffh,
l_int32 tab 
)

pixCorrelationScore()

Input: pix1 (test pix, 1 bpp) pix2 (exemplar pix, 1 bpp) area1 (number of on pixels in pix1) area2 (number of on pixels in pix2) delx (x comp of centroid difference) dely (y comp of centroid difference) maxdiffw (max width difference of pix1 and pix2) maxdiffh (max height difference of pix1 and pix2) tab (sum tab for byte) Return: correlation score

Note: we check first that the two pix are roughly the same size. For jbclass (jbig2) applications at roughly 300 ppi, maxdiffw and maxdiffh should be at least 2.

Only if they meet that criterion do we compare the bitmaps. The centroid difference is used to align the two images to the nearest integer for the correlation.

The correlation score is the ratio of the square of the number of pixels in the AND of the two bitmaps to the product of the number of ON pixels in each. Denote the number of ON pixels in pix1 by |1|, the number in pix2 by |2|, and the number in the AND of pix1 and pix2 by |1 & 2|. The correlation score is then (|1 & 2|)**2 / (|1|*|2|).

This score is compared with an input threshold, which can be modified depending on the weight of the template. The modified threshold is thresh + (1.0 - thresh) * weight * R where weight is a fixed input factor between 0.0 and 1.0 R = |2| / area(2) and area(2) is the total number of pixels in 2 (i.e., width x height).

To understand why a weight factor is useful, consider what happens with thick, sans-serif characters that look similar and have a value of R near 1. Different characters can have a high correlation value, and the classifier will make incorrect substitutions. The weight factor raises the threshold for these characters.

Yet another approach to reduce such substitutions is to run the classifier in a non-greedy way, matching to the template with the highest score, not the first template with a score satisfying the matching constraint. However, this is not particularly effective.

The implementation here gives the same result as in pixCorrelationScoreSimple(), where a temporary Pix is made to hold the AND and implementation uses rasterop: pixt = pixCreateTemplate(pix1); pixRasterop(pixt, idelx, idely, wt, ht, PIX_SRC, pix2, 0, 0); pixRasterop(pixt, 0, 0, wi, hi, PIX_SRC & PIX_DST, pix1, 0, 0); pixCountPixels(pixt, &count, tab); pixDestroy(&pixt); However, here it is done in a streaming fashion, counting as it goes, and touching memory exactly once, giving a 3-4x speedup over the simple implementation. This very fast correlation matcher was contributed by William Rucklidge.

Definition at line 96 of file correlscore.c.

References ERROR_FLOAT, L_ABS, L_MAX, L_MIN, NULL, pixGetData(), pixGetDepth(), pixGetDimensions(), pixGetWpl(), PROCNAME, and FillSeg::y.

Referenced by jbClassifyCorrelation().

l_int32 pixCorrelationScoreThresholded ( PIX pix1,
PIX pix2,
l_int32  area1,
l_int32  area2,
l_float32  delx,
l_float32  dely,
l_int32  maxdiffw,
l_int32  maxdiffh,
l_int32 tab,
l_int32 downcount,
l_float32  score_threshold 
)

pixCorrelationScoreThresholded()

Input: pix1 (test pix, 1 bpp) pix2 (exemplar pix, 1 bpp) area1 (number of on pixels in pix1) area2 (number of on pixels in pix2) delx (x comp of centroid difference) dely (y comp of centroid difference) maxdiffw (max width difference of pix1 and pix2) maxdiffh (max height difference of pix1 and pix2) tab (sum tab for byte) downcount (count of 1 pixels below each row of pix1) score_threshold Return: whether the correlation score is >= score_threshold

Note: we check first that the two pix are roughly the same size. Only if they meet that criterion do we compare the bitmaps. The centroid difference is used to align the two images to the nearest integer for the correlation.

The correlation score is the ratio of the square of the number of pixels in the AND of the two bitmaps to the product of the number of ON pixels in each. Denote the number of ON pixels in pix1 by |1|, the number in pix2 by |2|, and the number in the AND of pix1 and pix2 by |1 & 2|. The correlation score is then (|1 & 2|)**2 / (|1|*|2|).

This score is compared with an input threshold, which can be modified depending on the weight of the template. The modified threshold is thresh + (1.0 - thresh) * weight * R where weight is a fixed input factor between 0.0 and 1.0 R = |2| / area(2) and area(2) is the total number of pixels in 2 (i.e., width x height).

To understand why a weight factor is useful, consider what happens with thick, sans-serif characters that look similar and have a value of R near 1. Different characters can have a high correlation value, and the classifier will make incorrect substitutions. The weight factor raises the threshold for these characters.

Yet another approach to reduce such substitutions is to run the classifier in a non-greedy way, matching to the template with the highest score, not the first template with a score satisfying the matching constraint. However, this is not particularly effective.

This very fast correlation matcher was contributed by William Rucklidge.

Definition at line 387 of file correlscore.c.

References ERROR_INT, FALSE, L_ABS, L_MAX, L_MIN, NULL, pixGetData(), pixGetDepth(), pixGetDimensions(), pixGetWpl(), PROCNAME, TRUE, and FillSeg::y.

Referenced by jbClassifyCorrelation().

l_float32 pixCorrelationScoreSimple ( PIX pix1,
PIX pix2,
l_int32  area1,
l_int32  area2,
l_float32  delx,
l_float32  dely,
l_int32  maxdiffw,
l_int32  maxdiffh,
l_int32 tab 
)

pixCorrelationScoreSimple()

Input: pix1 (test pix, 1 bpp) pix2 (exemplar pix, 1 bpp) area1 (number of on pixels in pix1) area2 (number of on pixels in pix2) delx (x comp of centroid difference) dely (y comp of centroid difference) maxdiffw (max width difference of pix1 and pix2) maxdiffh (max height difference of pix1 and pix2) tab (sum tab for byte) Return: correlation score

Notes: (1) This calculates exactly the same value as pixCorrelationScore(). It is 2-3x slower, but much simpler to understand.

Definition at line 687 of file correlscore.c.

References ERROR_FLOAT, L_ABS, NULL, PIX_DST, PIX_SRC, pixCountPixels(), pixCreateTemplate(), pixDestroy(), pixGetDepth(), pixGetDimensions(), pixRasterop(), and PROCNAME.

Referenced by jbClassifyCorrelation().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines