157#include <config_auto.h>
162#include "allheaders.h"
172 l_int32 x, l_int32 *pdely, l_int32 *pwsum);
176static const l_float32 SetwidthFraction = 0.95;
177static const l_int32 MaxYShift = 1;
185static const l_float32 DefaultAlpha2[] = {0.95f, 0.9f};
186static 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);
254 pixa = pixaCreate(2);
258 return (
BOXA *)ERROR_PTR(
"error in rescoring", __func__, NULL);
261 *ppixdb = pixaDisplayTiledInRows(pixa, 32, 2 * pixGetWidth(pix1) + 100,
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);
309 ret = recogAverageSamples(recog, 0);
311 return ERROR_INT(
"averaging of samples failed", __func__, 1);
315 if ((pix1 = recogProcessToIdentify(recog, pixs, 0)) == NULL)
316 return ERROR_INT(
"pix1 not made", __func__, 1);
322 return ERROR_INT(
"decoder not made", __func__, 1);
327 did->
nasum = pixCountPixelsByColumn(pix1);
328 did->
namoment = pixGetMomentByColumn(pix1, 1);
359l_int32 i, j, w1, h1, w2, h2, nx, ycent2, count, maxcount, maxdely;
360l_int32 sum, moment, dely, shifty;
361l_int32 *counta, *delya, *ycent1, *arraysum, *arraymoment, *sumtab;
362NUMA *nasum, *namoment;
363PIX *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);
375 pixGetDimensions(pix1, &w1, &h1, NULL);
377 pixGetDimensions(pix2, &w2, &h2, NULL);
379 L_INFO(
"w1 = %d < w2 = %d for index %d\n", __func__, w1, w2, index);
386 ptaGetIPt(recog->
pta_u, index, NULL, &ycent2);
388 counta = did->
counta[index];
389 delya = did->
delya[index];
394 ycent1 = (l_int32 *)LEPT_CALLOC(nx,
sizeof(l_int32));
395 arraysum = numaGetIArray(nasum);
396 arraymoment = numaGetIArray(namoment);
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;
416 pix3 = pixCreate(w2, h1, 1);
417 for (i = 0; i < nx; i++) {
418 shifty = (l_int32)(ycent1[i] - ycent2 + 0.5);
421 for (j = -MaxYShift; j <= MaxYShift; j++) {
424 pixRasterop(pix3, 0, dely, w2, h2,
PIX_SRC, pix2, 0, 0);
426 pixCountPixels(pix3, &count, sumtab);
427 if (count > maxcount) {
432 counta[i] = maxcount;
441 LEPT_FREE(arraymoment);
481l_int32 i, w1, w2, h1, xnz, x, narray, minsetw;
482l_int32 first, templ, xloc, dely, counts, area1;
483l_int32 besttempl, spacetempl;
484l_int32 *setw, *didtempl;
486l_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);
521 area2 = numaGetIArray(recog->
nasum_u);
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);
574 numaAddNumber(did->
natempl, templ);
575 numaAddNumber(did->
naxloc, xloc);
576 numaAddNumber(did->
nadely, dely);
577 numaAddNumber(did->
nawidth, pixGetWidth(pix1));
578 numaAddNumber(did->
nascore, correl);
579 xnz = L_MAX(xloc, 0);
580 box = boxCreate(xnz, dely, w2, h1);
588 numaWriteStderr(did->
naxloc);
589 numaWriteStderr(did->
nadely);
592 boxaWriteStderr(did->
boxa);
618l_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);
632 if ((n = numaGetCount(did->
naxloc)) == 0)
633 return ERROR_INT(
"no elements in path", __func__, 1);
636 for (i = 0; i < n; i++) {
638 boxGetGeometry(box1, &x, &dely, NULL, NULL);
639 pix1 = pixClipRectangle(pixs, box1, NULL);
640 recogIdentifyPix(recog, pix1, NULL);
643 rchExtract(recog->
rch, &index, &score, &text,
644 &sample, NULL, NULL, NULL);
645 lept_stderr(
"text = %s, index = %d, sample = %d,"
646 " score = %5.3f\n", text, index, sample, score);
672l_int32 i, j, n, index, xloc, dely;
675NUMA *natempl_s, *nasample_s, *nascore_s, *naxloc_s, *nadely_s;
676PIX *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);
684 bmf = bmfCreate(NULL, 8);
685 pixs = pixScale(did->
pixs, 4.0, 4.0);
686 pix0 = pixAddBorderGeneral(pixs, 0, 0, 0, 40, 0);
687 pix1 = pixConvertTo32(pix0);
701 n = numaGetCount(natempl_s);
702 for (i = 0; i < n; i++) {
703 numaGetIValue(natempl_s, i, &index);
707 numaGetIValue(nasample_s, i, &j);
710 pix3 = pixScale(pix2, 4.0, 4.0);
711 pix4 = pixErodeBrick(NULL, pix3, 5, 5);
712 pixXor(pix4, pix4, pix3);
713 numaGetFValue(nascore_s, i, &score);
714 snprintf(textstr,
sizeof(textstr),
"%5.3f", score);
715 pix5 = pixAddTextlines(pix4, bmf, textstr, 1,
L_ADD_BELOW);
716 numaGetIValue(naxloc_s, i, &xloc);
717 numaGetIValue(nadely_s, i, &dely);
718 pixPaintThroughMask(pix1, pix5, 4 * xloc, 4 * dely, 0xff000000);
750 return ERROR_INT(
"recog not defined", __func__, 1);
752 return ERROR_INT(
"pixs not defined", __func__, 1);
758 did->
pixs = pixClone(pixs);
760 did->
size = pixGetWidth(pixs);
762 did->
naxloc = numaCreate(5);
763 did->
nadely = numaCreate(5);
765 did->
boxa = boxaCreate(5);
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);
834 pixDestroy(&did->
pixs);
835 numaDestroy(&did->
nasum);
838 numaDestroy(&did->
naxloc);
839 numaDestroy(&did->
nadely);
841 boxaDestroy(&did->
boxa);
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",
934l_int32 w1, h1, w2, h2;
935PIX *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);
949 pixGetDimensions(pix1, &w1, &h1, NULL);
951 return ERROR_INT(
"invalid x position", __func__, 1);
954 pixGetDimensions(pix2, &w2, &h2, NULL);
956 L_INFO(
"template %d too small\n", __func__, index);
961 *pdely = did->
delya[index][x];
962 pixt = pixCreate(w2, h1, 1);
963 pixRasterop(pixt, 0, *pdely, w2, h2,
PIX_SRC, pix2, 0, 0);
965 pixCountPixels(pixt, pwsum, recog->
sumtab);
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);
static l_int32 recogRunViterbi(L_RECOG *recog, PIX **ppixdb)
recogRunViterbi()
L_RDID * recogGetDid(L_RECOG *recog)
recogGetDid()
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()
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()