157 #include <config_auto.h>
162 #include "allheaders.h"
172 l_int32 x, l_int32 *pdely, l_int32 *pwsum);
176 static const l_float32 SetwidthFraction = 0.95;
177 static const l_int32 MaxYShift = 1;
185 static const l_float32 DefaultAlpha2[] = {0.95f, 0.9f};
186 static const l_float32 DefaultAlpha4[] = {0.95f, 0.9f, 0.75f, 0.25f};
228 if (ppixdb) *ppixdb = NULL;
230 return (
BOXA *)ERROR_PTR(
"recog not defined", __func__, NULL);
231 if (!pixs || pixGetDepth(pixs) != 1)
232 return (
BOXA *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
234 return (
BOXA *)ERROR_PTR(
"training not finished", __func__, NULL);
236 return (
BOXA *)ERROR_PTR(
"nlevels != 2 (for now)", __func__, NULL);
238 debug = (ppixdb) ? 1 : 0;
240 return (
BOXA *)ERROR_PTR(
"error making arrays", __func__, NULL);
248 return (
BOXA *)ERROR_PTR(
"error in Viterbi", __func__, NULL);
253 return (
BOXA *)ERROR_PTR(
"error in viterbi", __func__, NULL);
258 return (
BOXA *)ERROR_PTR(
"error in rescoring", __func__, NULL);
302 return ERROR_INT(
"recog not defined", __func__, 1);
303 if (!pixs || pixGetDepth(pixs) != 1)
304 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
306 return ERROR_INT(
"training not finished", __func__, 1);
311 return ERROR_INT(
"averaging of samples failed", __func__, 1);
316 return ERROR_INT(
"pix1 not made", __func__, 1);
322 return ERROR_INT(
"decoder not made", __func__, 1);
359 l_int32 i, j, w1, h1, w2, h2, nx, ycent2, count, maxcount, maxdely;
360 l_int32 sum, moment, dely, shifty;
361 l_int32 *counta, *delya, *ycent1, *arraysum, *arraymoment, *sumtab;
362 NUMA *nasum, *namoment;
363 PIX *pix1, *pix2, *pix3;
367 return ERROR_INT(
"recog not defined", __func__, 1);
369 return ERROR_INT(
"did not defined", __func__, 1);
370 if (index < 0 || index >= did->
narray)
371 return ERROR_INT(
"invalid index", __func__, 1);
379 L_INFO(
"w1 = %d < w2 = %d for index %d\n", __func__, w1, w2, index);
388 counta = did->
counta[index];
389 delya = did->
delya[index];
394 ycent1 = (l_int32 *)LEPT_CALLOC(nx,
sizeof(l_int32));
397 for (i = 0, sum = 0, moment = 0; i < w2; i++) {
399 moment += arraymoment[i];
401 for (i = 0; i < nx - 1; i++) {
402 ycent1[i] = (sum == 0) ? ycent2 : (l_float32)moment / (l_float32)sum;
403 sum += arraysum[w2 + i] - arraysum[i];
404 moment += arraymoment[w2 + i] - arraymoment[i];
406 ycent1[nx - 1] = (sum == 0) ? ycent2 : (l_float32)moment / (l_float32)sum;
417 for (i = 0; i < nx; i++) {
418 shifty = (l_int32)(ycent1[i] - ycent2 + 0.5);
421 for (j = -MaxYShift; j <= MaxYShift; j++) {
427 if (count > maxcount) {
432 counta[i] = maxcount;
441 LEPT_FREE(arraymoment);
481 l_int32 i, w1, w2, h1, xnz, x, narray, minsetw;
482 l_int32 first, templ, xloc, dely, counts, area1;
483 l_int32 besttempl, spacetempl;
484 l_int32 *setw, *didtempl;
486 l_float32 prevscore, matchscore, maxscore, correl;
492 if (ppixdb) *ppixdb = NULL;
494 return ERROR_INT(
"recog not defined", __func__, 1);
496 return ERROR_INT(
"did not defined", __func__, 1);
498 return ERROR_INT(
"did full arrays not made", __func__, 1);
507 for (i = 0; i < narray; i++) {
508 if (setw[i] < minsetw)
512 return ERROR_INT(
"minsetw <= 2; bad templates", __func__, 1);
524 for (x = minsetw; x < w1; x++) {
526 for (i = 0; i < narray; i++) {
527 if (x - setw[i] < 0)
continue;
528 matchscore = didscore[x - setw[i]] +
530 did->
beta[1] * area2[i];
532 maxscore = matchscore;
536 if (matchscore > maxscore) {
537 maxscore = matchscore;
545 prevscore = didscore[x - 1];
546 if (prevscore > maxscore) {
547 maxscore = prevscore;
548 besttempl = spacetempl;
550 didscore[x] = maxscore;
551 didtempl[x] = besttempl;
556 for (x = w1 - 1; x >= 0; x--) {
557 if (didtempl[x] != spacetempl)
break;
559 h1 = pixGetHeight(did->
pixs);
561 if (didtempl[x] == spacetempl) {
566 xloc = x - setw[templ];
568 counts = did->
counta[templ][xloc];
570 correl = ((l_float32)(counts) * counts) /
571 (l_float32)(area2[templ] * area1);
573 w2 = pixGetWidth(pix1);
579 xnz = L_MAX(xloc, 0);
618 l_int32 i, n, sample, x, dely, index;
625 if (ppixdb) *ppixdb = NULL;
627 return ERROR_INT(
"recog not defined", __func__, 1);
629 return ERROR_INT(
"did not defined", __func__, 1);
631 return ERROR_INT(
"did full arrays not made", __func__, 1);
633 return ERROR_INT(
"no elements in path", __func__, 1);
636 for (i = 0; i < n; i++) {
644 &sample, NULL, NULL, NULL);
646 " score = %5.3f\n", text, index, sample, score);
672 l_int32 i, j, n, index, xloc, dely;
675 NUMA *natempl_s, *nasample_s, *nascore_s, *naxloc_s, *nadely_s;
676 PIX *pixs, *pix0, *pix1, *pix2, *pix3, *pix4, *pix5;
680 return (
PIX *)ERROR_PTR(
"recog not defined", __func__, NULL);
682 return (
PIX *)ERROR_PTR(
"did not defined", __func__, NULL);
702 for (i = 0; i < n; i++) {
714 snprintf(textstr,
sizeof(textstr),
"%5.3f", score);
750 return ERROR_INT(
"recog not defined", __func__, 1);
752 return ERROR_INT(
"pixs not defined", __func__, 1);
760 did->
size = pixGetWidth(pixs);
775 did->
setwidth = (l_int32 *)LEPT_CALLOC(did->
narray,
sizeof(l_int32));
776 did->
counta = (l_int32 **)LEPT_CALLOC(did->
narray,
sizeof(l_int32 *));
777 did->
delya = (l_int32 **)LEPT_CALLOC(did->
narray,
sizeof(l_int32 *));
778 did->
beta = (l_float32 *)LEPT_CALLOC(5,
sizeof(l_float32));
779 did->
gamma = (l_float32 *)LEPT_CALLOC(5,
sizeof(l_float32));
782 for (i = 0; i < did->
narray; i++) {
783 did->
counta[i] = (l_int32 *)LEPT_CALLOC(did->
size,
sizeof(l_int32));
784 did->
delya[i] = (l_int32 *)LEPT_CALLOC(did->
size,
sizeof(l_int32));
788 for (i = 0; i < did->
narray; i++) {
790 did->
setwidth[i] = (l_int32)(SetwidthFraction * pixGetWidth(pix1));
817 return ERROR_INT(
"recog not defined", __func__, 1);
819 if ((did = recog->
did) == NULL)
return 0;
821 return ERROR_INT(
"ptr array is null; shouldn't happen!", __func__, 1);
823 for (i = 0; i < did->
narray; i++) {
824 LEPT_FREE(did->
counta[i]);
825 LEPT_FREE(did->
delya[i]);
829 LEPT_FREE(did->
delya);
830 LEPT_FREE(did->
beta);
831 LEPT_FREE(did->
gamma);
868 return ERROR_INT(
"recog not defined", __func__, 0);
869 return (recog->
did) ? 1 : 0;
891 return (
L_RDID *)ERROR_PTR(
"recog not defined", __func__, NULL);
892 if ((did = recog->
did) == NULL)
893 return (
L_RDID *)ERROR_PTR(
"did not defined", __func__, NULL);
895 return (
L_RDID *)ERROR_PTR(
"did array ptrs not defined",
897 for (i = 0; i < did->
narray; i++) {
899 return (
L_RDID *)ERROR_PTR(
"did arrays not defined",
934 l_int32 w1, h1, w2, h2;
935 PIX *pix1, *pix2, *pixt;
938 if (pdely) *pdely = 0;
939 if (pwsum) *pwsum = 0;
940 if (!pdely || !pwsum)
941 return ERROR_INT(
"&dely and &wsum not both defined", __func__, 1);
943 return ERROR_INT(
"recog not defined", __func__, 1);
945 return ERROR_INT(
"did not defined", __func__, 1);
946 if (index < 0 || index >= did->
narray)
947 return ERROR_INT(
"invalid index", __func__, 1);
951 return ERROR_INT(
"invalid x position", __func__, 1);
956 L_INFO(
"template %d too small\n", __func__, index);
961 *pdely = did->
delya[index][x];
998 return ERROR_INT(
"recog not defined", __func__, 1);
1000 return ERROR_INT(
"did not defined", __func__, 1);
1003 else if (nlevels == 4)
1006 return ERROR_INT(
"nlevels not 2 or 4", __func__, 1);
1008 for (i = 1; i < nlevels; i++) {
1009 did->
beta[i] = log((1.0 - da[i]) / da[0]);
1010 did->
gamma[i] = log(da[0] * da[i] / ((1.0 - da[0]) * (1.0 - da[i])));
1042 return ERROR_INT(
"recog not defined", __func__, 1);
1044 return ERROR_INT(
"did not defined", __func__, 1);
1045 if ((rch = recog->
rch) == NULL)
1046 return ERROR_INT(
"rch not defined", __func__, 1);
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()
l_ok boxGetGeometry(const BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
BOXA * boxaCopy(BOXA *boxa, l_int32 copyflag)
boxaCopy()
void boxDestroy(BOX **pbox)
boxDestroy()
l_ok boxaWriteStderr(BOXA *boxa)
boxaWriteStderr()
l_ok boxaAddBox(BOXA *boxa, BOX *box, l_int32 copyflag)
boxaAddBox()
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
BOXA * boxaCreate(l_int32 n)
boxaCreate()
PIX * pixErodeBrick(PIX *pixd, PIX *pixs, l_int32 hsize, l_int32 vsize)
pixErodeBrick()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
l_ok numaWriteStderr(NUMA *na)
numaWriteStderr()
NUMA * numaCreate(l_int32 n)
numaCreate()
void numaDestroy(NUMA **pna)
numaDestroy()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
l_int32 * numaGetIArray(NUMA *na)
numaGetIArray()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
PIX * pixClone(PIX *pixs)
pixClone()
l_ok pixClearAll(PIX *pix)
pixClearAll()
PIX * pixAddBorderGeneral(PIX *pixs, l_int32 left, l_int32 right, l_int32 top, l_int32 bot, l_uint32 val)
pixAddBorderGeneral()
l_ok pixPaintThroughMask(PIX *pixd, PIX *pixm, l_int32 x, l_int32 y, l_uint32 val)
pixPaintThroughMask()
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
NUMA * pixCountPixelsByColumn(PIX *pix)
pixCountPixelsByColumn()
NUMA * pixGetMomentByColumn(PIX *pix, l_int32 order)
pixGetMomentByColumn()
PIX * pixXor(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixXor()
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
PIX * pixaaGetPix(PIXAA *paa, l_int32 index, l_int32 ipix, l_int32 accessflag)
pixaaGetPix()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
PIX * pixaDisplayTiledInRows(PIXA *pixa, l_int32 outdepth, l_int32 maxwidth, l_float32 scalefactor, l_int32 background, l_int32 spacing, l_int32 border)
pixaDisplayTiledInRows()
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
l_ok ptaGetIPt(PTA *pta, l_int32 index, l_int32 *px, l_int32 *py)
ptaGetIPt()
static l_int32 recogRunViterbi(L_RECOG *recog, PIX **ppixdb)
recogRunViterbi()
static l_int32 recogTransferRchToDid(L_RECOG *recog, l_int32 x, l_int32 y)
recogTransferRchToDid()
l_ok recogDestroyDid(L_RECOG *recog)
recogDestroyDid()
BOXA * recogDecode(L_RECOG *recog, PIX *pixs, l_int32 nlevels, PIX **ppixdb)
recogDecode()
static PIX * recogShowPath(L_RECOG *recog, l_int32 select)
recogShowPath()
l_ok recogCreateDid(L_RECOG *recog, PIX *pixs)
recogCreateDid()
l_ok recogSetChannelParams(L_RECOG *recog, l_int32 nlevels)
recogSetChannelParams()
L_RDID * recogGetDid(L_RECOG *recog)
recogGetDid()
static l_int32 recogGetWindowedArea(L_RECOG *recog, l_int32 index, l_int32 x, l_int32 *pdely, l_int32 *pwsum)
recogGetWindowedArea()
static l_int32 recogRescoreDidResult(L_RECOG *recog, PIX **ppixdb)
recogRescoreDidResult()
l_int32 recogDidExists(L_RECOG *recog)
recogDidExists()
static l_int32 recogPrepareForDecoding(L_RECOG *recog, PIX *pixs, l_int32 debug)
recogPrepareForDecoding()
static l_int32 recogMakeDecodingArray(L_RECOG *recog, l_int32 index, l_int32 debug)
recogMakeDecodingArray()
l_ok rchExtract(L_RCH *rch, l_int32 *pindex, l_float32 *pscore, char **ptext, l_int32 *psample, l_int32 *pxloc, l_int32 *pyloc, l_int32 *pwidth)
rchExtract()
l_ok recogIdentifyPix(L_RECOG *recog, PIX *pixs, PIX **ppixdb)
recogIdentifyPix()
PIX * recogProcessToIdentify(L_RECOG *recog, PIX *pixs, l_int32 pad)
recogProcessToIdentify()
l_int32 recogAverageSamples(L_RECOG *recog, l_int32 debug)
recogAverageSamples()
l_ok pixRasterop(PIX *pixd, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, PIX *pixs, l_int32 sx, l_int32 sy)
pixRasterop()
PIX * pixScale(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScale()
PIX * pixAddTextlines(PIX *pixs, L_BMF *bmf, const char *textstr, l_uint32 val, l_int32 location)
pixAddTextlines()
void lept_stderr(const char *fmt,...)
lept_stderr()