Leptonica 1.68
C Image Processing Library

boxfunc2.c File Reference

Boxa/Box transforms; Boxa/Boxaa sorting; Boxaa utilities. More...

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "allheaders.h"

Go to the source code of this file.

Functions

BOXAboxaTransform (BOXA *boxas, l_int32 shiftx, l_int32 shifty, l_float32 scalex, l_float32 scaley)
BOXboxTransform (BOX *box, l_int32 shiftx, l_int32 shifty, l_float32 scalex, l_float32 scaley)
BOXAboxaTransformOrdered (BOXA *boxas, l_int32 shiftx, l_int32 shifty, l_float32 scalex, l_float32 scaley, l_int32 xcen, l_int32 ycen, l_float32 angle, l_int32 order)
BOXboxTransformOrdered (BOX *boxs, l_int32 shiftx, l_int32 shifty, l_float32 scalex, l_float32 scaley, l_int32 xcen, l_int32 ycen, l_float32 angle, l_int32 order)
BOXAboxaRotateOrth (BOXA *boxas, l_int32 w, l_int32 h, l_int32 rotation)
BOXboxRotateOrth (BOX *box, l_int32 w, l_int32 h, l_int32 rotation)
BOXAboxaSort (BOXA *boxas, l_int32 sorttype, l_int32 sortorder, NUMA **pnaindex)
BOXAboxaBinSort (BOXA *boxas, l_int32 sorttype, l_int32 sortorder, NUMA **pnaindex)
BOXAboxaSortByIndex (BOXA *boxas, NUMA *naindex)
BOXAAboxaSort2d (BOXA *boxas, NUMAA **pnaad, l_int32 delta1, l_int32 delta2, l_int32 minh1)
BOXAAboxaSort2dByIndex (BOXA *boxas, NUMAA *naa)
BOXboxaGetRankSize (BOXA *boxa, l_float32 fract)
BOXboxaGetMedian (BOXA *boxa)
l_int32 boxaaGetExtent (BOXAA *boxaa, l_int32 *pw, l_int32 *ph, BOX **pbox)
BOXAboxaaFlattenToBoxa (BOXAA *baa, NUMA **pnaindex, l_int32 copyflag)
l_int32 boxaaAlignBox (BOXAA *baa, BOX *box, l_int32 delta, l_int32 *pindex)

Variables

static const l_int32 MIN_COMPS_FOR_BIN_SORT = 500

Detailed Description

Boxa/Box transforms; Boxa/Boxaa sorting; Boxaa utilities.

Definition in file boxfunc2.c.


Function Documentation

BOXA* boxaTransform ( BOXA boxas,
l_int32  shiftx,
l_int32  shifty,
l_float32  scalex,
l_float32  scaley 
)

boxaTransform()

Input: boxa shiftx, shifty scalex, scaley Return: boxad, or null on error

Notes: (1) This is a very simple function that first shifts, then scales.

Definition at line 70 of file boxfunc2.c.

References boxaAddBox(), boxaCreate(), boxaGetBox(), boxaGetCount(), boxDestroy(), boxTransform(), ERROR_PTR, L_CLONE, L_INSERT, NULL, and PROCNAME.

Referenced by main(), and pixFindBaselines().

BOX* boxTransform ( BOX box,
l_int32  shiftx,
l_int32  shifty,
l_float32  scalex,
l_float32  scaley 
)

boxTransform()

Input: box shiftx, shifty scalex, scaley Return: boxd, or null on error

Notes: (1) This is a very simple function that first shifts, then scales.

Definition at line 111 of file boxfunc2.c.

References boxCreate(), ERROR_PTR, Box::h, L_MAX, NULL, PROCNAME, Box::w, Box::x, and Box::y.

Referenced by boxaTransform(), pixConvertToPdfDataSegmented(), and pixSplitComponentIntoBoxa().

BOXA* boxaTransformOrdered ( BOXA boxas,
l_int32  shiftx,
l_int32  shifty,
l_float32  scalex,
l_float32  scaley,
l_int32  xcen,
l_int32  ycen,
l_float32  angle,
l_int32  order 
)

boxaTransformOrdered()

Input: boxa shiftx, shifty scalex, scaley xcen, ycen (center of rotation) angle (in radians; clockwise is positive) order (one of 6 combinations: L_TR_SC_RO, ...) Return: boxd, or null on error

Notes: (1) This allows a sequence of linear transforms on each box. the transforms are from the affine set, composed of shift, scaling and rotation, and the order of the transforms is specified. (2) Although these operations appear to be on an infinite 2D plane, in practice the region of interest is clipped to a finite image. The center of rotation is usually taken with respect to the image (either the UL corner or the center). A translation can have two very different effects: (a) Moves the boxes across the fixed image region. (b) Moves the image origin, causing a change in the image region and an opposite effective translation of the boxes. This function should only be used for (a), where the image region is fixed on translation. If the image region is changed by the translation, use instead the functions in affinecompose.c, where the image region and rotation center can be computed from the actual clipping due to translation of the image origin. (3) See boxTransformOrdered() for usage and implementation details.

Definition at line 161 of file boxfunc2.c.

References boxaAddBox(), boxaCreate(), boxaGetBox(), boxaGetCount(), boxDestroy(), boxTransformOrdered(), ERROR_PTR, L_CLONE, L_INSERT, NULL, and PROCNAME.

Referenced by main().

BOX* boxTransformOrdered ( BOX boxs,
l_int32  shiftx,
l_int32  shifty,
l_float32  scalex,
l_float32  scaley,
l_int32  xcen,
l_int32  ycen,
l_float32  angle,
l_int32  order 
)

boxTransformOrdered()

Input: boxs shiftx, shifty scalex, scaley xcen, ycen (center of rotation) angle (in radians; clockwise is positive) order (one of 6 combinations: L_TR_SC_RO, ...) Return: boxd, or null on error

Notes: (1) This allows a sequence of linear transforms, composed of shift, scaling and rotation, where the order of the transforms is specified. (2) The rotation is taken about a point specified by (xcen, ycen). Let the components of the vector from the center of rotation to the box center be (xdif, ydif): xdif = (bx + 0.5 * bw) - xcen ydif = (by + 0.5 * bh) - ycen Then the box center after rotation has new components: bxcen = xcen + xdif * cosa + ydif * sina bycen = ycen + ydif * cosa - xdif * sina where cosa and sina are the cos and sin of the angle, and the enclosing box for the rotated box has size: rw = |bw * cosa| + |bh * sina| rh = |bh * cosa| + |bw * sina| where bw and bh are the unrotated width and height. Then the box UL corner (rx, ry) is rx = bxcen - 0.5 * rw ry = bycen - 0.5 * rh (3) The center of rotation specified by args and is the point BEFORE any translation or scaling. If the rotation is not the first operation, this function finds the actual center at the time of rotation. It does this by making the following assumptions: (1) Any scaling is with respect to the UL corner, so that the center location scales accordingly. (2) A translation does not affect the center of the image; it just moves the boxes. We always use assumption (1). However, assumption (2) will be incorrect if the apparent translation is due to a clipping operation that, in effect, moves the origin of the image. In that case, you should NOT use these simple functions. Instead, use the functions in affinecompose.c, where the rotation center can be computed from the actual clipping due to translation of the image origin.

Definition at line 245 of file boxfunc2.c.

References boxCreate(), boxGetGeometry(), ERROR_PTR, L_ABS, L_MAX, L_RO_SC_TR, L_RO_TR_SC, L_SC_RO_TR, L_SC_TR_RO, L_TR_RO_SC, L_TR_SC_RO, NULL, and PROCNAME.

Referenced by boxaTransformOrdered().

BOXA* boxaRotateOrth ( BOXA boxas,
l_int32  w,
l_int32  h,
l_int32  rotation 
)

boxaRotateOrth()

Input: boxa w, h (of image in which the boxa is embedded) rotation (0 = noop, 1 = 90 deg, 2 = 180 deg, 3 = 270 deg; all rotations are clockwise) Return: boxad, or null on error

Notes: (1) See boxRotateOrth() for details.

Definition at line 424 of file boxfunc2.c.

References boxaAddBox(), boxaCopy(), boxaCreate(), boxaGetBox(), boxaGetCount(), boxDestroy(), boxRotateOrth(), ERROR_PTR, L_CLONE, L_COPY, L_INSERT, NULL, and PROCNAME.

BOX* boxRotateOrth ( BOX box,
l_int32  w,
l_int32  h,
l_int32  rotation 
)

boxRotateOrth()

Input: box w, h (of image in which the box is embedded) rotation (0 = noop, 1 = 90 deg, 2 = 180 deg, 3 = 270 deg; all rotations are clockwise) Return: boxd, or null on error

Notes: (1) Rotate the image with the embedded box by the specified amount. (2) After rotation, the rotated box is always measured with respect to the UL corner of the image.

Definition at line 472 of file boxfunc2.c.

References boxCopy(), boxCreate(), boxGetGeometry(), ERROR_PTR, NULL, and PROCNAME.

Referenced by boxaRotateOrth().

BOXA* boxaSort ( BOXA boxas,
l_int32  sorttype,
l_int32  sortorder,
NUMA **  pnaindex 
)

boxaSort()

Input: boxa sorttype (L_SORT_BY_X, L_SORT_BY_Y, L_SORT_BY_WIDTH, L_SORT_BY_HEIGHT, L_SORT_BY_MIN_DIMENSION, L_SORT_BY_MAX_DIMENSION, L_SORT_BY_PERIMETER, L_SORT_BY_AREA, L_SORT_BY_ASPECT_RATIO) sortorder (L_SORT_INCREASING, L_SORT_DECREASING) &naindex (<optional return>=""> index of sorted order into original array) Return: boxad (sorted version of boxas), or null on error

Definition at line 516 of file boxfunc2.c.

References boxaBinSort(), boxaGetBoxGeometry(), boxaGetCount(), boxaSortByIndex(), ERROR_PTR, L_MAX, L_MIN, L_SORT_BY_AREA, L_SORT_BY_ASPECT_RATIO, L_SORT_BY_HEIGHT, L_SORT_BY_MAX_DIMENSION, L_SORT_BY_MIN_DIMENSION, L_SORT_BY_PERIMETER, L_SORT_BY_WIDTH, L_SORT_BY_X, L_SORT_BY_Y, L_SORT_DECREASING, L_SORT_INCREASING, L_WARNING, MIN_COMPS_FOR_BIN_SORT, NULL, numaAddNumber(), numaCreate(), numaDestroy(), numaGetSortIndex(), PROCNAME, and size.

Referenced by boxaSort2d(), BoxaSortTest(), main(), pixaGenerateFont(), pixDeskewBarcode(), and pixFindBaselines().

BOXA* boxaBinSort ( BOXA boxas,
l_int32  sorttype,
l_int32  sortorder,
NUMA **  pnaindex 
)

boxaBinSort()

Input: boxa sorttype (L_SORT_BY_X, L_SORT_BY_Y, L_SORT_BY_WIDTH, L_SORT_BY_HEIGHT, L_SORT_BY_PERIMETER) sortorder (L_SORT_INCREASING, L_SORT_DECREASING) &naindex (<optional return>=""> index of sorted order into original array) Return: boxad (sorted version of boxas), or null on error

Notes: (1) For a large number of boxes (say, greater than 1000), this O(n) binsort is much faster than the O(nlogn) shellsort. For 5000 components, this is over 20x faster than boxaSort(). (2) Consequently, boxaSort() calls this function if it will likely go much faster.

Definition at line 627 of file boxfunc2.c.

References boxaGetBoxGeometry(), boxaGetCount(), boxaSortByIndex(), ERROR_PTR, L_SORT_BY_HEIGHT, L_SORT_BY_PERIMETER, L_SORT_BY_WIDTH, L_SORT_BY_X, L_SORT_BY_Y, L_SORT_DECREASING, L_SORT_INCREASING, L_WARNING, NULL, numaAddNumber(), numaCreate(), numaDestroy(), numaGetBinSortIndex(), and PROCNAME.

Referenced by boxaSort(), and BoxaSortTest().

BOXA* boxaSortByIndex ( BOXA boxas,
NUMA naindex 
)

boxaSortByIndex()

Input: boxas naindex (na that maps from the new boxa to the input boxa) Return: boxad (sorted), or null on error

Definition at line 700 of file boxfunc2.c.

References boxaAddBox(), boxaCreate(), boxaGetBox(), boxaGetCount(), ERROR_PTR, L_COPY, L_INSERT, NULL, numaGetIValue(), and PROCNAME.

Referenced by boxaBinSort(), boxaPermutePseudorandom(), and boxaSort().

BOXAA* boxaSort2d ( BOXA boxas,
NUMAA **  pnaad,
l_int32  delta1,
l_int32  delta2,
l_int32  minh1 
)

boxaSort2d()

Input: boxas &naa (<optional return>=""> numaa with sorted indices whose values are the indices of the input array) delta1 (min overlap that permits aggregation of a box onto a boxa of horizontally-aligned boxes; pass 1) delta2 (min overlap that permits aggregation of a box onto a boxa of horizontally-aligned boxes; pass 2) minh1 (components less than this height either join an existing boxa or are set aside for pass 2) Return: boxaa (2d sorted version of boxa), or null on error

Notes: (1) The final result is a sort where the 'fast scan' direction is left to right, and the 'slow scan' direction is from top to bottom. Each boxa in the boxaa represents a sorted set of boxes from left to right. (2) Two passes are used to aggregate the boxas, which can corresond to characters or words in a line of text. In pass 1, only taller components, which correspond to xheight or larger, are permitted to start a new boxa, whereas in pass 2, the remaining vertically-challenged components are allowed to join an existing boxa or start a new one. (3) If delta1 < 0, the first pass allows aggregation when boxes in the same boxa do not overlap vertically. The distance by which they can miss and still be aggregated is the absolute value |delta1|. Similar for delta2 on the second pass. (4) On the first pass, any component of height less than minh1 cannot start a new boxa; it's put aside for later insertion. (5) On the second pass, any small component that doesn't align with an existing boxa can start a new one. (6) This can be used to identify lines of text from character or word bounding boxes.

Definition at line 764 of file boxfunc2.c.

References boxaaAddBox(), boxaaAddBoxa(), boxaaAlignBox(), boxaaCreate(), boxaAddBox(), boxaaDestroy(), boxaaGetBoxa(), boxaaGetCount(), boxaaReplaceBoxa(), boxaCreate(), boxaDestroy(), boxaGetBox(), boxaGetCount(), boxaSort(), boxGetGeometry(), ERROR_PTR, L_CLONE, L_INSERT, L_SORT_BY_X, L_SORT_BY_Y, L_SORT_INCREASING, NULL, numaaAddNuma(), numaaAddNumber(), numaaCreate(), numaAddNumber(), numaaDestroy(), numaaGetNuma(), numaaReplaceNuma(), numaCreate(), numaDestroy(), numaGetIValue(), numaSortByIndex(), and PROCNAME.

Referenced by pixGetWordBoxesInTextlines(), and pixGetWordsInTextlines().

BOXAA* boxaSort2dByIndex ( BOXA boxas,
NUMAA naa 
)

boxaSort2dByIndex()

Input: boxas naa (numaa that maps from the new baa to the input boxa) Return: baa (sorted boxaa), or null on error

Definition at line 904 of file boxfunc2.c.

References boxaaAddBoxa(), boxaaCreate(), boxaAddBox(), boxaCreate(), boxaGetBox(), boxaGetCount(), ERROR_PTR, L_CLONE, L_COPY, L_INSERT, NULL, numaaGetCount(), numaaGetNuma(), numaaGetNumberCount(), numaDestroy(), numaGetCount(), numaGetIValue(), and PROCNAME.

BOX* boxaGetRankSize ( BOXA boxa,
l_float32  fract 
)

boxaGetRankSize()

Input: boxa fract (use 0.0 for smallest, 1.0 for largest) Return: box (with rank values for x, y, w, h), or null on error or if the boxa is empty (has no valid boxes)

Notes: (1) This function does not assume that all boxes in the boxa are valid (2) The four box parameters are sorted independently. To assure that the resulting box size is sorted in increasing order:

  • x and y are sorted in decreasing order
  • w and h are sorted in increasing order

Definition at line 964 of file boxfunc2.c.

References boxaGetBoxGeometry(), boxaGetCount(), boxCreate(), ERROR_PTR, NULL, numaAddNumber(), numaCreate(), numaDestroy(), numaGetRankValue(), and PROCNAME.

Referenced by boxaGetMedian().

BOX* boxaGetMedian ( BOXA boxa)

boxaGetMedian()

Input: boxa Return: box (with median values for x, y, w, h), or null on error or if the boxa is empty.

Notes: (1) See boxaGetRankSize()

Definition at line 1018 of file boxfunc2.c.

References boxaGetCount(), boxaGetRankSize(), ERROR_PTR, NULL, and PROCNAME.

l_int32 boxaaGetExtent ( BOXAA boxaa,
l_int32 pw,
l_int32 ph,
BOX **  pbox 
)

boxaaGetExtent()

Input: boxaa &w (<optional return>=""> width) &h (<optional return>=""> height) &box (<optional return>="">, minimum box containing all boxa in boxaa) 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.

Definition at line 1049 of file boxfunc2.c.

References boxaaGetBoxa(), boxaaGetCount(), boxaGetBoxGeometry(), boxaGetCount(), boxCreate(), ERROR_INT, L_CLONE, L_MAX, L_MIN, NULL, and PROCNAME.

Referenced by boxaaDisplay().

BOXA* boxaaFlattenToBoxa ( BOXAA baa,
NUMA **  pnaindex,
l_int32  copyflag 
)

boxaaFlattenToBoxa()

Input: boxaa &naindex (<optional return>=""> the boxa index in the boxaa) copyflag (L_COPY or L_CLONE) Return: boxa, or null on error

Notes: (1) This 'flattens' the boxaa to a boxa, taking the boxes in order in the first boxa, then the second, etc. (2) If &naindex is defined, we generate a Numa that gives, for each box in the boxaa, the index of the boxa to which it belongs.

Definition at line 1108 of file boxfunc2.c.

References boxaAddBox(), boxaaGetBoxa(), boxaaGetCount(), boxaCreate(), boxaDestroy(), boxaGetBox(), boxaGetCount(), ERROR_PTR, L_CLONE, L_COPY, L_INSERT, NULL, numaAddNumber(), numaCreate(), and PROCNAME.

Referenced by pixGetWordBoxesInTextlines().

l_int32 boxaaAlignBox ( BOXAA baa,
BOX box,
l_int32  delta,
l_int32 pindex 
)

boxaaAlignBox()

Input: boxaa box (to be aligned with the last of one of the boxa in boxaa, if possible) delta (amount by which consecutive components can miss in overlap and still be included in the array) &index (of boxa with best overlap, or if none match, this is the index of the next boxa to be generated) Return: 0 if OK, 1 on error

Notes: (1) This is not greedy; it finds the boxa whose last box has the biggest overlap with the input box.

Definition at line 1164 of file boxfunc2.c.

References boxaaGetBoxa(), boxaaGetCount(), boxaDestroy(), boxaGetBoxGeometry(), boxaGetCount(), boxGetGeometry(), ERROR_INT, L_CLONE, L_WARNING, NULL, and PROCNAME.

Referenced by boxaSort2d().


Variable Documentation

const l_int32 MIN_COMPS_FOR_BIN_SORT = 500 [static]

Definition at line 52 of file boxfunc2.c.

Referenced by boxaSort().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines