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 PROCNAME(
"recogDecode");
230 if (ppixdb) *ppixdb = NULL;
232 return (
BOXA *)ERROR_PTR(
"recog not defined", procName, NULL);
233 if (!
pixs || pixGetDepth(
pixs) != 1)
234 return (
BOXA *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
236 return (
BOXA *)ERROR_PTR(
"training not finished", procName, NULL);
238 return (
BOXA *)ERROR_PTR(
"nlevels != 2 (for now)", procName, NULL);
240 debug = (ppixdb) ? 1 : 0;
242 return (
BOXA *)ERROR_PTR(
"error making arrays", procName, NULL);
250 return (
BOXA *)ERROR_PTR(
"error in Viterbi", procName, NULL);
255 return (
BOXA *)ERROR_PTR(
"error in viterbi", procName, NULL);
260 return (
BOXA *)ERROR_PTR(
"error in rescoring", procName, NULL);
303 PROCNAME(
"recogPrepareForDecoding");
306 return ERROR_INT(
"recog not defined", procName, 1);
307 if (!
pixs || pixGetDepth(
pixs) != 1)
308 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
310 return ERROR_INT(
"training not finished", procName, 1);
317 return ERROR_INT(
"pix1 not made", procName, 1);
323 return ERROR_INT(
"decoder not made", procName, 1);
360 l_int32 i, j, w1, h1, w2, h2, nx, ycent2, count, maxcount, maxdely;
361 l_int32 sum, moment, dely, shifty;
362 l_int32 *
counta, *
delya, *ycent1, *arraysum, *arraymoment, *sumtab;
364 PIX *pix1, *pix2, *pix3;
367 PROCNAME(
"recogMakeDecodingArray");
370 return ERROR_INT(
"recog not defined", procName, 1);
372 return ERROR_INT(
"did not defined", procName, 1);
373 if (index < 0 || index >= did->
narray)
374 return ERROR_INT(
"invalid index", procName, 1);
382 L_INFO(
"w1 = %d < w2 = %d for index %d\n", procName, w1, w2, index);
397 ycent1 = (l_int32 *)LEPT_CALLOC(nx,
sizeof(l_int32));
400 for (i = 0, sum = 0, moment = 0; i < w2; i++) {
402 moment += arraymoment[i];
404 for (i = 0; i < nx - 1; i++) {
405 ycent1[i] = (sum == 0) ? ycent2 : (l_float32)moment / (l_float32)sum;
406 sum += arraysum[w2 + i] - arraysum[i];
407 moment += arraymoment[w2 + i] - arraymoment[i];
409 ycent1[nx - 1] = (sum == 0) ? ycent2 : (l_float32)moment / (l_float32)sum;
420 for (i = 0; i < nx; i++) {
421 shifty = (l_int32)(ycent1[i] - ycent2 + 0.5);
424 for (j = -MaxYShift; j <= MaxYShift; j++) {
430 if (count > maxcount) {
444 LEPT_FREE(arraymoment);
484 l_int32 i, w1, w2, h1, xnz, x,
narray, minsetw;
485 l_int32 first, templ, xloc, dely, counts, area1;
486 l_int32 besttempl, spacetempl;
487 l_int32 *setw, *didtempl;
489 l_float32 prevscore, matchscore, maxscore, correl;
495 PROCNAME(
"recogRunViterbi");
497 if (ppixdb) *ppixdb = NULL;
499 return ERROR_INT(
"recog not defined", procName, 1);
501 return ERROR_INT(
"did not defined", procName, 1);
503 return ERROR_INT(
"did full arrays not made", procName, 1);
512 for (i = 0; i <
narray; i++) {
513 if (setw[i] < minsetw)
517 return ERROR_INT(
"minsetw <= 2; bad templates", procName, 1);
529 for (x = minsetw; x < w1; x++) {
531 for (i = 0; i <
narray; i++) {
532 if (x - setw[i] < 0)
continue;
533 matchscore = didscore[x - setw[i]] +
535 did->
beta[1] * area2[i];
537 maxscore = matchscore;
541 if (matchscore > maxscore) {
542 maxscore = matchscore;
550 prevscore = didscore[x - 1];
551 if (prevscore > maxscore) {
552 maxscore = prevscore;
553 besttempl = spacetempl;
555 didscore[x] = maxscore;
556 didtempl[x] = besttempl;
561 for (x = w1 - 1; x >= 0; x--) {
562 if (didtempl[x] != spacetempl)
break;
564 h1 = pixGetHeight(did->
pixs);
566 if (didtempl[x] == spacetempl) {
571 xloc = x - setw[templ];
573 counts = did->
counta[templ][xloc];
575 correl = ((l_float32)(counts) * counts) /
576 (l_float32)(area2[templ] * area1);
578 w2 = pixGetWidth(pix1);
584 xnz = L_MAX(xloc, 0);
623 l_int32 i, n, sample, x, dely, index;
630 PROCNAME(
"recogRescoreDidResult");
632 if (ppixdb) *ppixdb = NULL;
634 return ERROR_INT(
"recog not defined", procName, 1);
636 return ERROR_INT(
"did not defined", procName, 1);
638 return ERROR_INT(
"did full arrays not made", procName, 1);
640 return ERROR_INT(
"no elements in path", procName, 1);
643 for (i = 0; i < n; i++) {
651 &sample, NULL, NULL, NULL);
653 " score = %5.3f\n", text, index, sample, score);
679 l_int32 i, j, n, index, xloc, dely;
682 NUMA *natempl_s, *nasample_s, *nascore_s, *naxloc_s, *nadely_s;
683 PIX *
pixs, *pix0, *pix1, *pix2, *pix3, *pix4, *pix5;
686 PROCNAME(
"recogShowPath");
689 return (
PIX *)ERROR_PTR(
"recog not defined", procName, NULL);
691 return (
PIX *)ERROR_PTR(
"did not defined", procName, NULL);
711 for (i = 0; i < n; i++) {
723 snprintf(textstr,
sizeof(textstr),
"%5.3f", score);
758 PROCNAME(
"recogCreateDid");
761 return ERROR_INT(
"recog not defined", procName, 1);
763 return ERROR_INT(
"pixs not defined", procName, 1);
786 did->
setwidth = (l_int32 *)LEPT_CALLOC(did->
narray,
sizeof(l_int32));
787 did->
counta = (l_int32 **)LEPT_CALLOC(did->
narray,
sizeof(l_int32 *));
788 did->
delya = (l_int32 **)LEPT_CALLOC(did->
narray,
sizeof(l_int32 *));
789 did->
beta = (l_float32 *)LEPT_CALLOC(5,
sizeof(l_float32));
790 did->
gamma = (l_float32 *)LEPT_CALLOC(5,
sizeof(l_float32));
793 for (i = 0; i < did->
narray; i++) {
794 did->
counta[i] = (l_int32 *)LEPT_CALLOC(did->
size,
sizeof(l_int32));
795 did->
delya[i] = (l_int32 *)LEPT_CALLOC(did->
size,
sizeof(l_int32));
799 for (i = 0; i < did->
narray; i++) {
801 did->
setwidth[i] = (l_int32)(SetwidthFraction * pixGetWidth(pix1));
827 PROCNAME(
"recogDestroyDid");
830 return ERROR_INT(
"recog not defined", procName, 1);
832 if ((did = recog->
did) == NULL)
return 0;
834 return ERROR_INT(
"ptr array is null; shouldn't happen!", procName, 1);
836 for (i = 0; i < did->
narray; i++) {
837 LEPT_FREE(did->
counta[i]);
838 LEPT_FREE(did->
delya[i]);
842 LEPT_FREE(did->
delya);
843 LEPT_FREE(did->
beta);
844 LEPT_FREE(did->
gamma);
880 PROCNAME(
"recogDidExists");
883 return ERROR_INT(
"recog not defined", procName, 0);
884 return (recog->
did) ? 1 : 0;
905 PROCNAME(
"recogGetDid");
908 return (
L_RDID *)ERROR_PTR(
"recog not defined", procName, NULL);
909 if ((did = recog->
did) == NULL)
910 return (
L_RDID *)ERROR_PTR(
"did not defined", procName, NULL);
912 return (
L_RDID *)ERROR_PTR(
"did array ptrs not defined",
914 for (i = 0; i < did->
narray; i++) {
916 return (
L_RDID *)ERROR_PTR(
"did arrays not defined",
951 l_int32 w1, h1, w2, h2;
952 PIX *pix1, *pix2, *pixt;
955 PROCNAME(
"recogGetWindowedArea");
957 if (pdely) *pdely = 0;
958 if (pwsum) *pwsum = 0;
959 if (!pdely || !pwsum)
960 return ERROR_INT(
"&dely and &wsum not both defined", procName, 1);
962 return ERROR_INT(
"recog not defined", procName, 1);
964 return ERROR_INT(
"did not defined", procName, 1);
965 if (index < 0 || index >= did->
narray)
966 return ERROR_INT(
"invalid index", procName, 1);
970 return ERROR_INT(
"invalid x position", procName, 1);
975 L_INFO(
"template %d too small\n", procName, index);
980 *pdely = did->
delya[index][x];
1013 const l_float32 *da;
1016 PROCNAME(
"recogSetChannelParams");
1019 return ERROR_INT(
"recog not defined", procName, 1);
1021 return ERROR_INT(
"did not defined", procName, 1);
1024 else if (nlevels == 4)
1027 return ERROR_INT(
"nlevels not 2 or 4", procName, 1);
1029 for (i = 1; i < nlevels; i++) {
1030 did->
beta[i] = log((1.0 - da[i]) / da[0]);
1031 did->
gamma[i] = log(da[0] * da[i] / ((1.0 - da[0]) * (1.0 - da[i])));
1062 PROCNAME(
"recogTransferRchToDid");
1065 return ERROR_INT(
"recog not defined", procName, 1);
1067 return ERROR_INT(
"did not defined", procName, 1);
1068 if ((rch = recog->
rch) == NULL)
1069 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()
void lept_stderr(const char *fmt,...)
lept_stderr()
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()
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()
l_ok boxaWriteStderr(BOXA *boxa)
boxaWriteStderr()
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()
l_ok numaWriteStderr(NUMA *na)
numaWriteStderr()
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()