Leptonica  1.54
Файл src/boxfunc4.c
#include "allheaders.h"

Функции

BOXAboxaSelectBySize (BOXA *boxas, l_int32 width, l_int32 height, l_int32 type, l_int32 relation, l_int32 *pchanged)
NUMAboxaMakeSizeIndicator (BOXA *boxa, l_int32 width, l_int32 height, l_int32 type, l_int32 relation)
BOXAboxaSelectWithIndicator (BOXA *boxas, NUMA *na, l_int32 *pchanged)
BOXAboxaPermutePseudorandom (BOXA *boxas)
BOXAboxaPermuteRandom (BOXA *boxad, BOXA *boxas)
l_int32 boxaSwapBoxes (BOXA *boxa, l_int32 i, l_int32 j)
PTAboxaConvertToPta (BOXA *boxa, l_int32 ncorners)
BOXAptaConvertToBoxa (PTA *pta, l_int32 ncorners)
BOXAboxaSmoothSequence (BOXA *boxas, l_float32 factor, l_int32 max_error, l_int32 debug)
BOXAboxaLinearFit (BOXA *boxas, l_float32 factor, l_int32 max_error, l_int32 debug)
BOXAboxaConstrainSize (BOXA *boxas, l_int32 width, l_int32 widthflag, l_int32 height, l_int32 heightflag)
l_int32 boxaPlotSides (BOXA *boxa, const char *plotname, NUMA **pnal, NUMA **pnat, NUMA **pnar, NUMA **pnab, l_int32 outformat)
l_int32 boxaGetExtent (BOXA *boxa, l_int32 *pw, l_int32 *ph, BOX **pbox)
l_int32 boxaGetCoverage (BOXA *boxa, l_int32 wc, l_int32 hc, l_int32 exactflag, l_float32 *pfract)
l_int32 boxaSizeRange (BOXA *boxa, l_int32 *pminw, l_int32 *pminh, l_int32 *pmaxw, l_int32 *pmaxh)
l_int32 boxaLocationRange (BOXA *boxa, l_int32 *pminx, l_int32 *pminy, l_int32 *pmaxx, l_int32 *pmaxy)

Функции

BOXA* boxaConstrainSize ( BOXA boxas,
l_int32  width,
l_int32  widthflag,
l_int32  height,
l_int32  heightflag 
)

boxaConstrainSize()

Input: boxas width (force width of all boxes to this size; input 0 to use the median width) widthflag (L_ADJUST_LEFT, L_ADJUST_RIGHT, or L_ADJUST_LEFT_AND_RIGHT) height (force height of all boxes to this size; input 0 to use the median height) heightflag (L_ADJUST_TOP, L_ADJUST_BOT, or L_ADJUST_TOP_AND_BOT) Return: boxad (adjusted so all boxes are the same size)

Notes: (1) Typical input might be the output of boxaLinearFit(), where each side has been fit. This alters the width and height to the given value, moving the sides either in or out. (2) All input boxes should be valid. Median values will be used with invalid boxes.

PTA* boxaConvertToPta ( BOXA boxa,
l_int32  ncorners 
)

boxaConvertToPta()

Input: boxa ncorners (2 or 4 for the representation of each box) Return: pta (with points for each box in the boxa), or null on error

Notes: (1) If ncorners == 2, we select the UL and LR corners. Otherwise we save all 4 corners in this order: UL, UR, LL, LR.

l_int32 boxaGetCoverage ( BOXA boxa,
l_int32  wc,
l_int32  hc,
l_int32  exactflag,
l_float32 pfract 
)

boxaGetCoverage()

Input: boxa wc, hc (dimensions of overall clipping rectangle with UL corner at (0, 0) that is covered by the boxes. exactflag (1 for guaranteeing an exact result; 0 for getting an exact result only if the boxes do not overlap) &fract (<return> sum of box area as fraction of w * h) Return: 0 if OK, 1 on error

Notes: (1) The boxes in boxa are clipped to the input rectangle. (2) * When == 1, we generate a 1 bpp pix of size wc x hc, paint all the boxes black, and count the fg pixels. This can take 1 msec on a large page with many boxes. * When == 0, we clip each box to the wc x hc region and sum the resulting areas. This is faster. * The results are the same when none of the boxes overlap within the wc x hc region.

l_int32 boxaGetExtent ( BOXA boxa,
l_int32 pw,
l_int32 ph,
BOX **  pbox 
)

boxaGetExtent()

Input: boxa &w (<optional return>=""> width) &h (<optional return>=""> height) &box (<optional return>="">, minimum box containing all boxes in boxa) Return: 0 if OK, 1 on error

Notes: (1) The returned w and h are the minimum size image that would contain all boxes untranslated. (2) If there are no boxes, returned w and h are 0 and all parameters in the returned box are 0. This is not an error, because an empty boxa is valid and boxaGetExtent() is required for serialization.

BOXA* boxaLinearFit ( BOXA boxas,
l_float32  factor,
l_int32  max_error,
l_int32  debug 
)

boxaLinearFit()

Input: boxas (source boxa) factor (reject outliers with error greater than this number of median errors; typically ~3) max_error (maximum difference in pixels between fitted and original location to allow using the original value instead of the fitted value) debug (1 for debug output) Return: boxad (fitted boxa), or null on error

Notes: (1) Suppose you have a boxa where the box edges are expected to vary slowly and linearly across the set. These could be, for example, noisy measurements of similar regions on successive scanned pages. (2) Method: there are 2 basic steps: (a) Find outliers, separately based on the deviation from the median of the width and height of the box. After the width- and height-based outliers are removed, do a linear LSF for each of the four sides. Use to specify tolerance to outliers; use a very large value of to avoid rejecting points. (b) Using the LSF of (a), make the final determination of the four edge locations. See (3) for details. (3) The parameter makes the input values somewhat sticky. Use the fitted values only when the difference between input and fitted value is greater than . Two special cases: (a) set == 0 to use only fitted values in boxad. (b) set == 10000 to ignore all fitted values; then boxad will be the same as boxas. (4) Invalid input boxes are not used in computation of the LSF, and the output boxes are found from the LSF. (5) To enforce additional constraints on the size of each box, follow this operation with boxaConstrainSize(), taking boxad as input.

l_int32 boxaLocationRange ( BOXA boxa,
l_int32 pminx,
l_int32 pminy,
l_int32 pmaxx,
l_int32 pmaxy 
)

boxaLocationRange()

Input: boxa &minx, &miny, &maxx, &maxy (<optional return>=""> range of UL corner positions) Return: 0 if OK, 1 on error

NUMA* boxaMakeSizeIndicator ( BOXA boxa,
l_int32  width,
l_int32  height,
l_int32  type,
l_int32  relation 
)

boxaMakeSizeIndicator()

Input: boxa width, height (threshold dimensions) type (L_SELECT_WIDTH, L_SELECT_HEIGHT, L_SELECT_IF_EITHER, L_SELECT_IF_BOTH) relation (L_SELECT_IF_LT, L_SELECT_IF_GT, L_SELECT_IF_LTE, L_SELECT_IF_GTE) Return: na (indicator array), or null on error

Notes: (1) The args specify constraints on the size of the components that are kept. (2) If the selection type is L_SELECT_WIDTH, the input height is ignored, and v.v. (3) To keep small components, use relation = L_SELECT_IF_LT or L_SELECT_IF_LTE. To keep large components, use relation = L_SELECT_IF_GT or L_SELECT_IF_GTE.

boxaPermutePseudorandom()

Input: boxas (input boxa) Return: boxad (with boxes permuted), or null on error

Notes: (1) This does a pseudorandom in-place permutation of the boxes. (2) The result is guaranteed not to have any boxes in their original position, but it is not very random. If you need randomness, use boxaPermuteRandom().

BOXA* boxaPermuteRandom ( BOXA boxad,
BOXA boxas 
)

boxaPermuteRandom()

Input: boxad (<optional> can be null or equal to boxas) boxas (input boxa) Return: boxad (with boxes permuted), or null on error

Notes: (1) If boxad is null, make a copy of boxas and permute the copy. Otherwise, boxad must be equal to boxas, and the operation is done in-place. (2) This does a random in-place permutation of the boxes, by swapping each box in turn with a random box. The result is almost guaranteed not to have any boxes in their original position. (3) MSVC rand() has MAX_RAND = 2^15 - 1, so it will not do a proper permutation is the number of boxes exceeds this.

l_int32 boxaPlotSides ( BOXA boxa,
const char *  plotname,
NUMA **  pnal,
NUMA **  pnat,
NUMA **  pnar,
NUMA **  pnab,
l_int32  outformat 
)

boxaPlotSides()

Input: boxas (source boxa) plotname (<optional>, can be NULL) &nal (<optional return>=""> na of left sides) &nat (<optional return>=""> na of top sides) &nar (<optional return>=""> na of right sides) &nab (<optional return>=""> na of bottom sides) outformat (GPLOT_NONE for no output; GPLOT_PNG for png, etc) ut Return: 0 if OK, 1 on error

Notes: (1) This is a debugging function to show the progression of the four sides in the boxes. There must be at least 2 boxes. (2) One of three conditions holds: (a) only the even indices have valid boxes (b) only the odd indices have valid boxes (c) all indices have valid boxes This condition is determined by looking at the first 2 boxes. (3) The plotfiles are put in /tmp, and are named either with or, if NULL, a default name.

BOXA* boxaSelectBySize ( BOXA boxas,
l_int32  width,
l_int32  height,
l_int32  type,
l_int32  relation,
l_int32 pchanged 
)

boxaSelectBySize()

Input: boxas width, height (threshold dimensions) type (L_SELECT_WIDTH, L_SELECT_HEIGHT, L_SELECT_IF_EITHER, L_SELECT_IF_BOTH) relation (L_SELECT_IF_LT, L_SELECT_IF_GT, L_SELECT_IF_LTE, L_SELECT_IF_GTE) &changed (<optional return>=""> 1 if changed; 0 if clone returned) Return: boxad (filtered set), or null on error

Notes: (1) The args specify constraints on the size of the components that are kept. (2) Uses box clones in the new boxa. (3) If the selection type is L_SELECT_WIDTH, the input height is ignored, and v.v. (4) To keep small components, use relation = L_SELECT_IF_LT or L_SELECT_IF_LTE. To keep large components, use relation = L_SELECT_IF_GT or L_SELECT_IF_GTE.

BOXA* boxaSelectWithIndicator ( BOXA boxas,
NUMA na,
l_int32 pchanged 
)

boxaSelectWithIndicator()

Input: boxas na (indicator numa) &changed (<optional return>=""> 1 if changed; 0 if clone returned) Return: boxad, or null on error

Notes: (1) Returns a boxa clone if no components are removed. (2) Uses box clones in the new boxa. (3) The indicator numa has values 0 (ignore) and 1 (accept).

l_int32 boxaSizeRange ( BOXA boxa,
l_int32 pminw,
l_int32 pminh,
l_int32 pmaxw,
l_int32 pmaxh 
)

boxaSizeRange()

Input: boxa &minw, &minh, &maxw, &maxh (<optional return>=""> range of dimensions of box in the array) Return: 0 if OK, 1 on error

BOXA* boxaSmoothSequence ( BOXA boxas,
l_float32  factor,
l_int32  max_error,
l_int32  debug 
)

boxaSmoothSequence()

Input: boxas (source boxa) factor (reject outliers with error greater than this number of median errors; typically ~3) max_error (maximum difference in pixels between fitted and original location to allow using the original value instead of the fitted value) debug (1 for debug output) Return: boxad (fitted boxa), or null on error

Notes: (1) This does linear fitting separately to the sequences of even and odd boxes. It is assumed that in both the even and odd sets, the box edges vary slowly and linearly across each set.

l_int32 boxaSwapBoxes ( BOXA boxa,
l_int32  i,
l_int32  j 
)

boxaSwapBoxes()

Input: boxa i, j (two indices of boxes, that are to be swapped) Return: 0 if OK, 1 on error

BOXA* ptaConvertToBoxa ( PTA pta,
l_int32  ncorners 
)

ptaConvertToBoxa()

Input: pta ncorners (2 or 4 for the representation of each box) Return: boxa (with one box for each 2 or 4 points in the pta), or null on error

Notes: (1) For 2 corners, the order of the 2 points is UL, LR. For 4 corners, the order of points is UL, UR, LL, LR. (2) Each derived box is the minimum szie containing all corners.