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

Функции

static l_int32 recogaExtendArray (L_RECOGA *recoga)
static l_int32 recogAddCharstrLabels (L_RECOG *recog)
static l_int32 recogAddAllSamples (L_RECOG *recog, PIXAA *paa, l_int32 debug)
L_RECOGArecogaCreateFromRecog (L_RECOG *recog)
L_RECOGArecogaCreateFromPixaa (PIXAA *paa, l_int32 scalew, l_int32 scaleh, l_int32 templ_type, l_int32 threshold, l_int32 maxyshift)
L_RECOGArecogaCreate (l_int32 n)
void recogaDestroy (L_RECOGA **precoga)
l_int32 recogaAddRecog (L_RECOGA *recoga, L_RECOG *recog)
l_int32 recogReplaceInRecoga (L_RECOG **precog1, L_RECOG *recog2)
L_RECOGrecogaGetRecog (L_RECOGA *recoga, l_int32 index)
l_int32 recogaGetCount (L_RECOGA *recoga)
l_int32 recogGetCount (L_RECOG *recog)
l_int32 recogGetIndex (L_RECOG *recog, l_int32 *pindex)
L_RECOGArecogGetParent (L_RECOG *recog)
l_int32 recogSetBootflag (L_RECOG *recog)
L_RECOGrecogCreateFromRecog (L_RECOG *recs, l_int32 scalew, l_int32 scaleh, l_int32 templ_type, l_int32 threshold, l_int32 maxyshift)
L_RECOGrecogCreateFromPixa (PIXA *pixa, l_int32 scalew, l_int32 scaleh, l_int32 templ_type, l_int32 threshold, l_int32 maxyshift)
L_RECOGrecogCreate (l_int32 scalew, l_int32 scaleh, l_int32 templ_type, l_int32 threshold, l_int32 maxyshift)
void recogDestroy (L_RECOG **precog)
l_int32 recogAppend (L_RECOG *recog1, L_RECOG *recog2)
l_int32 recogGetClassIndex (L_RECOG *recog, l_int32 val, char *text, l_int32 *pindex)
l_int32 recogStringToIndex (L_RECOG *recog, char *text, l_int32 *pindex)
l_int32 recogGetClassString (L_RECOG *recog, l_int32 index, char **pcharstr)
l_int32 l_convertCharstrToInt (const char *str, l_int32 *pval)
L_RECOGArecogaRead (const char *filename)
L_RECOGArecogaReadStream (FILE *fp)
l_int32 recogaWrite (const char *filename, L_RECOGA *recoga)
l_int32 recogaWriteStream (FILE *fp, L_RECOGA *recoga, const char *filename)
l_int32 recogaWritePixaa (const char *filename, L_RECOGA *recoga)
L_RECOGrecogRead (const char *filename)
L_RECOGrecogReadStream (FILE *fp)
l_int32 recogWrite (const char *filename, L_RECOG *recog)
l_int32 recogWriteStream (FILE *fp, L_RECOG *recog, const char *filename)
l_int32 recogWritePixa (const char *filename, L_RECOG *recog)

Переменные

static const l_int32 INITIAL_PTR_ARRAYSIZE = 20
static const l_int32 MAX_EXAMPLES_IN_CLASS = 256
static l_float32 DEFAULT_ASPERITY_FRACT = 0.25

Функции

l_int32 l_convertCharstrToInt ( const char *  str,
l_int32 pval 
)

l_convertCharstrToInt()

Input: str (input string representing one UTF-8 character; not more than 4 bytes) &val (<return> integer value for the input. Think of it as a 1-to-1 hash code.) Return: 0 if OK, 1 on error

l_int32 recogaAddRecog ( L_RECOGA recoga,
L_RECOG recog 
)

recogaAddRecog()

Input: recoga recog (to be added and owned by the recoga; not a copy) Return: recoga, or null on error

recogaCreate()

Input: n (initial number of recog ptrs) Return: recoga, or null on error

L_RECOGA* recogaCreateFromPixaa ( PIXAA paa,
l_int32  scalew,
l_int32  scaleh,
l_int32  templ_type,
l_int32  threshold,
l_int32  maxyshift 
)

recogaCreateFromPixaa()

Input: paa (of labelled, 1 bpp images) scalew (scale all widths to this; use 0 for no scaling) scaleh (scale all heights to this; use 0 for no scaling) templ_type (L_USE_AVERAGE or L_USE_ALL) threshold (for binarization; typically ~128) maxyshift (from nominal centroid alignment; typically 0 or 1) Return: recoga, or null on error

Notes: (1) This is a convenience function for training from labelled data. (2) Each pixa in the paa is a set of labelled data that is used to train a recognizer (e.g., for a set of characters in a font). Each image example in the pixa is put into a class in its recognizer, defined by its character label. All examples in the same class should be similar. (3) The pixaa can be written by recogaWritePixaa(), and must contain the unscaled bitmaps used for training.

recogaCreateFromRecog()

Input: recog Return: recoga, or null on error

Notes: (1) This is a convenience function for making a recoga after you have a recog. The recog is owned by the recoga. (2) For splitting connected components, the input recog must be from the material to be identified, and not a generic bootstrap recog. Those can be added later.

static l_int32 recogAddAllSamples ( L_RECOG recog,
PIXAA paa,
l_int32  debug 
) [static]

recogAddAllSamples()

Input: recog paa (pixaa from previously trained recog) debug Return: 0 if OK, 1 on error

Notes: (1) This is used with the serialization routine recogRead(), where each pixa in the pixaa represents a set of characters in a different class. Two different pixa may represent characters with the same label. Before calling this function, we verify that the number of character classes, given by the setsize field in recog, equals the number of pixa in the paa. The character labels for each set are in the sa_text field.

static l_int32 recogAddCharstrLabels ( L_RECOG recog) [static]

recogAddCharstrLabels()

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

void recogaDestroy ( L_RECOGA **  precoga)

recogaDestroy()

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

Notes: (1) If a recog has a parent, the parent owns it. To destroy a recog, it must first be "orphaned".

static l_int32 recogaExtendArray ( L_RECOGA recoga) [static]

recogaExtendArray()

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

recogaGetCount()

Input: recoga Return: count of recog in array; 0 if no recog or on error

L_RECOG* recogaGetRecog ( L_RECOGA recoga,
l_int32  index 
)

recogaGetRecog()

Input: recoga index (to the index-th recog) Return: recog, or null on error

Notes: (1) This returns a ptr to the recog, which is still owned by the recoga. Do not destroy it.

l_int32 recogAppend ( L_RECOG recog1,
L_RECOG recog2 
)

recogAppend()

Input: recog1 recog2 (gets added to recog1) Return: 0 if OK, 1 on error

Notes: (1) This is used to make a training recognizer from more than one trained recognizer source. It should only be used when the bitmaps for corresponding character classes are very similar. That constraint does not arise when the character classes are disjoint; e.g., if recog1 is digits and recog2 is alphabetical. (2) This is done by appending recog2 to recog1. Averages are computed for each recognizer, if necessary, before appending. (3) Non-array fields are combined using the appropriate min and max.

L_RECOGA* recogaRead ( const char *  filename)

recogaRead()

Input: filename Return: recoga, or null on error

Notes: (1) This allows serialization of an array of recognizers, each of which can be used for different fonts, font styles, etc.

L_RECOGA* recogaReadStream ( FILE *  fp)

recogaReadStream()

Input: stream Return: recog, or null on error

l_int32 recogaWrite ( const char *  filename,
L_RECOGA recoga 
)

recogaWrite()

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

l_int32 recogaWritePixaa ( const char *  filename,
L_RECOGA recoga 
)

recogaWritePixaa()

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

Notes: (1) For each recognizer, this generates a pixa of all the unscaled images. They are combined into a pixaa for the set of recognizers. Each pix has has its character string in the pix text field. (2) As a side-effect, the character class label is written into each pix in recog.

l_int32 recogaWriteStream ( FILE *  fp,
L_RECOGA recoga,
const char *  filename 
)

recogaWriteStream()

Input: stream (opened for "wb") recoga filename (output serialized filename; embedded in file) Return: 0 if OK, 1 on error

L_RECOG* recogCreate ( l_int32  scalew,
l_int32  scaleh,
l_int32  templ_type,
l_int32  threshold,
l_int32  maxyshift 
)

recogCreate()

Input: scalew (scale all widths to this; use 0 for no scaling) scaleh (scale all heights to this; use 0 for no scaling) templ_type (L_USE_AVERAGE or L_USE_ALL) threshold (for binarization; typically ~128) maxyshift (from nominal centroid alignment; typically 0 or 1) Return: recog, or null on error

Notes: (1) For a set trained on one font, such as numbers in a book, it is sensible to set scalew = scaleh = 0. (2) For a mixed training set, scaling to a fixed height, such as 32 pixels, but leaving the width unscaled, is effective. (3) The storage for most of the arrays is allocated when training is finished.

L_RECOG* recogCreateFromPixa ( PIXA pixa,
l_int32  scalew,
l_int32  scaleh,
l_int32  templ_type,
l_int32  threshold,
l_int32  maxyshift 
)

recogCreateFromPixa()

Input: pixa (of labelled, 1 bpp images) scalew (scale all widths to this; use 0 for no scaling) scaleh (scale all heights to this; use 0 for no scaling) templ_type (L_USE_AVERAGE or L_USE_ALL) threshold (for binarization; typically ~128) maxyshift (from nominal centroid alignment; typically 0 or 1) Return: recog, or null on error

Notes: (1) This is a convenience function for training from labelled data. The pixa can be read from file. (2) The pixa should contain the unscaled bitmaps used for training. (3) The characters here should work as a single "font", because each image example is put into a class defined by its character label. All examples in the same class should be similar.

L_RECOG* recogCreateFromRecog ( L_RECOG recs,
l_int32  scalew,
l_int32  scaleh,
l_int32  templ_type,
l_int32  threshold,
l_int32  maxyshift 
)

recogCreateFromRecog()

Input: recs (source recog with arbitrary input parameters) scalew (scale all widths to this; use 0 for no scaling) scaleh (scale all heights to this; use 0 for no scaling) templ_type (L_USE_AVERAGE or L_USE_ALL) threshold (for binarization; typically ~128) maxyshift (from nominal centroid alignment; typically 0 or 1) Return: recd, or null on error

Notes: (1) This is a convenience function that generates a recog using the unscaled training data in an existing recog.

void recogDestroy ( L_RECOG **  precog)

recogDestroy()

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

Notes: (1) If a recog has a parent, the parent owns it. A recogDestroy() will fail if there is a parent.

l_int32 recogGetClassIndex ( L_RECOG recog,
l_int32  val,
char *  text,
l_int32 pindex 
)

recogGetClassIndex()

Input: recog (with LUT's pre-computed) val (integer value; can be up to 3 bytes for UTF-8) text (text from which was derived; used if not found) &index (<return> index into dna_tochar) Return: 0 if found; 1 if not found and added; 2 on error.

Notes: (1) This is used during training. It searches the dna character array for . If not found, it increments the setsize by 1, augmenting both the index and text arrays. (2) Returns the index in &index, except on error. (3) Caller must check the function return value.

l_int32 recogGetClassString ( L_RECOG recog,
l_int32  index,
char **  pcharstr 
)

recogGetClassString()

Input: recog index (into array of char types) &charstr (<return> string representation; returns an empty string on error) Return: 0 if found, 1 on error

Notes: (1) Extracts a copy of the string from sa_text, which the caller must free. (2) Caller must check the function return value.

recogGetCount()

Input: recog Return: count of classes in recog; 0 if no recog or on error

l_int32 recogGetIndex ( L_RECOG recog,
l_int32 pindex 
)

recogGetIndex()

Input: recog &index (into the parent recoga; -1 if no parent) Return: 0 if OK, 1 on error

recogGetParent()

Input: recog Return: recoga (back-pointer to parent); can be null

L_RECOG* recogRead ( const char *  filename)

recogRead()

Input: filename Return: recog, or null on error

Notes: (1) Serialization can be applied to any recognizer, including one with more than one "font". That is, it can have multiple character classes with the same character set description, where each of those classes contains characters that are very similar in size and shape. Each pixa in the serialized pixaa contains images for a single character class.

L_RECOG* recogReadStream ( FILE *  fp)

recogReadStream()

Input: stream Return: recog, or null on error

l_int32 recogReplaceInRecoga ( L_RECOG **  precog1,
L_RECOG recog2 
)

recogReplaceInRecoga()

Input: &recog1 (old recog, to be destroyed) recog2 (new recog, to be inserted in place of ) Return: 0 if OK, 1 on error

Notes: (1) This always destroys recog1. (2) If recog1 belongs to a recoga, this inserts recog2 into the slot that recog1 previously occupied.

recogSetBootflag()

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

Notes: (1) This must be set for any bootstrap recog, where the samples are not from the media being identified. (2) It is used to enforce scaled bitmaps for identification, and to prevent the recog from being used to split touching characters (which requires unscaled samples from the material being identified).

l_int32 recogStringToIndex ( L_RECOG recog,
char *  text,
l_int32 pindex 
)

recogStringToIndex()

Input: recog text (text string for some class) &index (<return> index for that class; -1 if not found) Return: 0 if OK, 1 on error (not finding the string is an error)

l_int32 recogWrite ( const char *  filename,
L_RECOG recog 
)

recogWrite()

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

l_int32 recogWritePixa ( const char *  filename,
L_RECOG recog 
)

recogWritePixa()

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

Notes: (1) This generates a pixa of all the unscaled images in the recognizer, where each one has its character string in the pix text field, by flattening pixaa_u to a pixa. (2) As a side-effect, the character class label is written into each pix in recog.

l_int32 recogWriteStream ( FILE *  fp,
L_RECOG recog,
const char *  filename 
)

recogWriteStream()

Input: stream (opened for "wb") recog filename (output serialized filename; embedded in file) Return: 0 if OK, 1 on error


Переменные

const l_int32 INITIAL_PTR_ARRAYSIZE = 20 [static]
const l_int32 MAX_EXAMPLES_IN_CLASS = 256 [static]