Leptonica 1.54
Файл src/dewarp.c
#include <math.h>
#include "allheaders.h"

Макросы

#define DEBUG_TEXTLINE_CENTERS   0
#define DEBUG_SHORT_LINES   0

Функции

L_DEWARPdewarpCreate (PIX *pixs, l_int32 pageno, l_int32 sampling, l_int32 minlines, l_int32 applyhoriz)
void dewarpDestroy (L_DEWARP **pdew)
l_int32 dewarpBuildModel (L_DEWARP *dew, l_int32 debugflag)
PTAApixGetTextlineCenters (PIX *pixs, l_int32 debugflag)
PTApixGetMeanVerticals (PIX *pixs, l_int32 x, l_int32 y)
PTAAptaaRemoveShortLines (PIX *pixs, PTAA *ptaas, l_float32 fract, l_int32 debugflag)
FPIXfpixBuildHorizontalDisparity (FPIX *fpixv, l_float32 factor, l_int32 *pextraw)
FPIXfpixSampledDisparity (FPIX *fpixs, l_int32 sampling)
l_int32 dewarpApplyDisparity (L_DEWARP *dew, PIX *pixs, l_int32 debugflag)
PIXpixApplyVerticalDisparity (PIX *pixs, FPIX *fpix)
PIXpixApplyHorizontalDisparity (PIX *pixs, FPIX *fpix, l_int32 extraw)
l_int32 dewarpMinimize (L_DEWARP *dew)
l_int32 dewarpPopulateFullRes (L_DEWARP *dew)
L_DEWARPdewarpRead (const char *filename)
L_DEWARPdewarpReadStream (FILE *fp)
l_int32 dewarpWrite (const char *filename, L_DEWARP *dew)
l_int32 dewarpWriteStream (FILE *fp, L_DEWARP *dew)

Переменные

static const l_int32 L_DEFAULT_SAMPLING = 30
static const l_float32 DEFAULT_SLOPE_FACTOR = 2000.

Макросы

#define DEBUG_SHORT_LINES   0
#define DEBUG_TEXTLINE_CENTERS   0

Функции

l_int32 dewarpApplyDisparity ( L_DEWARP dew,
PIX pixs,
l_int32  debugflag 
)

dewarpApplyDisparity()

Input: dew pixs (image to be modified; can be 1, 8 or 32 bpp) debugflag Return: 0 if OK, 1 on error

Notes: (1) This applies the vertical disparity array to the specified image. For src pixels above the image, we use the pixels in the first raster line. (2) This works with stripped models. If the full resolution disparity array(s) are missing, they are remade.

l_int32 dewarpBuildModel ( L_DEWARP dew,
l_int32  debugflag 
)

dewarpBuildModel()

Input: dew debugflag (1 for debugging output) Return: 0 if OK, 1 on error

Notes: (1) This is the basic function that builds the vertical disparity array, which allows determination of the src pixel in the input image corresponding to each dest pixel in the dewarped image. (2) The method is as follows: * Estimate the centers of all the long textlines and fit a LS quadratic to each one. This smooths the curves. * Sample each curve at a regular interval, find the y-value of the flat point on each curve, and subtract the sampled curve value from this value. This is the vertical disparity. * Fit a LS quadratic to each set of vertically aligned disparity samples. This smooths the disparity values in the vertical direction. Then resample at the same regular interval, We now have a regular grid of smoothed vertical disparity valuels. * Interpolate this grid to get a full resolution disparity map. This can be applied directly to the src image pixels to dewarp the image in the vertical direction, making all textlines horizontal.

L_DEWARP* dewarpCreate ( PIX pixs,
l_int32  pageno,
l_int32  sampling,
l_int32  minlines,
l_int32  applyhoriz 
)

dewarpCreate()

Input: pixs (1 bpp) pageno (page number) sampling (use -1 or 0 for default value; otherwise minimum of 5) minlines (minimum number of lines to accept; e.g., 10) applyhoriz (1 to estimate horiz disparity; 0 to skip) Return: dew (or null on error)

Notes: (1) The page number is typically 0-based. If scanned from a book, the even pages are usually on the left. Disparity arrays built for even pages should only be applied to even pages. (2) The sampling factor is for the disparity array. The number used is not critical; anything between 10 and 60 should be fine. (3) The minimum number of nearly full-length lines required to generate a vertical disparity array. Use a small number if you are willing to accept a questionable array.

void dewarpDestroy ( L_DEWARP **  pdew)

dewarpDestroy()

Input: &dew (<will be="" set="" to="" null="" before="" returning>="">) Return: void

l_int32 dewarpMinimize ( L_DEWARP dew)

dewarpMinimize()

Input: dew Return: 0 if OK, 1 on error

Notes: (1) This removes all data that is not needed for serialization. It keeps the subsampled disparity array(s), so the full resolution arrays can be reconstructed.

l_int32 dewarpPopulateFullRes ( L_DEWARP dew)

dewarpPopulateFullRes()

Input: dew Return: 0 if OK, 1 on error

Notes: (1) If the full resolution vertical (and, optionally horizontal) disparity arrays do not exist, they are built from the subsampled ones.

L_DEWARP* dewarpRead ( const char *  filename)

dewarpRead()

Input: filename Return: dew, or null on error

L_DEWARP* dewarpReadStream ( FILE *  fp)

dewarpReadStream()

Input: stream Return: dew, or null on error

Notes: (1) The dewarp struct is stored in minimized format, with only subsampled disparity arrays.

l_int32 dewarpWrite ( const char *  filename,
L_DEWARP dew 
)

dewarpWrite()

Input: filename dew Return: 0 if OK, 1 on error

l_int32 dewarpWriteStream ( FILE *  fp,
L_DEWARP dew 
)

dewarpWriteStream()

Input: stream (opened for "wb") dew Return: 0 if OK, 1 on error

FPIX* fpixBuildHorizontalDisparity ( FPIX fpixv,
l_float32  factor,
l_int32 pextraw 
)

fpixBuildHorizontalDisparity()

Input: fpixv (vertical disparity model) factor (conversion factor for vertical disparity slope; use 0 for default) &extraw (<return> extra width to be added to dewarped pix) Return: fpixh, or null on error

Notes: (1) This takes the difference in vertical disparity at top and bottom of the image, and converts it to an assumed horizontal disparity.

FPIX* fpixSampledDisparity ( FPIX fpixs,
l_int32  sampling 
)

fpixSampledDisparity()

Input: fpixs (full resolution disparity model) sampling (sampling factor) Return: fpixd (sampled disparity model), or null on error

Notes: (1) The input array is sampled at the right and top edges, and at every pixels horizontally and vertically. (2) The sampled array is constructed large enough to (a) cover fpixs and (b) have the sampled grid on all boundary pixels in fpixd. Having sampled pixels around the boundary simplifies interpolation. (3) There must be at least 3 sampled points horizontally and vertically.

PIX* pixApplyHorizontalDisparity ( PIX pixs,
FPIX fpix,
l_int32  extraw 
)

pixApplyHorizontalDisparity()

Input: pixs (1, 8 or 32 bpp) fpix (horizontal disparity array) extraw (extra width added to pixd) Return: pixd (modified by fpix), or null on error

Notes: (1) This applies the horizontal disparity array to the specified image.

PIX* pixApplyVerticalDisparity ( PIX pixs,
FPIX fpix 
)

pixApplyVerticalDisparity()

Input: pixs (1, 8 or 32 bpp) fpix (vertical disparity array) Return: pixd (modified by fpix), or null on error

Notes: (1) This applies the vertical disparity array to the specified image. For src pixels above the image, we use the pixels in the first raster line.

PTA* pixGetMeanVerticals ( PIX pixs,
l_int32  x,
l_int32  y 
)

ptaGetMeanVerticals()

Input: pixs (1 bpp, single c.c.) x,y (location of UL corner of pixs with respect to page image Return: pta (mean y-values in component for each x-value, both translated by (x,y)

PTAA* pixGetTextlineCenters ( PIX pixs,
l_int32  debugflag 
)

pixGetTextlineCenters()

Input: pixs (1 bpp) debugflag (1 for debug output) Return: ptaa (of center values of textlines)

Notes: (1) This in general does not have a point for each value of x, because there will be gaps between words. It doesn't matter because we will fit a quadratic to the points that we do have.

PTAA* ptaaRemoveShortLines ( PIX pixs,
PTAA ptaas,
l_float32  fract,
l_int32  debugflag 
)

ptaaRemoveShortLines()

Input: pixs (1 bpp) ptaas (input lines) fract (minimum fraction of longest line to keep) debugflag Return: ptaad (containing only lines of sufficient length), or null on error


Переменные

const l_float32 DEFAULT_SLOPE_FACTOR = 2000. [static]
const l_int32 L_DEFAULT_SAMPLING = 30 [static]