158 #include "allheaders.h" 168 l_int32 x, l_int32 *pdely, l_int32 *pwsum);
172 static const l_float32 SetwidthFraction = 0.95;
173 static const l_int32 MaxYShift = 1;
181 static const l_float32 DefaultAlpha2[] = {0.95f, 0.9f};
182 static const l_float32 DefaultAlpha4[] = {0.95f, 0.9f, 0.75f, 0.25f};
224 PROCNAME(
"recogDecode");
226 if (ppixdb) *ppixdb = NULL;
228 return (
BOXA *)ERROR_PTR(
"recog not defined", procName, NULL);
229 if (!
pixs || pixGetDepth(
pixs) != 1)
230 return (
BOXA *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
232 return (
BOXA *)ERROR_PTR(
"training not finished", procName, NULL);
234 return (
BOXA *)ERROR_PTR(
"nlevels != 2 (for now)", procName, NULL);
236 debug = (ppixdb) ? 1 : 0;
238 return (
BOXA *)ERROR_PTR(
"error making arrays", procName, NULL);
246 return (
BOXA *)ERROR_PTR(
"error in Viterbi", procName, NULL);
251 return (
BOXA *)ERROR_PTR(
"error in viterbi", procName, NULL);
256 return (
BOXA *)ERROR_PTR(
"error in rescoring", procName, NULL);
299 PROCNAME(
"recogPrepareForDecoding");
302 return ERROR_INT(
"recog not defined", procName, 1);
303 if (!
pixs || pixGetDepth(
pixs) != 1)
304 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
306 return ERROR_INT(
"training not finished", procName, 1);
313 return ERROR_INT(
"pix1 not made", procName, 1);
319 return ERROR_INT(
"decoder not made", procName, 1);
356 l_int32 i, j, w1, h1, w2, h2, nx, ycent2, count, maxcount, maxdely;
357 l_int32 sum, moment, dely, shifty;
358 l_int32 *
counta, *
delya, *ycent1, *arraysum, *arraymoment, *sumtab;
360 PIX *pix1, *pix2, *pix3;
363 PROCNAME(
"recogMakeDecodingArray");
366 return ERROR_INT(
"recog not defined", procName, 1);
368 return ERROR_INT(
"did not defined", procName, 1);
369 if (index < 0 || index >= did->
narray)
370 return ERROR_INT(
"invalid index", procName, 1);
378 L_INFO(
"w1 = %d < w2 = %d for index %d\n", procName, w1, w2, index);
393 ycent1 = (l_int32 *)LEPT_CALLOC(nx,
sizeof(l_int32));
396 for (i = 0, sum = 0, moment = 0; i < w2; i++) {
398 moment += arraymoment[i];
400 for (i = 0; i < nx - 1; i++) {
401 ycent1[i] = (sum == 0) ? ycent2 : (l_float32)moment / (l_float32)sum;
402 sum += arraysum[w2 + i] - arraysum[i];
403 moment += arraymoment[w2 + i] - arraymoment[i];
405 ycent1[nx - 1] = (sum == 0) ? ycent2 : (l_float32)moment / (l_float32)sum;
416 for (i = 0; i < nx; i++) {
417 shifty = (l_int32)(ycent1[i] - ycent2 + 0.5);
420 for (j = -MaxYShift; j <= MaxYShift; j++) {
426 if (count > maxcount) {
440 LEPT_FREE(arraymoment);
480 l_int32 i, w1, w2, h1, xnz, x,
narray, minsetw;
481 l_int32 first, templ, xloc, dely, counts, area1;
482 l_int32 besttempl, spacetempl;
483 l_int32 *setw, *didtempl;
485 l_float32 prevscore, matchscore, maxscore, correl;
491 PROCNAME(
"recogRunViterbi");
493 if (ppixdb) *ppixdb = NULL;
495 return ERROR_INT(
"recog not defined", procName, 1);
497 return ERROR_INT(
"did not defined", procName, 1);
499 return ERROR_INT(
"did full arrays not made", procName, 1);
508 for (i = 0; i <
narray; i++) {
509 if (setw[i] < minsetw)
513 return ERROR_INT(
"minsetw <= 2; bad templates", procName, 1);
525 for (x = minsetw; x < w1; x++) {
527 for (i = 0; i <
narray; i++) {
528 if (x - setw[i] < 0)
continue;
529 matchscore = didscore[x - setw[i]] +
531 did->
beta[1] * area2[i];
533 maxscore = matchscore;
537 if (matchscore > maxscore) {
538 maxscore = matchscore;
546 prevscore = didscore[x - 1];
547 if (prevscore > maxscore) {
548 maxscore = prevscore;
549 besttempl = spacetempl;
551 didscore[x] = maxscore;
552 didtempl[x] = besttempl;
557 for (x = w1 - 1; x >= 0; x--) {
558 if (didtempl[x] != spacetempl)
break;
560 h1 = pixGetHeight(did->
pixs);
562 if (didtempl[x] == spacetempl) {
567 xloc = x - setw[templ];
569 counts = did->
counta[templ][xloc];
571 correl = ((l_float32)(counts) * counts) /
572 (l_float32)(area2[templ] * area1);
574 w2 = pixGetWidth(pix1);
580 xnz = L_MAX(xloc, 0);
619 l_int32 i, n, sample, x, dely, index;
626 PROCNAME(
"recogRescoreDidResult");
628 if (ppixdb) *ppixdb = NULL;
630 return ERROR_INT(
"recog not defined", procName, 1);
632 return ERROR_INT(
"did not defined", procName, 1);
634 return ERROR_INT(
"did full arrays not made", procName, 1);
636 return ERROR_INT(
"no elements in path", procName, 1);
639 for (i = 0; i < n; i++) {
647 &sample, NULL, NULL, NULL);
648 fprintf(stderr,
"text = %s, index = %d, sample = %d," 649 " score = %5.3f\n", text, index, sample, score);
675 l_int32 i, j, n, index, xloc, dely;
678 NUMA *natempl_s, *nasample_s, *nascore_s, *naxloc_s, *nadely_s;
679 PIX *
pixs, *pix0, *pix1, *pix2, *pix3, *pix4, *pix5;
682 PROCNAME(
"recogShowPath");
685 return (
PIX *)ERROR_PTR(
"recog not defined", procName, NULL);
687 return (
PIX *)ERROR_PTR(
"did not defined", procName, NULL);
707 for (i = 0; i < n; i++) {
719 snprintf(textstr,
sizeof(textstr),
"%5.3f", score);
754 PROCNAME(
"recogCreateDid");
757 return ERROR_INT(
"recog not defined", procName, 1);
759 return ERROR_INT(
"pixs not defined", procName, 1);
782 did->
setwidth = (l_int32 *)LEPT_CALLOC(did->
narray,
sizeof(l_int32));
783 did->
counta = (l_int32 **)LEPT_CALLOC(did->
narray,
sizeof(l_int32 *));
784 did->
delya = (l_int32 **)LEPT_CALLOC(did->
narray,
sizeof(l_int32 *));
785 did->
beta = (l_float32 *)LEPT_CALLOC(5,
sizeof(l_float32));
786 did->
gamma = (l_float32 *)LEPT_CALLOC(5,
sizeof(l_float32));
789 for (i = 0; i < did->
narray; i++) {
790 did->
counta[i] = (l_int32 *)LEPT_CALLOC(did->
size,
sizeof(l_int32));
791 did->
delya[i] = (l_int32 *)LEPT_CALLOC(did->
size,
sizeof(l_int32));
795 for (i = 0; i < did->
narray; i++) {
797 did->
setwidth[i] = (l_int32)(SetwidthFraction * pixGetWidth(pix1));
823 PROCNAME(
"recogDestroyDid");
826 return ERROR_INT(
"recog not defined", procName, 1);
828 if ((did = recog->
did) == NULL)
return 0;
830 return ERROR_INT(
"ptr array is null; shouldn't happen!", procName, 1);
832 for (i = 0; i < did->
narray; i++) {
833 LEPT_FREE(did->
counta[i]);
834 LEPT_FREE(did->
delya[i]);
838 LEPT_FREE(did->
delya);
839 LEPT_FREE(did->
beta);
840 LEPT_FREE(did->
gamma);
876 PROCNAME(
"recogDidExists");
879 return ERROR_INT(
"recog not defined", procName, 0);
880 return (recog->
did) ? 1 : 0;
901 PROCNAME(
"recogGetDid");
904 return (
L_RDID *)ERROR_PTR(
"recog not defined", procName, NULL);
905 if ((did = recog->
did) == NULL)
906 return (
L_RDID *)ERROR_PTR(
"did not defined", procName, NULL);
908 return (
L_RDID *)ERROR_PTR(
"did array ptrs not defined",
910 for (i = 0; i < did->
narray; i++) {
912 return (
L_RDID *)ERROR_PTR(
"did arrays not defined",
947 l_int32 w1, h1, w2, h2;
948 PIX *pix1, *pix2, *pixt;
951 PROCNAME(
"recogGetWindowedArea");
953 if (pdely) *pdely = 0;
954 if (pwsum) *pwsum = 0;
955 if (!pdely || !pwsum)
956 return ERROR_INT(
"&dely and &wsum not both defined", procName, 1);
958 return ERROR_INT(
"recog not defined", procName, 1);
960 return ERROR_INT(
"did not defined", procName, 1);
961 if (index < 0 || index >= did->
narray)
962 return ERROR_INT(
"invalid index", procName, 1);
966 return ERROR_INT(
"invalid x position", procName, 1);
971 L_INFO(
"template %d too small\n", procName, index);
976 *pdely = did->
delya[index][x];
1009 const l_float32 *da;
1012 PROCNAME(
"recogSetChannelParams");
1015 return ERROR_INT(
"recog not defined", procName, 1);
1017 return ERROR_INT(
"did not defined", procName, 1);
1020 else if (nlevels == 4)
1023 return ERROR_INT(
"nlevels not 2 or 4", procName, 1);
1025 for (i = 1; i < nlevels; i++) {
1026 did->
beta[i] = log((1.0 - da[i]) / da[0]);
1027 did->
gamma[i] = log(da[0] * da[i] / ((1.0 - da[0]) * (1.0 - da[i])));
1058 PROCNAME(
"recogTransferRchToDid");
1061 return ERROR_INT(
"recog not defined", procName, 1);
1063 return ERROR_INT(
"did not defined", procName, 1);
1064 if ((rch = recog->
rch) == NULL)
1065 return ERROR_INT(
"rch not defined", procName, 1);
l_ok recogDestroyDid(L_RECOG *recog)
recogDestroyDid()
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
NUMA * pixCountPixelsByColumn(PIX *pix)
pixCountPixelsByColumn()
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
l_ok recogSetChannelParams(L_RECOG *recog, l_int32 nlevels)
recogSetChannelParams()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
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()
l_ok boxaWriteStream(FILE *fp, BOXA *boxa)
boxaWriteStream()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
L_RDID * recogGetDid(L_RECOG *recog)
recogGetDid()
BOXA * recogDecode(L_RECOG *recog, PIX *pixs, l_int32 nlevels, PIX **ppixdb)
recogDecode()
BOXA * boxaCopy(BOXA *boxa, l_int32 copyflag)
boxaCopy()
l_ok numaWriteStream(FILE *fp, NUMA *na)
numaWriteStream()
NUMA * numaCreate(l_int32 n)
numaCreate()
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
PIX * pixaaGetPix(PIXAA *paa, l_int32 index, l_int32 ipix, l_int32 accessflag)
pixaaGetPix()
l_ok pixPaintThroughMask(PIX *pixd, PIX *pixm, l_int32 x, l_int32 y, l_uint32 val)
pixPaintThroughMask()
l_int32 * numaGetIArray(NUMA *na)
numaGetIArray()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
l_ok recogIdentifyPix(L_RECOG *recog, PIX *pixs, PIX **ppixdb)
recogIdentifyPix()
PIX * pixXor(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixXor()
l_ok recogCreateDid(L_RECOG *recog, PIX *pixs)
recogCreateDid()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
static l_int32 recogPrepareForDecoding(L_RECOG *recog, PIX *pixs, l_int32 debug)
recogPrepareForDecoding()
l_ok pixClearAll(PIX *pix)
pixClearAll()
PIX * pixaDisplayTiledInRows(PIXA *pixa, l_int32 outdepth, l_int32 maxwidth, l_float32 scalefactor, l_int32 background, l_int32 spacing, l_int32 border)
pixaDisplayTiledInRows()
static l_int32 recogRunViterbi(L_RECOG *recog, PIX **ppixdb)
recogRunViterbi()
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
l_ok boxaAddBox(BOXA *boxa, BOX *box, l_int32 copyflag)
boxaAddBox()
static PIX * recogShowPath(L_RECOG *recog, l_int32 select)
recogShowPath()
l_int32 recogDidExists(L_RECOG *recog)
recogDidExists()
PIX * pixClone(PIX *pixs)
pixClone()
void pixDestroy(PIX **ppix)
pixDestroy()
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
PIX * recogProcessToIdentify(L_RECOG *recog, PIX *pixs, l_int32 pad)
recogProcessToIdentify()
NUMA * pixGetMomentByColumn(PIX *pix, l_int32 order)
pixGetMomentByColumn()
static l_int32 recogMakeDecodingArray(L_RECOG *recog, l_int32 index, l_int32 debug)
recogMakeDecodingArray()
l_int32 recogAverageSamples(L_RECOG **precog, l_int32 debug)
recogAverageSamples()
void numaDestroy(NUMA **pna)
numaDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
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()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
BOXA * boxaCreate(l_int32 n)
boxaCreate()
void boxDestroy(BOX **pbox)
boxDestroy()
static l_int32 recogGetWindowedArea(L_RECOG *recog, l_int32 index, l_int32 x, l_int32 *pdely, l_int32 *pwsum)
recogGetWindowedArea()
PIX * pixAddTextlines(PIX *pixs, L_BMF *bmf, const char *textstr, l_uint32 val, l_int32 location)
pixAddTextlines()
l_ok ptaGetIPt(PTA *pta, l_int32 index, l_int32 *px, l_int32 *py)
ptaGetIPt()
PIX * pixErodeBrick(PIX *pixd, PIX *pixs, l_int32 hsize, l_int32 vsize)
pixErodeBrick()
static l_int32 recogTransferRchToDid(L_RECOG *recog, l_int32 x, l_int32 y)
recogTransferRchToDid()
l_ok boxGetGeometry(BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
PIX * pixScale(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScale()
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
PIX * pixAddBorderGeneral(PIX *pixs, l_int32 left, l_int32 right, l_int32 top, l_int32 bot, l_uint32 val)
pixAddBorderGeneral()
static l_int32 recogRescoreDidResult(L_RECOG *recog, PIX **ppixdb)
recogRescoreDidResult()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()