src/skew.c
l_int32
pixFindNormalizedSquareSum(PIX        *pixs,
                           l_float32  *phratio,
                           l_float32  *pvratio,
                           l_float32  *pfract)
{
l_int32    i, w, h, empty;
l_float32  sum, sumsq, uniform, val;
NUMA      *na;
PIX       *pixt;

    PROCNAME("pixFindNormalizedSquareSum");

    if (!pixs || pixGetDepth(pixs) != 1)
        return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
    pixGetDimensions(pixs, &w, &h, NULL);

    if (!phratio && !pvratio)
        return ERROR_INT("nothing to do", procName, 1);
    if (phratio) *phratio = 0.0;
    if (pvratio) *pvratio = 0.0;

    empty = 0;
    if (phratio) {
        na = pixCountPixelsByRow(pixs, NULL);
        numaGetSum(na, &sum);  /* fg pixels */
        if (pfract) *pfract = sum / (l_float32)(w * h);
        if (sum != 0.0) {
            uniform = sum * sum / h;   /*  h*(sum / h)^2  */
            sumsq = 0.0;
            for (i = 0; i < h; i++) {
                numaGetFValue(na, i, &val);
                sumsq += val * val;
            }
            *phratio = sumsq / uniform;
        }
        else
            empty = 1;
        numaDestroy(&na);
    }

    if (pvratio) {
        if (empty == 1) return 1;
        pixt = pixRotateOrth(pixs, 1);
        na = pixCountPixelsByRow(pixt, NULL);
        numaGetSum(na, &sum);
        if (pfract) *pfract = sum / (l_float32)(w * h);
        if (sum != 0.0) {
            uniform = sum * sum / w;
            sumsq = 0.0;
            for (i = 0; i < w; i++) {
                numaGetFValue(na, i, &val);
                sumsq += val * val;
            }
            *pvratio = sumsq / uniform;
        }
        else
            empty = 1;
        pixDestroy(&pixt);
        numaDestroy(&na);
    }

    return empty;
}
/*!
 *  pixFindNormalizedSquareSum()
 *
 *      Input:  pixs
 *              &hratio (<optional return> ratio of normalized horiz square sum
 *                       to result if the pixel distribution were uniform)
 *              &vratio (<optional return> ratio of normalized vert square sum
 *                       to result if the pixel distribution were uniform)
 *              &fract  (<optional return> ratio of fg pixels to total pixels)
 *      Return: 0 if OK, 1 on error or if there are no fg pixels
 *
 *  Notes:
 *      (1) Let the image have h scanlines and N fg pixels.
 *          If the pixels were uniformly distributed on scanlines,
 *          the sum of squares of fg pixels on each scanline would be
 *          h * (N / h)^2.  However, if the pixels are not uniformly
 *          distributed (e.g., for text), the sum of squares of fg
 *          pixels will be larger.  We return in hratio and vratio the
 *          ratio of these two values.
 *      (2) If there are no fg pixels, hratio and vratio are returned as 0.0.
 */
pixFindNormalizedSquareSum()
Input:  pixs
&hratio ( ratio of normalized horiz square sum
to result if the pixel distribution were uniform)
&vratio ( ratio of normalized vert square sum
to result if the pixel distribution were uniform)
&fract  ( ratio of fg pixels to total pixels)
Return: 0 if OK, 1 on error or if there are no fg pixels

(1) Let the image have h scanlines and N fg pixels.
If the pixels were uniformly distributed on scanlines,
the sum of squares of fg pixels on each scanline would be
h * (N / h)^2.  However, if the pixels are not uniformly
distributed , the sum of squares of fg
pixels will be larger.  We return in hratio and vratio the
ratio of these two values.
(2) If there are no fg pixels, hratio and vratio are returned as 0.0.
pixFindNormalizedSquareSum () Input : pixs &hratio ( ratio of normalized horiz square sum to result if the pixel distribution were uniform ) &vratio ( ratio of normalized vert square sum to result if the pixel distribution were uniform ) &fract ( ratio of fg pixels to total pixels ) Return : 0 if OK 1 on error or if there are no fg pixels ( 1 ) Let the image have h scanlines and N fg pixels . If the pixels were uniformly distributed on scanlines the sum of squares of fg pixels on each scanline would be h * ( N / h ) ^2 . However if the pixels are not uniformly distributed the sum of squares of fg pixels will be larger . We return in hratio and vratio the ratio of these two values . ( 2 ) If there are no fg pixels hratio and vratio are returned as 0 . 0 .
l_int32
pixFindNormalizedSquareSum(PIX        *pixs,
                           l_float32  *phratio,
                           l_float32  *pvratio,
                           l_float32  *pfract)
{
l_int32    i, w, h, empty;
l_float32  sum, sumsq, uniform, val;
NUMA      *na;
PIX       *pixt;
    PROCNAME("pixFindNormalizedSquareSum");
    if (!pixs || pixGetDepth(pixs) != 1)
        return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
    pixGetDimensions(pixs, &w, &h, NULL);
    if (!phratio && !pvratio)
        return ERROR_INT("nothing to do", procName, 1);
    if (phratio) *phratio = 0.0;
    if (pvratio) *pvratio = 0.0;
    empty = 0;
    if (phratio) {
        na = pixCountPixelsByRow(pixs, NULL);
        numaGetSum(na, &sum);  
        if (pfract) *pfract = sum / (l_float32)(w * h);
        if (sum != 0.0) {
            uniform = sum * sum / h;   
            sumsq = 0.0;
            for (i = 0; i < h; i++) {
                numaGetFValue(na, i, &val);
                sumsq += val * val;
            }
            *phratio = sumsq / uniform;
        }
        else
            empty = 1;
        numaDestroy(&na);
    }
    if (pvratio) {
        if (empty == 1) return 1;
        pixt = pixRotateOrth(pixs, 1);
        na = pixCountPixelsByRow(pixt, NULL);
        numaGetSum(na, &sum);
        if (pfract) *pfract = sum / (l_float32)(w * h);
        if (sum != 0.0) {
            uniform = sum * sum / w;
            sumsq = 0.0;
            for (i = 0; i < w; i++) {
                numaGetFValue(na, i, &val);
                sumsq += val * val;
            }
            *pvratio = sumsq / uniform;
        }
        else
            empty = 1;
        pixDestroy(&pixt);
        numaDestroy(&na);
    }
    return empty;
}
l_int32 pixFindNormalizedSquareSum ( PIX * pixs , l_float32 * phratio , l_float32 * pvratio , l_float32 * pfract ) { l_int32 i , w , h , empty ; l_float32 sum , sumsq , uniform , val ; NUMA * na ; PIX * pixt ; PROCNAME ( " " ) ; if ( ! pixs || pixGetDepth ( pixs ) != 1 ) return ERROR_INT ( " " , procName , 1 ) ; pixGetDimensions ( pixs , & w , & h , NULL ) ; if ( ! phratio && ! pvratio ) return ERROR_INT ( " " , procName , 1 ) ; if ( phratio ) * phratio = 0.0 ; if ( pvratio ) * pvratio = 0.0 ; empty = 0 ; if ( phratio ) { na = pixCountPixelsByRow ( pixs , NULL ) ; numaGetSum ( na , & sum ) ; if ( pfract ) * pfract = sum / ( l_float32 ) ( w * h ) ; if ( sum != 0.0 ) { uniform = sum * sum / h ; sumsq = 0.0 ; for ( i = 0 ; i < h ; i ++ ) { numaGetFValue ( na , i , & val ) ; sumsq += val * val ; } * phratio = sumsq / uniform ; } else empty = 1 ; numaDestroy ( & na ) ; } if ( pvratio ) { if ( empty == 1 ) return 1 ; pixt = pixRotateOrth ( pixs , 1 ) ; na = pixCountPixelsByRow ( pixt , NULL ) ; numaGetSum ( na , & sum ) ; if ( pfract ) * pfract = sum / ( l_float32 ) ( w * h ) ; if ( sum != 0.0 ) { uniform = sum * sum / w ; sumsq = 0.0 ; for ( i = 0 ; i < w ; i ++ ) { numaGetFValue ( na , i , & val ) ; sumsq += val * val ; } * pvratio = sumsq / uniform ; } else empty = 1 ; pixDestroy ( & pixt ) ; numaDestroy ( & na ) ; } return empty ; }
pixFindNormalizedSquareSum()
      Input:  pixs
              &hratio (<optional return> ratio of normalized horiz square sum
                       to result if the pixel distribution were uniform)
              &vratio (<optional return> ratio of normalized vert square sum
                       to result if the pixel distribution were uniform)
              &fract  (<optional return> ratio of fg pixels to total pixels)
      Return: 0 if OK, 1 on error or if there are no fg pixels
pixFindNormalizedSquareSum () Input : pixs &hratio ( <optional return > ratio of normalized horiz square sum to result if the pixel distribution were uniform ) &vratio ( <optional return > ratio of normalized vert square sum to result if the pixel distribution were uniform ) &fract ( <optional return > ratio of fg pixels to total pixels ) Return : 0 if OK 1 on error or if there are no fg pixels
/* fg pixels */ /*  h*(sum / h)^2  */