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);
523 for (x = minsetw; x < w1; x++) {
525 for (i = 0; i <
narray; i++) {
526 if (x - setw[i] < 0)
continue;
527 matchscore = didscore[x - setw[i]] +
529 did->
beta[1] * area2[i];
531 maxscore = matchscore;
535 if (matchscore > maxscore) {
536 maxscore = matchscore;
544 prevscore = didscore[x - 1];
545 if (prevscore > maxscore) {
546 maxscore = prevscore;
547 besttempl = spacetempl;
549 didscore[x] = maxscore;
550 didtempl[x] = besttempl;
555 for (x = w1 - 1; x >= 0; x--) {
556 if (didtempl[x] != spacetempl)
break;
558 h1 = pixGetHeight(did->
pixs);
560 if (didtempl[x] == spacetempl) {
565 xloc = x - setw[templ];
567 counts = did->
counta[templ][xloc];
569 correl = ((l_float32)(counts) * counts) /
570 (l_float32)(area2[templ] * area1);
572 w2 = pixGetWidth(pix1);
578 xnz = L_MAX(xloc, 0);
617 l_int32 i, n, sample, x, dely, index;
624 PROCNAME(
"recogRescoreDidResult");
626 if (ppixdb) *ppixdb = NULL;
628 return ERROR_INT(
"recog not defined", procName, 1);
630 return ERROR_INT(
"did not defined", procName, 1);
632 return ERROR_INT(
"did full arrays not made", procName, 1);
634 return ERROR_INT(
"no elements in path", procName, 1);
637 for (i = 0; i < n; i++) {
645 &sample, NULL, NULL, NULL);
646 fprintf(stderr,
"text = %s, index = %d, sample = %d," 647 " score = %5.3f\n", text, index, sample, score);
673 l_int32 i, j, n, index, xloc, dely;
676 NUMA *natempl_s, *nasample_s, *nascore_s, *naxloc_s, *nadely_s;
677 PIX *
pixs, *pix0, *pix1, *pix2, *pix3, *pix4, *pix5;
680 PROCNAME(
"recogShowPath");
683 return (
PIX *)ERROR_PTR(
"recog not defined", procName, NULL);
685 return (
PIX *)ERROR_PTR(
"did not defined", procName, NULL);
705 for (i = 0; i < n; i++) {
717 snprintf(textstr,
sizeof(textstr),
"%5.3f", score);
752 PROCNAME(
"recogCreateDid");
755 return ERROR_INT(
"recog not defined", procName, 1);
757 return ERROR_INT(
"pixs not defined", procName, 1);
780 did->
setwidth = (l_int32 *)LEPT_CALLOC(did->
narray,
sizeof(l_int32));
781 did->
counta = (l_int32 **)LEPT_CALLOC(did->
narray,
sizeof(l_int32 *));
782 did->
delya = (l_int32 **)LEPT_CALLOC(did->
narray,
sizeof(l_int32 *));
783 did->
beta = (l_float32 *)LEPT_CALLOC(5,
sizeof(l_float32));
784 did->
gamma = (l_float32 *)LEPT_CALLOC(5,
sizeof(l_float32));
787 for (i = 0; i < did->
narray; i++) {
788 did->
counta[i] = (l_int32 *)LEPT_CALLOC(did->
size,
sizeof(l_int32));
789 did->
delya[i] = (l_int32 *)LEPT_CALLOC(did->
size,
sizeof(l_int32));
793 for (i = 0; i < did->
narray; i++) {
795 did->
setwidth[i] = (l_int32)(SetwidthFraction * pixGetWidth(pix1));
821 PROCNAME(
"recogDestroyDid");
824 return ERROR_INT(
"recog not defined", procName, 1);
826 if ((did = recog->
did) == NULL)
return 0;
828 return ERROR_INT(
"ptr array is null; shouldn't happen!", procName, 1);
830 for (i = 0; i < did->
narray; i++) {
831 LEPT_FREE(did->
counta[i]);
832 LEPT_FREE(did->
delya[i]);
836 LEPT_FREE(did->
delya);
837 LEPT_FREE(did->
beta);
838 LEPT_FREE(did->
gamma);
874 PROCNAME(
"recogDidExists");
877 return ERROR_INT(
"recog not defined", procName, 0);
878 return (recog->
did) ? 1 : 0;
899 PROCNAME(
"recogGetDid");
902 return (
L_RDID *)ERROR_PTR(
"recog not defined", procName, NULL);
903 if ((did = recog->
did) == NULL)
904 return (
L_RDID *)ERROR_PTR(
"did not defined", procName, NULL);
906 return (
L_RDID *)ERROR_PTR(
"did array ptrs not defined",
908 for (i = 0; i < did->
narray; i++) {
910 return (
L_RDID *)ERROR_PTR(
"did arrays not defined",
945 l_int32 w1, h1, w2, h2;
946 PIX *pix1, *pix2, *pixt;
949 PROCNAME(
"recogGetWindowedArea");
951 if (pdely) *pdely = 0;
952 if (pwsum) *pwsum = 0;
953 if (!pdely || !pwsum)
954 return ERROR_INT(
"&dely and &wsum not both defined", procName, 1);
956 return ERROR_INT(
"recog not defined", procName, 1);
958 return ERROR_INT(
"did not defined", procName, 1);
959 if (index < 0 || index >= did->
narray)
960 return ERROR_INT(
"invalid index", procName, 1);
964 return ERROR_INT(
"invalid x position", procName, 1);
969 L_INFO(
"template %d too small\n", procName, index);
974 *pdely = did->
delya[index][x];
1007 const l_float32 *da;
1010 PROCNAME(
"recogSetChannelParams");
1013 return ERROR_INT(
"recog not defined", procName, 1);
1015 return ERROR_INT(
"did not defined", procName, 1);
1018 else if (nlevels == 4)
1021 return ERROR_INT(
"nlevels not 2 or 4", procName, 1);
1023 for (i = 1; i < nlevels; i++) {
1024 did->
beta[i] = log((1.0 - da[i]) / da[0]);
1025 did->
gamma[i] = log(da[0] * da[i] / ((1.0 - da[0]) * (1.0 - da[i])));
1056 PROCNAME(
"recogTransferRchToDid");
1059 return ERROR_INT(
"recog not defined", procName, 1);
1061 return ERROR_INT(
"did not defined", procName, 1);
1062 if ((rch = recog->
rch) == NULL)
1063 return ERROR_INT(
"rch not defined", procName, 1);
l_int32 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()
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
NUMA * pixCountPixelsByColumn(PIX *pix)
pixCountPixelsByColumn()
l_int32 numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
l_int32 boxaWriteStream(FILE *fp, BOXA *boxa)
boxaWriteStream()
PIXA * pixaCreate(l_int32 n)
pixaCreate()
l_int32 boxaAddBox(BOXA *boxa, BOX *box, l_int32 copyflag)
boxaAddBox()
l_int32 numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
l_int32 recogSetChannelParams(L_RECOG *recog, l_int32 nlevels)
recogSetChannelParams()
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()
NUMA * numaCreate(l_int32 n)
numaCreate()
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
l_int32 recogCreateDid(L_RECOG *recog, PIX *pixs)
recogCreateDid()
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
PIX * pixaaGetPix(PIXAA *paa, l_int32 index, l_int32 ipix, l_int32 accessflag)
pixaaGetPix()
l_int32 * numaGetIArray(NUMA *na)
numaGetIArray()
l_int32 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()
static const l_int32 L_INSERT
PIX * pixXor(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixXor()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
static l_int32 recogPrepareForDecoding(L_RECOG *recog, PIX *pixs, l_int32 debug)
recogPrepareForDecoding()
l_int32 pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
l_int32 recogIdentifyPix(L_RECOG *recog, PIX *pixs, PIX **ppixdb)
recogIdentifyPix()
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_int32 pixClearAll(PIX *pix)
pixClearAll()
static PIX * recogShowPath(L_RECOG *recog, l_int32 select)
recogShowPath()
l_int32 recogDidExists(L_RECOG *recog)
recogDidExists()
l_int32 boxGetGeometry(BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
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()
l_int32 pixPaintThroughMask(PIX *pixd, PIX *pixm, l_int32 x, l_int32 y, l_uint32 val)
pixPaintThroughMask()
void numaDestroy(NUMA **pna)
numaDestroy()
l_int32 ptaGetIPt(PTA *pta, l_int32 index, l_int32 *px, l_int32 *py)
ptaGetIPt()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
BOXA * boxaCreate(l_int32 n)
boxaCreate()
void boxDestroy(BOX **pbox)
boxDestroy()
l_int32 recogDestroyDid(L_RECOG *recog)
recogDestroyDid()
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_int32 numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
l_int32 pixGetDimensions(PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
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_int32 pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
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_int32 numaWriteStream(FILE *fp, NUMA *na)
numaWriteStream()
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()