Файл src/jbclass.c

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

Классы

struct  JbFindTemplatesState

Макросы

#define L_BUF_SIZE   512
#define MAX_ALLOWED_DILATION   14
#define DEBUG_PLOT_CC   0
#define DEBUG_CORRELATION_SCORE   0

Определения типов

typedef struct JbFindTemplatesState JBFINDCTX

Функции

static JBCLASSERjbCorrelationInitInternal (l_int32 components, l_int32 maxwidth, l_int32 maxheight, l_float32 thresh, l_float32 weightfactor, l_int32 keep_components)
static JBFINDCTXfindSimilarSizedTemplatesInit (JBCLASSER *classer, PIX *pixs)
static l_int32 findSimilarSizedTemplatesNext (JBFINDCTX *context)
static void findSimilarSizedTemplatesDestroy (JBFINDCTX **pcontext)
static l_int32 finalPositioningForAlignment (PIX *pixs, l_int32 x, l_int32 y, l_int32 idelx, l_int32 idely, PIX *pixt, l_int32 *sumtab, l_int32 *pdx, l_int32 *pdy)
JBCLASSERjbRankHausInit (l_int32 components, l_int32 maxwidth, l_int32 maxheight, l_int32 size, l_float32 rank)
JBCLASSERjbCorrelationInit (l_int32 components, l_int32 maxwidth, l_int32 maxheight, l_float32 thresh, l_float32 weightfactor)
JBCLASSERjbCorrelationInitWithoutComponents (l_int32 components, l_int32 maxwidth, l_int32 maxheight, l_float32 thresh, l_float32 weightfactor)
l_int32 jbAddPages (JBCLASSER *classer, SARRAY *safiles)
l_int32 jbAddPage (JBCLASSER *classer, PIX *pixs)
l_int32 jbAddPageComponents (JBCLASSER *classer, PIX *pixs, BOXA *boxas, PIXA *pixas)
l_int32 jbClassifyRankHaus (JBCLASSER *classer, BOXA *boxa, PIXA *pixas)
l_int32 pixHaustest (PIX *pix1, PIX *pix2, PIX *pix3, PIX *pix4, l_float32 delx, l_float32 dely, l_int32 maxdiffw, l_int32 maxdiffh)
l_int32 pixRankHaustest (PIX *pix1, PIX *pix2, PIX *pix3, PIX *pix4, l_float32 delx, l_float32 dely, l_int32 maxdiffw, l_int32 maxdiffh, l_int32 area1, l_int32 area3, l_float32 rank, l_int32 *tab8)
l_int32 jbClassifyCorrelation (JBCLASSER *classer, BOXA *boxa, PIXA *pixas)
l_int32 jbGetComponents (PIX *pixs, l_int32 components, l_int32 maxwidth, l_int32 maxheight, BOXA **pboxad, PIXA **ppixad)
PIXpixWordMaskByDilation (PIX *pixs, l_int32 maxsize, l_int32 *psize)
PIXAjbAccumulateComposites (PIXAA *pixaa, NUMA **pna, PTA **pptat)
PIXAjbTemplatesFromComposites (PIXA *pixac, NUMA *na)
JBCLASSERjbClasserCreate (l_int32 method, l_int32 components)
void jbClasserDestroy (JBCLASSER **pclasser)
JBDATAjbDataSave (JBCLASSER *classer)
void jbDataDestroy (JBDATA **pdata)
l_int32 jbDataWrite (const char *rootout, JBDATA *jbdata)
JBDATAjbDataRead (const char *rootname)
PIXAjbDataRender (JBDATA *data, l_int32 debugflag)
l_int32 jbGetULCorners (JBCLASSER *classer, PIX *pixs, BOXA *boxa)
l_int32 jbGetLLCorners (JBCLASSER *classer)

Переменные

static const l_int32 JB_ADDED_PIXELS = 6
static const l_int32 MAX_DIFF_WIDTH = 2
static const l_int32 MAX_DIFF_HEIGHT = 2
static const l_int32 MAX_CONN_COMP_WIDTH = 350
static const l_int32 MAX_CHAR_COMP_WIDTH = 350
static const l_int32 MAX_WORD_COMP_WIDTH = 1000
static const l_int32 MAX_COMP_HEIGHT = 120
static int two_by_two_walk [50]

Макросы

#define DEBUG_CORRELATION_SCORE   0

#define DEBUG_PLOT_CC   0

#define L_BUF_SIZE   512

#define MAX_ALLOWED_DILATION   14


Типы


Функции

static l_int32 finalPositioningForAlignment ( PIX pixs,
l_int32  x,
l_int32  y,
l_int32  idelx,
l_int32  idely,
PIX pixt,
l_int32 sumtab,
l_int32 pdx,
l_int32 pdy 
) [static]

finalPositioningForAlignment()

Input: pixs (input page image) x, y (location of UL corner of bb of component in pixs) idelx, idely (compensation to match centroids of component and template) pixt (template, with JB_ADDED_PIXELS of padding on all sides) sumtab (for summing fg pixels in an image) &dx, &dy (return delta on position for best match; each one is in the set {-1, 0, 1}) Return: 0 if OK, 1 on error

static void findSimilarSizedTemplatesDestroy ( JBFINDCTX **  pcontext  )  [static]

static JBFINDCTX * findSimilarSizedTemplatesInit ( JBCLASSER classer,
PIX pixs 
) [static]

findSimilarSizedTemplatesInit()

Input: classer pixs (instance to be matched) Return: Allocated context to be used with findSimilar*

static l_int32 findSimilarSizedTemplatesNext ( JBFINDCTX state  )  [static]

findSimilarSizedTemplatesNext()

Input: state (from findSimilarSizedTemplatesInit) Return: Next template number, or -1 when finished

We have a hash table mapping template area to a list of template numbers with that area. We wish to find similar sized templates, so we first look for templates with the same width and height, and then with width + 1, etc. This walk is guided by the two_by_two_walk array, above.

We don't want to have to collect the whole list of templates first because (we hope) to find it quickly. So we keep the context for this walk in an explictit state structure and this function acts like a generator.

PIXA* jbAccumulateComposites ( PIXAA pixaa,
NUMA **  pna,
PTA **  pptat 
)

jbAccumulateComposites()

Input: pixaa (one pixa for each class) &pna (<return> number of samples used to build each composite) &ptat (<return> centroids of bordered composites) Return: pixad (accumulated sum of samples in each class), or null on error

l_int32 jbAddPage ( JBCLASSER classer,
PIX pixs 
)

jbAddPage()

Input: jbclasser pixs (of input page) Return: 0 if OK; 1 on error

l_int32 jbAddPageComponents ( JBCLASSER classer,
PIX pixs,
BOXA boxas,
PIXA pixas 
)

jbAddPageComponents()

Input: jbclasser pixs (of input page) boxas (b.b. of components for this page) pixas (components for this page) Return: 0 if OK; 1 on error

Notes: (1) If there are no components on the page, we don't require input of empty boxas or pixas, although that's the typical situation.

l_int32 jbAddPages ( JBCLASSER classer,
SARRAY safiles 
)

jbAddPages()

Input: jbclasser safiles (of page image file names) Return: 0 if OK; 1 on error

Note: (1) jbclasser makes a copy of the array of file names. (2) The caller is still responsible for destroying the input array.

JBCLASSER* jbClasserCreate ( l_int32  method,
l_int32  components 
)

jbClasserCreate()

Input: method (JB_RANKHAUS, JB_CORRELATION) components (JB_CONN_COMPS, JB_CHARACTERS, JB_WORDS) Return: jbclasser, or null on error

void jbClasserDestroy ( JBCLASSER **  pclasser  ) 

l_int32 jbClassifyCorrelation ( JBCLASSER classer,
BOXA boxa,
PIXA pixas 
)

jbClassifyCorrelation()

Input: jbclasser boxa (of new components for classification) pixas (of new components for classification) Return: 0 if OK; 1 on error

l_int32 jbClassifyRankHaus ( JBCLASSER classer,
BOXA boxa,
PIXA pixas 
)

jbClassifyRankHaus()

Input: jbclasser boxa (of new components for classification) pixas (of new components for classification) Return: 0 if OK; 1 on error

JBCLASSER* jbCorrelationInit ( l_int32  components,
l_int32  maxwidth,
l_int32  maxheight,
l_float32  thresh,
l_float32  weightfactor 
)

jbCorrelationInit()

Input: components (JB_CONN_COMPS, JB_CHARACTERS, JB_WORDS) maxwidth (of component; use 0 for default) maxheight (of component; use 0 for default) thresh (value for correlation score: in [0.4 - 0.9]) weightfactor (corrects thresh for thick characters [0.0 - 1.0]) Return: jbclasser if OK; NULL on error

static JBCLASSER * jbCorrelationInitInternal ( l_int32  components,
l_int32  maxwidth,
l_int32  maxheight,
l_float32  thresh,
l_float32  weightfactor,
l_int32  keep_components 
) [static]

JBCLASSER* jbCorrelationInitWithoutComponents ( l_int32  components,
l_int32  maxwidth,
l_int32  maxheight,
l_float32  thresh,
l_float32  weightfactor 
)

jbCorrelationInitWithoutComponents()

Input: same as jbCorrelationInit Output: same as jbCorrelationInit

Note: acts the same as jbCorrelationInit(), but the resulting object doesn't keep a list of all the components.

void jbDataDestroy ( JBDATA **  pdata  ) 

JBDATA* jbDataRead ( const char *  rootname  ) 

jbDataRead()

Input: rootname (for template and data files) Return: jbdata, or NULL on error

PIXA* jbDataRender ( JBDATA data,
l_int32  debugflag 
)

jbDataRender()

Input: jbdata debugflag (if TRUE, writes into 2 bpp pix and adds component outlines in color) Return: pixa (reconstruction of original images, using templates) or null on error

JBDATA* jbDataSave ( JBCLASSER classer  ) 

jbDataSave()

Input: jbclasser latticew, latticeh (cell size used to store each connected component in the composite) Return: jbdata, or null on error

Notes: (1) This routine stores the jbig2-type data required for generating a lossy jbig2 version of the image. It can be losslessly written to (and read from) two files. (2) It generates and stores the mosaic of templates. (3) It clones the Numa and Pta arrays, so these must all be destroyed by the caller. (4) Input 0 to use the default values for latticew and/or latticeh,

l_int32 jbDataWrite ( const char *  rootout,
JBDATA jbdata 
)

jbDataWrite()

Input: rootname (for output files; everything but the extension) jbdata Return: 0 if OK, 1 on error

Notes: (1) Serialization function that writes data in jbdata to file.

l_int32 jbGetComponents ( PIX pixs,
l_int32  components,
l_int32  maxwidth,
l_int32  maxheight,
BOXA **  pboxad,
PIXA **  ppixad 
)

jbGetComponents()

Input: pixs (1 bpp) components (JB_CONN_COMPS, JB_CHARACTERS, JB_WORDS) maxwidth, maxheight (of saved components; larger are discarded) &pboxa (<return> b.b. of component items) &ppixa (<return> component items) Return: 0 if OK, 1 on error

l_int32 jbGetLLCorners ( JBCLASSER classer  ) 

jbGetLLCorners()

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

Notes: (1) This computes the ptall field, which has the global LL corners, adjusted for each specific component, so that each component can be replaced by the template for its class and have the centroid in the template in the same position as the centroid of the original connected component. It is important that this be done properly to avoid a wavy baseline in the result. (2) It is computed here from the corresponding UL corners, where the input templates and stored instances are all bordered. This should be done after all pages have been processed. (3) For proper substitution, the templates whose LL corners are placed in these locations must be UN-bordered. This is available for a realistic jbig2 encoder, which would (1) encode each template without a border, and (2) encode the position using the LL corner (rather than the UL corner) because the difference between y-values of successive instances is typically close to zero.

l_int32 jbGetULCorners ( JBCLASSER classer,
PIX pixs,
BOXA boxa 
)

jbGetULCorners()

Input: jbclasser pixs (full res image) boxa (of c.c. bounding rectangles for this page) Return: 0 if OK, 1 on error

Notes: (1) This computes the ptaul field, which has the global UL corners, adjusted for each specific component, so that each component can be replaced by the template for its class and have the centroid in the template in the same position as the centroid of the original connected component. It is important that this be done properly to avoid a wavy baseline in the result. (2) The array fields ptac and ptact give the centroids of those components relative to the UL corner of each component. Here, we compute the difference in each component, round to nearest integer, and correct the box->x and box->y by the appropriate integral difference. (3) The templates and stored instances are all bordered.

JBCLASSER* jbRankHausInit ( l_int32  components,
l_int32  maxwidth,
l_int32  maxheight,
l_int32  size,
l_float32  rank 
)

jbRankHausInit()

Input: components (JB_CONN_COMPS, JB_CHARACTERS, JB_WORDS) maxwidth (of component; use 0 for default) maxheight (of component; use 0 for default) size (of square structuring element; 2, representing 2x2 sel, is necessary for reasonable accuracy of small components; combine this with rank ~ 0.97 to avoid undue class expansion) rank (rank val of match, each way; in [0.5 - 1.0]; when using size = 2, 0.97 is a reasonable value) Return: jbclasser if OK; NULL on error

PIXA* jbTemplatesFromComposites ( PIXA pixac,
NUMA na 
)

jbTemplatesFromComposites()

Input: pixac (one pix of composites for each class) na (number of samples used for each class composite) Return: pixad (8 bpp templates for each class), or null on error

l_int32 pixHaustest ( PIX pix1,
PIX pix2,
PIX pix3,
PIX pix4,
l_float32  delx,
l_float32  dely,
l_int32  maxdiffw,
l_int32  maxdiffh 
)

pixHaustest()

Input: pix1 (new pix, not dilated) pix2 (new pix, dilated) pix3 (exemplar pix, not dilated) pix4 (exemplar pix, dilated) delx (x comp of centroid difference) dely (y comp of centroid difference) maxdiffw (max width difference of pix1 and pix2) maxdiffh (max height difference of pix1 and pix2) Return: 0 (FALSE) if no match, 1 (TRUE) if the new pix is in the same class as the exemplar.

Note: we check first that the two pix are roughly the same size. Only if they meet that criterion do we compare the bitmaps. The Hausdorff is a 2-way check. The centroid difference is used to align the two images to the nearest integer for each of the checks. These check that the dilated image of one contains ALL the pixels of the undilated image of the other. Checks are done in both direction. A single pixel not contained in either direction results in failure of the test.

l_int32 pixRankHaustest ( PIX pix1,
PIX pix2,
PIX pix3,
PIX pix4,
l_float32  delx,
l_float32  dely,
l_int32  maxdiffw,
l_int32  maxdiffh,
l_int32  area1,
l_int32  area3,
l_float32  rank,
l_int32 tab8 
)

pixRankHaustest()

Input: pix1 (new pix, not dilated) pix2 (new pix, dilated) pix3 (exemplar pix, not dilated) pix4 (exemplar pix, dilated) delx (x comp of centroid difference) dely (y comp of centroid difference) maxdiffw (max width difference of pix1 and pix2) maxdiffh (max height difference of pix1 and pix2) area1 (fg pixels in pix1) area3 (fg pixels in pix3) rank (rank value of test, each way) tab8 (table of pixel sums for byte) Return: 0 (FALSE) if no match, 1 (TRUE) if the new pix is in the same class as the exemplar.

Note: we check first that the two pix are roughly the same size. Only if they meet that criterion do we compare the bitmaps. We convert the rank value to a number of pixels by multiplying the rank fraction by the number of pixels in the undilated image. The Hausdorff is a 2-way check. The centroid difference is used to align the two images to the nearest integer for each of the checks. The rank hausdorff checks that the dilated image of one contains the rank fraction of the pixels of the undilated image of the other. Checks are done in both direction. Failure of the test in either direction results in failure of the test.

PIX* pixWordMaskByDilation ( PIX pixs,
l_int32  maxsize,
l_int32 psize 
)

pixWordMaskByDilation()

Input: pixs (1 bpp; typ. at 75 to 150 ppi) maxsize (use 0 for default; not to exceed 14) &size (<optional return>=""> size of optimal horiz Sel) Return: pixd (dilated word mask), or null on error

Notes: (1) For 75 to 150 ppi, the optimal dilation should not exceed 7. This is the default size chosen if maxsize <= 0. (2) To run this on images at resolution between 200 and 300, it is advisable to use a larger maxsize, say between 10 and 14. (3) The best size for dilating to get word masks is optionally returned.


Переменные

const l_int32 JB_ADDED_PIXELS = 6 [static]

const l_int32 MAX_CHAR_COMP_WIDTH = 350 [static]

const l_int32 MAX_COMP_HEIGHT = 120 [static]

const l_int32 MAX_CONN_COMP_WIDTH = 350 [static]

const l_int32 MAX_DIFF_HEIGHT = 2 [static]

const l_int32 MAX_DIFF_WIDTH = 2 [static]

const l_int32 MAX_WORD_COMP_WIDTH = 1000 [static]

int two_by_two_walk[50] [static]

Инициализатор

 {
  0, 0,
  0, 1,
  -1, 0,
  0, -1,
  1, 0,
  -1, 1,
  1, 1,
  -1, -1,
  1, -1,
  0, -2,
  2, 0,
  0, 2,
  -2, 0,
  -1, -2,
  1, -2,
  2, -1,
  2, 1,
  1, 2,
  -1, 2,
  -2, 1,
  -2, -1,
  -2, -2,
  2, -2,
  2, 2,
  -2, 2}


Документация по Leptonica. Последние изменения: Fri Aug 7 20:31:35 2009. Создано системой  doxygen 1.5.9