src/skew.c
PIX *
pixDeskewGeneral(PIX        *pixs,
                 l_int32     redsweep,
                 l_float32   sweeprange,
                 l_float32   sweepdelta,
                 l_int32     redsearch,
                 l_int32     thresh,
                 l_float32  *pangle,
                 l_float32  *pconf)
{
l_int32    ret, depth;
l_float32  angle, conf, deg2rad;
PIX       *pixb, *pixd;

    PROCNAME("pixDeskewGeneral");

    if (!pixs)
        return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
    if (redsweep == 0)
        redsweep = DEFAULT_SWEEP_REDUCTION;
    else if (redsweep != 1 && redsweep != 2 && redsweep != 4)
        return (PIX *)ERROR_PTR("redsweep not in {1,2,4}", procName, NULL);
    if (sweeprange == 0.0)
        sweeprange = DEFAULT_SWEEP_RANGE;
    if (sweepdelta == 0.0)
        sweepdelta = DEFAULT_SWEEP_DELTA;
    if (redsearch == 0)
        redsearch = DEFAULT_BS_REDUCTION;
    else if (redsearch != 1 && redsearch != 2 && redsearch != 4)
        return (PIX *)ERROR_PTR("redsearch not in {1,2,4}", procName, NULL);
    if (thresh == 0)
        thresh = DEFAULT_BINARY_THRESHOLD;

    deg2rad = 3.1415926535 / 180.;

        /* Binarize if necessary */
    depth = pixGetDepth(pixs);
    if (depth == 1)
        pixb = pixClone(pixs);
    else
        pixb = pixConvertTo1(pixs, thresh);

        /* Use the 1 bpp image to find the skew */
    ret = pixFindSkewSweepAndSearch(pixb, &angle, &conf, redsweep, redsearch,
                                    sweeprange, sweepdelta,
                                    DEFAULT_MINBS_DELTA);
    pixDestroy(&pixb);
    if (pangle)
        *pangle = angle;
    if (pconf)
        *pconf = conf;
    if (ret)
        return pixClone(pixs);

    if (L_ABS(angle) < MIN_DESKEW_ANGLE || conf < MIN_ALLOWED_CONFIDENCE)
        return pixClone(pixs);

    if ((pixd = pixRotate(pixs, deg2rad * angle, L_ROTATE_AREA_MAP,
                          L_BRING_IN_WHITE, 0, 0)) == NULL)
        return pixClone(pixs);
    else
        return pixd;
}
/*!
 *  pixDeskewGeneral()
 *
 *      Input:  pixs  (any depth)
 *              redsweep  (for linear search: reduction factor = 1, 2 or 4;
 *                         use 0 for default)
 *              sweeprange (in degrees in each direction from 0;
 *                          use 0.0 for default)
 *              sweepdelta (in degrees; use 0.0 for default)
 *              redsearch  (for binary search: reduction factor = 1, 2 or 4;
 *                          use 0 for default;)
 *              thresh (for binarizing the image; use 0 for default)
 *              &angle   (<optional return> angle required to deskew,
 *                        in degrees; use NULL to skip)
 *              &conf    (<optional return> conf value is ratio
 *                        of max/min scores; use NULL to skip)
 *      Return: pixd (deskewed pix), or null on error
 *
 *  Notes:
 *      (1) This binarizes if necessary and finds the skew angle.  If the
 *          angle is large enough and there is sufficient confidence,
 *          it returns a deskewed image; otherwise, it returns a clone.
 */


(1) This binarizes if necessary and finds the skew angle.  If the
angle is large enough and there is sufficient confidence,
it returns a deskewed image; otherwise, it returns a clone.
( 1 ) This binarizes if necessary and finds the skew angle . If the angle is large enough and there is sufficient confidence it returns a deskewed image ; otherwise it returns a clone .
PIX *
pixDeskewGeneral(PIX        *pixs,
                 l_int32     redsweep,
                 l_float32   sweeprange,
                 l_float32   sweepdelta,
                 l_int32     redsearch,
                 l_int32     thresh,
                 l_float32  *pangle,
                 l_float32  *pconf)
{
l_int32    ret, depth;
l_float32  angle, conf, deg2rad;
PIX       *pixb, *pixd;
    PROCNAME("pixDeskewGeneral");
    if (!pixs)
        return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
    if (redsweep == 0)
        redsweep = DEFAULT_SWEEP_REDUCTION;
    else if (redsweep != 1 && redsweep != 2 && redsweep != 4)
        return (PIX *)ERROR_PTR("redsweep not in {1,2,4}", procName, NULL);
    if (sweeprange == 0.0)
        sweeprange = DEFAULT_SWEEP_RANGE;
    if (sweepdelta == 0.0)
        sweepdelta = DEFAULT_SWEEP_DELTA;
    if (redsearch == 0)
        redsearch = DEFAULT_BS_REDUCTION;
    else if (redsearch != 1 && redsearch != 2 && redsearch != 4)
        return (PIX *)ERROR_PTR("redsearch not in {1,2,4}", procName, NULL);
    if (thresh == 0)
        thresh = DEFAULT_BINARY_THRESHOLD;
    deg2rad = 3.1415926535 / 180.;
    depth = pixGetDepth(pixs);
    if (depth == 1)
        pixb = pixClone(pixs);
    else
        pixb = pixConvertTo1(pixs, thresh);
    ret = pixFindSkewSweepAndSearch(pixb, &angle, &conf, redsweep, redsearch,
                                    sweeprange, sweepdelta,
                                    DEFAULT_MINBS_DELTA);
    pixDestroy(&pixb);
    if (pangle)
        *pangle = angle;
    if (pconf)
        *pconf = conf;
    if (ret)
        return pixClone(pixs);
    if (L_ABS(angle) < MIN_DESKEW_ANGLE || conf < MIN_ALLOWED_CONFIDENCE)
        return pixClone(pixs);
    if ((pixd = pixRotate(pixs, deg2rad * angle, L_ROTATE_AREA_MAP,
                          L_BRING_IN_WHITE, 0, 0)) == NULL)
        return pixClone(pixs);
    else
        return pixd;
}
PIX * pixDeskewGeneral ( PIX * pixs , l_int32 redsweep , l_float32 sweeprange , l_float32 sweepdelta , l_int32 redsearch , l_int32 thresh , l_float32 * pangle , l_float32 * pconf ) { l_int32 ret , depth ; l_float32 angle , conf , deg2rad ; PIX * pixb , * pixd ; PROCNAME ( " " ) ; if ( ! pixs ) return ( PIX * ) ERROR_PTR ( " " , procName , NULL ) ; if ( redsweep == 0 ) redsweep = DEFAULT_SWEEP_REDUCTION ; else if ( redsweep != 1 && redsweep != 2 && redsweep != 4 ) return ( PIX * ) ERROR_PTR ( " " , procName , NULL ) ; if ( sweeprange == 0.0 ) sweeprange = DEFAULT_SWEEP_RANGE ; if ( sweepdelta == 0.0 ) sweepdelta = DEFAULT_SWEEP_DELTA ; if ( redsearch == 0 ) redsearch = DEFAULT_BS_REDUCTION ; else if ( redsearch != 1 && redsearch != 2 && redsearch != 4 ) return ( PIX * ) ERROR_PTR ( " " , procName , NULL ) ; if ( thresh == 0 ) thresh = DEFAULT_BINARY_THRESHOLD ; deg2rad = 3.1415926535 / 180. ; depth = pixGetDepth ( pixs ) ; if ( depth == 1 ) pixb = pixClone ( pixs ) ; else pixb = pixConvertTo1 ( pixs , thresh ) ; ret = pixFindSkewSweepAndSearch ( pixb , & angle , & conf , redsweep , redsearch , sweeprange , sweepdelta , DEFAULT_MINBS_DELTA ) ; pixDestroy ( & pixb ) ; if ( pangle ) * pangle = angle ; if ( pconf ) * pconf = conf ; if ( ret ) return pixClone ( pixs ) ; if ( L_ABS ( angle ) < MIN_DESKEW_ANGLE || conf < MIN_ALLOWED_CONFIDENCE ) return pixClone ( pixs ) ; if ( ( pixd = pixRotate ( pixs , deg2rad * angle , L_ROTATE_AREA_MAP , L_BRING_IN_WHITE , 0 , 0 ) ) == NULL ) return pixClone ( pixs ) ; else return pixd ; }
pixDeskewGeneral()
      Input:  pixs  (any depth)
              redsweep  (for linear search: reduction factor = 1, 2 or 4;
                         use 0 for default)
              sweeprange (in degrees in each direction from 0;
                          use 0.0 for default)
              sweepdelta (in degrees; use 0.0 for default)
              redsearch  (for binary search: reduction factor = 1, 2 or 4;
                          use 0 for default;)
              thresh (for binarizing the image; use 0 for default)
              &angle   (<optional return> angle required to deskew,
                        in degrees; use NULL to skip)
              &conf    (<optional return> conf value is ratio
                        of max/min scores; use NULL to skip)
      Return: pixd (deskewed pix), or null on error
pixDeskewGeneral () Input : pixs ( any depth ) redsweep ( for linear search : reduction factor = 1 2 or 4 ; use 0 for default ) sweeprange ( in degrees in each direction from 0 ; use 0 . 0 for default ) sweepdelta ( in degrees ; use 0 . 0 for default ) redsearch ( for binary search : reduction factor = 1 2 or 4 ; use 0 for default ; ) thresh ( for binarizing the image ; use 0 for default ) &angle ( <optional return > angle required to deskew in degrees ; use NULL to skip ) &conf ( <optional return > conf value is ratio of max / min scores ; use NULL to skip ) Return : pixd ( deskewed pix ) or null on error
/* Binarize if necessary */ /* Use the 1 bpp image to find the skew */