Leptonica  1.83.1
Image processing and image analysis suite
dewarp4.c File Reference
#include <math.h>
#include "allheaders.h"

Go to the source code of this file.

Macros

#define DEBUG_INVALID_MODELS   0 /* set this to 1 for debugging */
 

Functions

static l_int32 dewarpaTestForValidModel (L_DEWARPA *dewa, L_DEWARP *dew, l_int32 notests)
 
l_ok dewarpSinglePage (PIX *pixs, l_int32 thresh, l_int32 adaptive, l_int32 useboth, l_int32 check_columns, PIX **ppixd, L_DEWARPA **pdewa, l_int32 debug)
 
l_ok dewarpSinglePageInit (PIX *pixs, l_int32 thresh, l_int32 adaptive, l_int32 useboth, l_int32 check_columns, PIX **ppixb, L_DEWARPA **pdewa)
 
l_ok dewarpSinglePageRun (PIX *pixs, PIX *pixb, L_DEWARPA *dewa, PIX **ppixd, l_int32 debug)
 
l_ok dewarpaListPages (L_DEWARPA *dewa)
 
l_ok dewarpaSetValidModels (L_DEWARPA *dewa, l_int32 notests, l_int32 debug)
 
l_ok dewarpaInsertRefModels (L_DEWARPA *dewa, l_int32 notests, l_int32 debug)
 
l_ok dewarpaStripRefModels (L_DEWARPA *dewa)
 
l_ok dewarpaRestoreModels (L_DEWARPA *dewa)
 
l_ok dewarpaInfo (FILE *fp, L_DEWARPA *dewa)
 
l_ok dewarpaModelStats (L_DEWARPA *dewa, l_int32 *pnnone, l_int32 *pnvsuccess, l_int32 *pnvvalid, l_int32 *pnhsuccess, l_int32 *pnhvalid, l_int32 *pnref)
 
l_ok dewarpaShowArrays (L_DEWARPA *dewa, l_float32 scalefact, l_int32 first, l_int32 last)
 
l_ok dewarpDebug (L_DEWARP *dew, const char *subdirs, l_int32 index)
 
l_ok dewarpShowResults (L_DEWARPA *dewa, SARRAY *sa, BOXA *boxa, l_int32 firstpage, l_int32 lastpage, const char *pdfout)
 

Variables

static const l_int32 GrayInValue = 200
 

Detailed Description


   Single page dewarper

   Reference model (book-level, dewarpa) operations and debugging output

     Top-level single page dewarper
         l_int32            dewarpSinglePage()
         l_int32            dewarpSinglePageInit()
         l_int32            dewarpSinglePageRun()

     Operations on dewarpa
         l_int32            dewarpaListPages()
         l_int32            dewarpaSetValidModels()
         l_int32            dewarpaInsertRefModels()
         l_int32            dewarpaStripRefModels()
         l_int32            dewarpaRestoreModels()

     Dewarp debugging output
         l_int32            dewarpaInfo()
         l_int32            dewarpaModelStats()
         static l_int32     dewarpaTestForValidModel()
         l_int32            dewarpaShowArrays()
         l_int32            dewarpDebug()
         l_int32            dewarpShowResults()

Definition in file dewarp4.c.

Function Documentation

◆ dewarpaInfo()

◆ dewarpaInsertRefModels()

l_ok dewarpaInsertRefModels ( L_DEWARPA dewa,
l_int32  notests,
l_int32  debug 
)

dewarpaInsertRefModels()

Parameters
[in]dewa
[in]notestsif 1, ignore curvature constraints on model
[in]debug1 to output information on invalid page models
Returns
0 if OK, 1 on error
Notes:
     (1) This destroys all dewarp models that are invalid, and then
         inserts reference models where possible.
     (2) If notests == 1, this ignores the curvature constraints
         and assumes that all successfully built models are valid.
     (3) If useboth == 0, it uses the closest valid model within the
         distance and parity constraints.  If useboth == 1, it tries
         to use the closest allowed hvalid model; if it doesn't find
         an hvalid model, it uses the closest valid model.
     (4) For all pages without a model, this clears out any existing
         invalid and reference dewarps, finds the nearest valid model
         with the same parity, and inserts an empty dewarp with the
         reference page.
     (5) Then if it is requested to use both vertical and horizontal
         disparity arrays (useboth == 1), it tries to replace any
         hvalid == 0 model or reference with an hvalid == 1 reference.
     (6) The distance constraint is that any reference model must
         be within maxdist.  Note that with the parity constraint,
         no reference models will be used if maxdist < 2.
     (7) This function must be called, even if reference models will
         not be used.  It should be called after building models on all
         available pages, and after setting the rendering parameters.
     (8) If the dewa has been serialized, this function is called by
         dewarpaRead() when it is read back.  It is also called
         any time the rendering parameters are changed.
     (9) Note: if this has been called with useboth == 1, and useboth
         is reset to 0, you should first call dewarpaRestoreModels()
         to bring real models from the cache back to the primary array.

Definition at line 444 of file dewarp4.c.

References L_Dewarpa::dewarp, dewarpaGetDewarp(), dewarpaInsertDewarp(), dewarpaRestoreModels(), dewarpaSetValidModels(), L_Dewarpa::dewarpcache, dewarpCreateRef(), L_Dewarp::hasref, L_Dewarp::hvalid, L_Dewarpa::maxdist, L_Dewarpa::maxpage, L_Dewarpa::modelsready, numaDestroy(), numaGetIValue(), numaMakeConstant(), numaReplaceNumber(), L_Dewarpa::useboth, and L_Dewarp::vvalid.

Referenced by dewarpaApplyInit().

◆ dewarpaListPages()

l_ok dewarpaListPages ( L_DEWARPA dewa)

dewarpaListPages()

Parameters
[in]dewapopulated with dewarp structs for pages
Returns
0 if OK, 1 on error list of page numbers, or NULL on error
Notes:
     (1) This generates two numas, stored in the dewarpa, that give:
         (a) the page number for each dew that has a page model.
         (b) the page number for each dew that has either a page
             model or a reference model.
         It can be called at any time.
     (2) It is called by the dewarpa serializer before writing.

Definition at line 290 of file dewarp4.c.

References dewarpaGetDewarp(), L_Dewarp::hasref, L_Dewarpa::maxpage, L_Dewarpa::namodels, L_Dewarpa::napages, numaAddNumber(), numaCreate(), numaDestroy(), and L_Dewarp::pageno.

Referenced by dewarpaModelStats(), dewarpaRestoreModels(), dewarpaStripRefModels(), and dewarpaWriteStream().

◆ dewarpaModelStats()

l_ok dewarpaModelStats ( L_DEWARPA dewa,
l_int32 *  pnnone,
l_int32 *  pnvsuccess,
l_int32 *  pnvvalid,
l_int32 *  pnhsuccess,
l_int32 *  pnhvalid,
l_int32 *  pnref 
)

dewarpaModelStats()

Parameters
[in]dewa
[out]pnnone[optional] number without any model
[out]pnvsuccess[optional] number with a vert model
[out]pnvvalid[optional] number with a valid vert model
[out]pnhsuccess[optional] number with both models
[out]pnhvalid[optional] number with both models valid
[out]pnref[optional] number with a reference model
Returns
0 if OK, 1 on error
Notes:
     (1) A page without a model has no dew.  It most likely failed to
         generate a vertical model, and has not been assigned a ref
         model from a neighboring page with a valid vertical model.
     (2) A page has vsuccess == 1 if there is at least a model of the
         vertical disparity.  The model may be invalid, in which case
         dewarpaInsertRefModels() will stash it in the cache and
         attempt to replace it by a valid ref model.
     (3) A vvvalid model is a vertical disparity model whose parameters
         satisfy the constraints given in dewarpaSetValidModels().
     (4) A page has hsuccess == 1 if both the vertical and horizontal
         disparity arrays have been constructed.
     (5) An  hvalid model has vertical and horizontal disparity
         models whose parameters satisfy the constraints given
         in dewarpaSetValidModels().
     (6) A page has a ref model if it failed to generate a valid
         model but was assigned a vvalid or hvalid model on another
         page (within maxdist) by dewarpaInsertRefModel().
     (7) This calls dewarpaTestForValidModel(); it ignores the vvalid
         and hvalid fields.

Definition at line 745 of file dewarp4.c.

References dewarpaGetDewarp(), dewarpaListPages(), dewarpaTestForValidModel(), L_Dewarp::hasref, L_Dewarp::hsuccess, L_Dewarp::hvalid, L_Dewarpa::napages, numaGetCount(), numaGetIValue(), L_Dewarp::vsuccess, and L_Dewarp::vvalid.

Referenced by dewarpaInfo().

◆ dewarpaRestoreModels()

l_ok dewarpaRestoreModels ( L_DEWARPA dewa)

dewarpaRestoreModels()

Parameters
[in]dewapopulated with dewarp structs for pages
Returns
0 if OK, 1 on error
Notes:
     (1) This puts all real models (and only real models) in the
         primary dewarpa array.  First remove all dewarps that are
         only references to other page models.  Then move all models
         that had been cached back into the primary dewarp array.
     (2) After this is done, we still need to recompute and insert
         the reference models before dewa->modelsready is true.

Definition at line 612 of file dewarp4.c.

References L_Dewarpa::dewarp, dewarpaListPages(), dewarpaStripRefModels(), L_Dewarpa::dewarpcache, L_Dewarpa::maxpage, and L_Dewarpa::modelsready.

Referenced by dewarpaInsertRefModels().

◆ dewarpaSetValidModels()

l_ok dewarpaSetValidModels ( L_DEWARPA dewa,
l_int32  notests,
l_int32  debug 
)

dewarpaSetValidModels()

Parameters
[in]dewa
[in]notests
[in]debug1 to output information on invalid page models
Returns
0 if OK, 1 on error
Notes:
     (1) A valid model must meet the rendering requirements, which
         include whether or not a vertical disparity model exists
         and conditions on curvatures for vertical and horizontal
         disparity models.
     (2) If notests == 1, this ignores the curvature constraints
         and assumes that all successfully built models are valid.
     (3) This function does not need to be called by the application.
         It is called by dewarpaInsertRefModels(), which
         will destroy all invalid dewarps.  Consequently, to inspect
         an invalid dewarp model, it must be done before calling
         dewarpaInsertRefModels().

Definition at line 340 of file dewarp4.c.

References dewarpaGetDewarp(), dewarpaTestForValidModel(), L_Dewarp::hasref, L_Dewarp::hsuccess, L_Dewarp::leftcurv, L_Dewarp::leftslope, L_Dewarpa::max_diff_edgecurv, L_Dewarpa::max_diff_linecurv, L_Dewarpa::max_edgecurv, L_Dewarpa::max_edgeslope, L_Dewarpa::max_linecurv, L_Dewarp::maxcurv, L_Dewarpa::maxpage, L_Dewarp::mincurv, L_Dewarp::rightcurv, L_Dewarp::rightslope, L_Dewarpa::useboth, and L_Dewarp::vsuccess.

Referenced by dewarpaInsertRefModels().

◆ dewarpaShowArrays()

l_ok dewarpaShowArrays ( L_DEWARPA dewa,
l_float32  scalefact,
l_int32  first,
l_int32  last 
)

dewarpaShowArrays()

Parameters
[in]dewa
[in]scalefacton contour images; typ. 0.5
[in]firstfirst page model to render
[in]lastlast page model to render; use 0 to go to end
Returns
0 if OK, 1 on error
Notes:
     (1) Generates a pdf of contour plots of the disparity arrays.
     (2) This only shows actual models; not ref models

Definition at line 897 of file dewarp4.c.

References bmfCreate(), dewarpaGetDewarp(), dewarpMinimize(), dewarpPopulateFullRes(), fpixRenderContours(), L_Dewarp::fullhdispar, L_Dewarp::fullvdispar, L_Dewarp::hasref, L_ADD_BELOW, L_INSERT, lept_mkdir(), lept_rmdir(), lept_stderr(), L_Dewarpa::maxpage, pixaAddPix(), pixaCreate(), pixAddSingleTextblock(), pixaDisplayTiledInRows(), pixDestroy(), pixScaleBySampling(), L_Dewarp::samphdispar, and L_Dewarp::sampvdispar.

◆ dewarpaStripRefModels()

l_ok dewarpaStripRefModels ( L_DEWARPA dewa)

dewarpaStripRefModels()

Parameters
[in]dewapopulated with dewarp structs for pages
Returns
0 if OK, 1 on error
Notes:
     (1) This examines each dew in a dewarpa, and removes
         all that don't have their own page model (i.e., all
         that have "references" to nearby pages with valid models).
         These references were generated by dewarpaInsertRefModels(dewa).

Definition at line 573 of file dewarp4.c.

References L_Dewarpa::dewarp, dewarpaGetDewarp(), dewarpaListPages(), dewarpDestroy(), L_Dewarp::hasref, L_Dewarpa::maxpage, and L_Dewarpa::modelsready.

Referenced by dewarpaRestoreModels().

◆ dewarpaTestForValidModel()

static l_int32 dewarpaTestForValidModel ( L_DEWARPA dewa,
L_DEWARP dew,
l_int32  notests 
)
static

dewarpaTestForValidModel()

Parameters
[in]dewa
[in]dew
[in]notests
Returns
0 if OK, 1 on error
Notes:
     (1) Computes validity of vertical (vvalid) model and both
         vertical and horizontal (hvalid) models.
     (2) If notests == 1, this ignores the curvature constraints
         and assumes that all successfully built models are valid.
     (3) This is just about the models, not the rendering process,
         so the value of useboth is not considered here.

Definition at line 811 of file dewarp4.c.

References L_Dewarp::hasref, L_Dewarp::hsuccess, L_Dewarp::hvalid, L_Dewarp::leftcurv, L_Dewarp::leftslope, lept_stderr(), L_Dewarpa::max_diff_edgecurv, L_Dewarpa::max_diff_linecurv, L_Dewarpa::max_edgecurv, L_Dewarpa::max_edgeslope, L_Dewarpa::max_linecurv, L_Dewarp::maxcurv, L_Dewarpa::min_diff_linecurv, L_Dewarp::mincurv, L_Dewarp::pageno, L_Dewarp::rightcurv, L_Dewarp::rightslope, L_Dewarp::vsuccess, and L_Dewarp::vvalid.

Referenced by dewarpaModelStats(), and dewarpaSetValidModels().

◆ dewarpDebug()

l_ok dewarpDebug ( L_DEWARP dew,
const char *  subdirs,
l_int32  index 
)

dewarpDebug()

Parameters
[in]dew
[in]subdirsone or more subdirectories of /tmp; e.g., "dew1"
[in]indexto help label output images; e.g., the page number
Returns
0 if OK, 1 on error
Notes:
     (1) Prints dewarp fields and generates disparity array contour images.
         The contour images are written to file:
               /tmp/[subdirs]/pixv_[index].png

Definition at line 991 of file dewarp4.c.

References dewarpPopulateFullRes(), fpixRenderContours(), L_Dewarp::fullvdispar, L_Dewarp::h, L_Dewarp::hasref, L_Dewarp::leftcurv, L_Dewarp::leftslope, lept_mkdir(), lept_stderr(), L_Dewarp::maxcurv, L_Dewarp::mincurv, L_Dewarp::minlines, L_Dewarp::nlines, L_Dewarp::nx, L_Dewarp::ny, L_Dewarp::pageno, pathJoin(), L_Dewarp::redfactor, L_Dewarp::refpage, L_Dewarp::rightcurv, L_Dewarp::rightslope, L_Dewarp::samphdispar, L_Dewarp::sampling, L_Dewarp::sampvdispar, and L_Dewarp::w.

◆ dewarpShowResults()

l_ok dewarpShowResults ( L_DEWARPA dewa,
SARRAY sa,
BOXA boxa,
l_int32  firstpage,
l_int32  lastpage,
const char *  pdfout 
)

dewarpShowResults()

Parameters
[in]dewa
[in]saof indexed input images
[in]boxacrop boxes for input images; can be null
[in]firstpage
[in]lastpage
[in]pdfoutfilename
Returns
0 if OK, 1 on error
Notes:
     (1) This generates a pdf of image pairs (before, after) for
         the designated set of input pages.
     (2) If the boxa exists, its elements are aligned with numbers
         in the filenames in sa.  It is used to crop the input images.
         It is assumed that the dewa was generated from the cropped
         images.  No undercropping is applied before rendering.

Definition at line 1076 of file dewarp4.c.

References bmfCreate(), boxaGetBox(), boxDestroy(), dewarpaApplyDisparity(), dewarpaGetDewarp(), L_CLONE, lept_mkdir(), lept_rmdir(), lept_stderr(), L_Dewarp::pageno, pixClipRectangle(), pixClone(), and pixReadIndexed().

◆ dewarpSinglePage()

l_ok dewarpSinglePage ( PIX pixs,
l_int32  thresh,
l_int32  adaptive,
l_int32  useboth,
l_int32  check_columns,
PIX **  ppixd,
L_DEWARPA **  pdewa,
l_int32  debug 
)

dewarpSinglePage()

Parameters
[in]pixswith text, any depth
[in]threshfor global thresh to 1 bpp; ignore otherwise
[in]adaptive1 for adaptive thresh; 0 for global threshold
[in]useboth1 for both horiz and vert; 0 for vertical only
[in]check_columns1 to skip horizontal if multiple columns; 0 otherwise; default is to skip
[out]ppixddewarped result
[out]pdewa[optional] dewa with single page; NULL to skip
[in]debug1 for debugging output, 0 otherwise
Returns
0 if OK, 1 on error list of page numbers, or NULL on error
Notes:
     (1) Dewarps pixs and returns the result in &pixd.
     (2) This uses default values for all model parameters.
     (3) If pixs is 1 bpp, the parameters adaptive and thresh are ignored.
     (4) If it can't build a model, returns a copy of pixs in &pixd.

Definition at line 100 of file dewarp4.c.

References dewarpaDestroy(), dewarpSinglePageInit(), dewarpSinglePageRun(), and pixDestroy().

◆ dewarpSinglePageInit()

l_ok dewarpSinglePageInit ( PIX pixs,
l_int32  thresh,
l_int32  adaptive,
l_int32  useboth,
l_int32  check_columns,
PIX **  ppixb,
L_DEWARPA **  pdewa 
)

dewarpSinglePageInit()

Parameters
[in]pixswith text, any depth
[in]threshfor global thresh to 1 bpp; ignore otherwise
[in]adaptive1 for adaptive thresh; 0 for global threshold
[in]useboth1 for both horiz and vert; 0 for vertical only
[in]check_columns1 to skip horizontal if multiple columns; 0 otherwise; default is to skip
[out]ppixb1 bpp debug image
[out]pdewainitialized dewa
Returns
0 if OK, 1 on error list of page numbers, or NULL on error
Notes:
     (1) This binarizes the input pixs if necessary, returning the
         binarized image.  It also initializes the dewa to default values
         for the model parameters.
     (2) If pixs is 1 bpp, the parameters adaptive and thresh are ignored.
     (3) To change the model parameters, call dewarpaSetCurvatures()
         before running dewarpSinglePageRun().  For example:
            dewarpSinglePageInit(pixs, 0, 1, 1, 1, &pixb, &dewa);
            dewarpaSetCurvatures(dewa, 250, -1, -1, 80, 70, 150);
            dewarpSinglePageRun(pixs, pixb, dewa, &pixd, 0);
            dewarpaDestroy(&dewa);
            pixDestroy(&pixb);

Definition at line 166 of file dewarp4.c.

Referenced by dewarpSinglePage().

◆ dewarpSinglePageRun()

l_ok dewarpSinglePageRun ( PIX pixs,
PIX pixb,
L_DEWARPA dewa,
PIX **  ppixd,
l_int32  debug 
)

dewarpSinglePageRun()

Parameters
[in]pixsany depth
[in]pixb1 bpp
[in]dewainitialized
[out]ppixddewarped result
[in]debug1 for debugging output, 0 otherwise
Returns
0 if OK, 1 on error list of page numbers, or NULL on error
Notes:
     (1) Dewarps pixs and returns the result in &pixd.
     (2) The 1 bpp version pixb and dewa are conveniently generated by
         dewarpSinglePageInit().
     (3) Non-default model parameters must be set before calling this.
     (4) If a model cannot be built, this returns a copy of pixs in &pixd.

Definition at line 226 of file dewarp4.c.

References dewarpaApplyDisparity(), dewarpaInsertDewarp(), dewarpaModelStatus(), dewarpBuildPageModel(), dewarpCreate(), lept_mkdir(), and pixCopy().

Referenced by dewarpSinglePage().