Leptonica  1.54
Файл jbclass.c
#include <string.h>
#include <math.h>
#include "allheaders.h"

Классы

struct  JbFindTemplatesState
 

Макросы

#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)
 
l_int32 pixWordMaskByDilation (PIX *pixs, l_int32 maxdil, PIX **ppixm, l_int32 *psize)
 
l_int32 pixWordBoxesByDilation (PIX *pixs, l_int32 maxdil, l_int32 minwidth, l_int32 minheight, l_int32 maxwidth, l_int32 maxheight, BOXA **pboxa, 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 L_BUF_SIZE = 512
 
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 const l_int32 MAX_ALLOWED_DILATION = 25
 
static int two_by_two_walk [50]
 

Макросы

◆ DEBUG_CORRELATION_SCORE

#define DEBUG_CORRELATION_SCORE   0

◆ DEBUG_PLOT_CC

#define DEBUG_PLOT_CC   0

Типы

◆ JBFINDCTX

Функции

◆ finalPositioningForAlignment()

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

◆ findSimilarSizedTemplatesDestroy()

static void findSimilarSizedTemplatesDestroy ( JBFINDCTX **  pcontext)
static

◆ findSimilarSizedTemplatesInit()

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

findSimilarSizedTemplatesInit()

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

◆ findSimilarSizedTemplatesNext()

static l_int32 findSimilarSizedTemplatesNext ( JBFINDCTX state)
static

findSimilarSizedTemplatesNext()

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

We have a dna hash table that maps 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 a well-matching template quickly. So we keep the context for this walk in an explictit state structure, and this function acts like a generator.

◆ jbAccumulateComposites()

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

◆ jbAddPage()

l_int32 jbAddPage ( JBCLASSER classer,
PIX pixs 
)

jbAddPage()

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

◆ jbAddPageComponents()

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.

◆ jbAddPages()

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.

◆ jbClasserCreate()

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

◆ jbClasserDestroy()

void jbClasserDestroy ( JBCLASSER **  pclasser)

◆ jbClassifyCorrelation()

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

◆ jbClassifyRankHaus()

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

◆ jbCorrelationInit()

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.98])
        weightfactor (corrects thresh for thick characters [0.0 - 1.0])
Return: jbclasser if OK; NULL on error

Notes: (1) For scanned text, suggested input values are: thresh ~ [0.8 - 0.85] weightfactor ~ [0.5 - 0.6] (2) For electronically generated fonts (e.g., rasterized pdf), a very high thresh (e.g., 0.95) will not cause a significant increase in the number of classes.

◆ jbCorrelationInitInternal()

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

◆ jbCorrelationInitWithoutComponents()

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.

◆ jbDataDestroy()

void jbDataDestroy ( JBDATA **  pdata)

◆ jbDataRead()

JBDATA* jbDataRead ( const char *  rootname)

jbDataRead()

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

◆ jbDataRender()

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

◆ jbDataSave()

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,

◆ jbDataWrite()

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.

◆ jbGetComponents()

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

◆ jbGetLLCorners()

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.

◆ jbGetULCorners()

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.

◆ jbRankHausInit()

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

◆ jbTemplatesFromComposites()

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

◆ pixHaustest()

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.

◆ pixRankHaustest()

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.

◆ pixWordBoxesByDilation()

l_int32 pixWordBoxesByDilation ( PIX pixs,
l_int32  maxdil,
l_int32  minwidth,
l_int32  minheight,
l_int32  maxwidth,
l_int32  maxheight,
BOXA **  pboxa,
l_int32 psize 
)

pixWordBoxesByDilation()

Input:  pixs (1 bpp; typ. at 75 to 150 ppi)
        maxdil (maximum dilation; 0 for default; warning if > 20)
        minwidth, minheight (of saved components; smaller are discarded)
        maxwidth, maxheight (of saved components; larger are discarded)
        &boxa (<return> dilated word mask)
        &size (<optional return> size of optimal horiz Sel)
Return: 0 if OK, 1 on error

Notes: (1) Returns a pruned set of word boxes. (2) See pixWordMaskByDilation().

◆ pixWordMaskByDilation()

l_int32 pixWordMaskByDilation ( PIX pixs,
l_int32  maxdil,
PIX **  ppixm,
l_int32 psize 
)

pixWordMaskByDilation()

Input:  pixs (1 bpp; typ. at 75 to 150 ppi)
        maxdil (maximum dilation; 0 for default; warning if > 20)
        &mask (<optional return> dilated word mask)
        &size (<optional return> size of optimal horiz Sel)
Return: 0 if OK, 1 on error

Notes: (1) This gives a crude estimate of the word masks. See pixWordBoxesByDilation() for further filtering of the word boxes. (2) For 75 to 150 ppi, the optimal dilation will be between 5 and 11. For 200 to 300 ppi, it is advisable to use a larger value for @maxdil, say between 10 and 20. Setting maxdil <= 0 results in a default dilation of 16. (3) The best size for dilating to get word masks is optionally returned.

Переменные

◆ JB_ADDED_PIXELS

const l_int32 JB_ADDED_PIXELS = 6
static

◆ L_BUF_SIZE

const l_int32 L_BUF_SIZE = 512
static

◆ MAX_ALLOWED_DILATION

const l_int32 MAX_ALLOWED_DILATION = 25
static

◆ MAX_CHAR_COMP_WIDTH

const l_int32 MAX_CHAR_COMP_WIDTH = 350
static

◆ MAX_COMP_HEIGHT

const l_int32 MAX_COMP_HEIGHT = 120
static

◆ MAX_CONN_COMP_WIDTH

const l_int32 MAX_CONN_COMP_WIDTH = 350
static

◆ MAX_DIFF_HEIGHT

const l_int32 MAX_DIFF_HEIGHT = 2
static

◆ MAX_DIFF_WIDTH

const l_int32 MAX_DIFF_WIDTH = 2
static

◆ MAX_WORD_COMP_WIDTH

const l_int32 MAX_WORD_COMP_WIDTH = 1000
static

◆ two_by_two_walk

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}