53 #include "allheaders.h" 60 static l_int32 testLineAlignmentX(
NUMA *na1,
NUMA *na2, l_int32 shiftx,
61 l_int32 delx, l_int32 nperline);
62 static l_int32 countAlignedMatches(
NUMA *nai1,
NUMA *nai2,
NUMA *nasx,
63 NUMA *nasy, l_int32 n1, l_int32 n2,
64 l_int32 delx, l_int32 dely,
65 l_int32 nreq, l_int32 *psame,
67 static void printRowIndices(l_int32 *index1, l_int32 n1,
68 l_int32 *index2, l_int32 n2);
100 const char *rootname,
106 l_int32 nfiles, i, numpages;
113 PROCNAME(
"jbCorrelation");
116 return ERROR_INT(
"dirin not defined", procName, 1);
118 return ERROR_INT(
"rootname not defined", procName, 1);
119 if (components != JB_CONN_COMPS && components != JB_CHARACTERS &&
120 components != JB_WORDS)
121 return ERROR_INT(
"components invalid", procName, 1);
127 classer = jbCorrelationInit(components, 0, 0, thresh, weight);
128 jbAddPages(classer, safiles);
131 data = jbDataSave(classer);
132 jbDataWrite(rootname, data);
136 pixa = jbDataRender(data, FALSE);
138 if (numpages != nfiles)
139 fprintf(stderr,
"numpages = %d, nfiles = %d, not equal!\n",
141 for (i = 0; i < numpages; i++) {
143 snprintf(filename,
L_BUF_SIZE,
"%s.%04d", rootname, i);
144 fprintf(stderr,
"filename: %s\n", filename);
145 pixWrite(filename,
pix, IFF_PNG);
152 jbClasserDestroy(&classer);
153 jbDataDestroy(&data);
182 const char *rootname,
188 l_int32 nfiles, i, numpages;
195 PROCNAME(
"jbRankHaus");
198 return ERROR_INT(
"dirin not defined", procName, 1);
200 return ERROR_INT(
"rootname not defined", procName, 1);
201 if (components != JB_CONN_COMPS && components != JB_CHARACTERS &&
202 components != JB_WORDS)
203 return ERROR_INT(
"components invalid", procName, 1);
209 classer = jbRankHausInit(components, 0, 0, size, rank);
210 jbAddPages(classer, safiles);
213 data = jbDataSave(classer);
214 jbDataWrite(rootname, data);
218 pixa = jbDataRender(data, FALSE);
220 if (numpages != nfiles)
221 fprintf(stderr,
"numpages = %d, nfiles = %d, not equal!\n",
223 for (i = 0; i < numpages; i++) {
225 snprintf(filename,
L_BUF_SIZE,
"%s.%04d", rootname, i);
226 fprintf(stderr,
"filename: %s\n", filename);
227 pixWrite(filename,
pix, IFF_PNG);
234 jbClasserDestroy(&classer);
235 jbDataDestroy(&data);
277 l_int32 nfiles, i,
w,
h;
285 PROCNAME(
"jbWordsInTextlines");
288 return (
JBCLASSER *)ERROR_PTR(
"&natl not defined", procName, NULL);
291 return (
JBCLASSER *)ERROR_PTR(
"dirin not defined", procName, NULL);
292 if (reduction != 1 && reduction != 2)
293 return (
JBCLASSER *)ERROR_PTR(
"reduction not in {1,2}", procName, NULL);
299 classer = jbCorrelationInit(JB_WORDS, maxwidth, maxheight, thresh, weight);
303 for (i = 0; i < nfiles; i++) {
305 if ((pix1 =
pixRead(fname)) == NULL) {
306 L_WARNING(
"image file %d not read\n", procName, i);
319 jbAddPageComponents(classer, pix2, boxa, pixa);
394 PROCNAME(
"pixGetWordsInTextlines");
396 if (!pboxad || !ppixad || !pnai)
397 return ERROR_INT(
"&boxad, &pixad, &nai not all defined", procName, 1);
402 return ERROR_INT(
"pixs not defined", procName, 1);
405 pixWordBoxesByDilation(pixs, minwidth, minheight, maxwidth, maxheight,
465 PROCNAME(
"pixGetWordBoxesInTextlines");
467 if (pnai) *pnai = NULL;
469 return ERROR_INT(
"&boxad and &nai not both defined", procName, 1);
472 return ERROR_INT(
"pixs not defined", procName, 1);
475 pixWordBoxesByDilation(pixs, minwidth, minheight, maxwidth, maxheight,
518 l_int32 index, nbox, row, prevrow, x, y,
w,
h;
523 PROCNAME(
"boxaExtractSortedPattern");
526 return (
NUMAA *)ERROR_PTR(
"boxa not defined", procName, NULL);
528 return (
NUMAA *)ERROR_PTR(
"na not defined", procName, NULL);
536 for (index = 0; index < nbox; index++) {
614 l_int32 n1, n2, i, j, nbox, y1, y2, xl1, xl2;
615 l_int32 shiftx, shifty, match;
616 l_int32 *line1, *line2;
617 l_int32 *yloc1, *yloc2;
618 l_int32 *xleft1, *xleft2;
619 NUMA *na1, *na2, *nai1, *nai2, *nasx, *nasy;
621 PROCNAME(
"numaaCompareImagesByBoxes");
624 return ERROR_INT(
"&same not defined", procName, 1);
627 return ERROR_INT(
"naa1 not defined", procName, 1);
629 return ERROR_INT(
"naa2 not defined", procName, 1);
631 return ERROR_INT(
"nperline < 1", procName, 1);
633 return ERROR_INT(
"nreq < 1", procName, 1);
637 if (n1 < nreq || n2 < nreq)
643 line1 = (l_int32 *)LEPT_CALLOC(n1,
sizeof(l_int32));
644 line2 = (l_int32 *)LEPT_CALLOC(n2,
sizeof(l_int32));
645 yloc1 = (l_int32 *)LEPT_CALLOC(n1,
sizeof(l_int32));
646 yloc2 = (l_int32 *)LEPT_CALLOC(n2,
sizeof(l_int32));
647 xleft1 = (l_int32 *)LEPT_CALLOC(n1,
sizeof(l_int32));
648 xleft2 = (l_int32 *)LEPT_CALLOC(n2,
sizeof(l_int32));
649 if (!line1 || !line2 || !yloc1 || !yloc2 || !xleft1 || !xleft2)
650 return ERROR_INT(
"callof failure for an array", procName, 1);
651 for (i = 0; i < n1; i++) {
656 if (nbox >= nperline)
660 for (i = 0; i < n2; i++) {
665 if (nbox >= nperline)
681 for (i = 0; i < n1; i++) {
682 if (line1[i] == 0)
continue;
686 for (j = 0; j < n2; j++) {
687 if (line2[j] == 0)
continue;
689 if (L_ABS(y1 - y2) > maxshifty)
continue;
691 if (L_ABS(xl1 - xl2) > maxshiftx)
continue;
697 match = testLineAlignmentX(na1, na2, shiftx, delx, nperline);
713 countAlignedMatches(nai1, nai2, nasx, nasy, n1, n2, delx, dely,
714 nreq, psame, debugflag);
731 testLineAlignmentX(
NUMA *na1,
737 l_int32 i, xl1, xr1, xl2, xr2, diffl, diffr;
739 PROCNAME(
"testLineAlignmentX");
742 return ERROR_INT(
"na1 not defined", procName, 1);
744 return ERROR_INT(
"na2 not defined", procName, 1);
746 for (i = 0; i < nperline; i++) {
751 diffl = L_ABS(xl1 - xl2 - shiftx);
752 diffr = L_ABS(xr1 - xr2 - shiftx);
753 if (diffl > delx || diffr > delx)
779 countAlignedMatches(
NUMA *nai1,
791 l_int32 i, j, nm, shiftx, shifty, nmatch, diffx, diffy;
792 l_int32 *ia1, *ia2, *iasx, *iasy, *index1, *index2;
794 PROCNAME(
"countAlignedMatches");
796 if (!nai1 || !nai2 || !nasx || !nasy)
797 return ERROR_INT(
"4 input numas not defined", procName, 1);
799 return ERROR_INT(
"&same not defined", procName, 1);
816 index1 = (l_int32 *)LEPT_CALLOC(n1,
sizeof(l_int32));
817 index2 = (l_int32 *)LEPT_CALLOC(n2,
sizeof(l_int32));
818 if (!index1 || !index2)
819 return ERROR_INT(
"calloc fail for array", procName, 1);
820 for (i = 0; i < nm; i++) {
825 memset(index1, 0, 4 * n1);
826 memset(index2, 0, 4 * n2);
828 index1[ia1[i]] = nmatch;
829 index2[ia2[i]] = nmatch;
836 for (j = 0; j < nm; j++) {
837 if (j == i)
continue;
839 if (index1[ia1[j]] > 0 || index2[ia2[j]] > 0)
continue;
841 diffx = L_ABS(shiftx - iasx[j]);
842 diffy = L_ABS(shifty - iasy[j]);
843 if (diffx > delx || diffy > dely)
continue;
846 index1[ia1[j]] = nmatch;
847 index2[ia2[j]] = nmatch;
848 if (nmatch >= nreq) {
851 printRowIndices(index1, n1, index2, n2);
868 printRowIndices(l_int32 *index1,
875 fprintf(stderr,
"Index1: ");
876 for (i = 0; i < n1; i++) {
877 if (i && (i % 20 == 0))
878 fprintf(stderr,
"\n ");
879 fprintf(stderr,
"%3d", index1[i]);
881 fprintf(stderr,
"\n");
883 fprintf(stderr,
"Index2: ");
884 for (i = 0; i < n2; i++) {
885 if (i && (i % 20 == 0))
886 fprintf(stderr,
"\n ");
887 fprintf(stderr,
"%3d", index2[i]);
889 fprintf(stderr,
"\n");
void pixaaDestroy(PIXAA **ppaa)
pixaaDestroy()
NUMAA * boxaExtractSortedPattern(BOXA *boxa, NUMA *na)
boxaExtractSortedPattern()
l_int32 numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
PIXAA * pixaSort2dByIndex(PIXA *pixas, NUMAA *naa, l_int32 copyflag)
pixaSort2dByIndex()
SARRAY * sarrayCopy(SARRAY *sa)
sarrayCopy()
l_int32 pixGetWordsInTextlines(PIX *pixs, l_int32 minwidth, l_int32 minheight, l_int32 maxwidth, l_int32 maxheight, BOXA **pboxad, PIXA **ppixad, NUMA **pnai)
pixGetWordsInTextlines()
PIXA * pixaaFlattenToPixa(PIXAA *paa, NUMA **pnaindex, l_int32 copyflag)
pixaaFlattenToPixa()
l_int32 numaJoin(NUMA *nad, NUMA *nas, l_int32 istart, l_int32 iend)
numaJoin()
NUMA * numaCreate(l_int32 n)
numaCreate()
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
static const l_int32 JB_WORDS_MIN_WIDTH
NUMAA * numaaCreate(l_int32 n)
numaaCreate()
void numaaDestroy(NUMAA **pnaa)
numaaDestroy()
l_int32 numaaAddNuma(NUMAA *naa, NUMA *na, l_int32 copyflag)
numaaAddNuma()
static const l_int32 JB_WORDS_MIN_HEIGHT
l_int32 jbCorrelation(const char *dirin, l_float32 thresh, l_float32 weight, l_int32 components, const char *rootname, l_int32 firstpage, l_int32 npages, l_int32 renderflag)
jbCorrelation()
l_int32 * numaGetIArray(NUMA *na)
numaGetIArray()
NUMA * numaaGetNuma(NUMAA *naa, l_int32 index, l_int32 accessflag)
numaaGetNuma()
void boxaaDestroy(BOXAA **pbaa)
boxaaDestroy()
static const l_int32 L_INSERT
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_int32 jbRankHaus(const char *dirin, l_int32 size, l_float32 rank, l_int32 components, const char *rootname, l_int32 firstpage, l_int32 npages, l_int32 renderflag)
jbRankHaus()
l_int32 boxGetGeometry(BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
PIX * pixClone(PIX *pixs)
pixClone()
void pixDestroy(PIX **ppix)
pixDestroy()
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
SARRAY * getSortedPathnamesInDirectory(const char *dirname, const char *substr, l_int32 first, l_int32 nfiles)
getSortedPathnamesInDirectory()
JBCLASSER * jbWordsInTextlines(const char *dirin, l_int32 reduction, l_int32 maxwidth, l_int32 maxheight, l_float32 thresh, l_float32 weight, NUMA **pnatl, l_int32 firstpage, l_int32 npages)
jbWordsInTextlines()
void numaDestroy(NUMA **pna)
numaDestroy()
l_int32 pixGetWordBoxesInTextlines(PIX *pixs, l_int32 minwidth, l_int32 minheight, l_int32 maxwidth, l_int32 maxheight, BOXA **pboxad, NUMA **pnai)
pixGetWordBoxesInTextlines()
PIXA * pixaCreateFromBoxa(PIX *pixs, BOXA *boxa, l_int32 *pcropwarn)
pixaCreateFromBoxa()
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
PIX * pixRead(const char *filename)
pixRead()
l_int32 numaaGetCount(NUMAA *naa)
numaaGetCount()
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
void boxDestroy(BOX **pbox)
boxDestroy()
BOXAA * boxaSort2d(BOXA *boxas, NUMAA **pnaad, l_int32 delta1, l_int32 delta2, l_int32 minh1)
boxaSort2d()
l_int32 numaaCompareImagesByBoxes(NUMAA *naa1, NUMAA *naa2, l_int32 nperline, l_int32 nreq, l_int32 maxshiftx, l_int32 maxshifty, l_int32 delx, l_int32 dely, l_int32 *psame, l_int32 debugflag)
numaaCompareImagesByBoxes()
l_int32 boxaGetCount(BOXA *boxa)
boxaGetCount()
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()
BOXA * boxaaFlattenToBoxa(BOXAA *baa, NUMA **pnaindex, l_int32 copyflag)
boxaaFlattenToBoxa()
PIX * pixReduceRankBinaryCascade(PIX *pixs, l_int32 level1, l_int32 level2, l_int32 level3, l_int32 level4)
pixReduceRankBinaryCascade()
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
static const l_int32 L_BUF_SIZE
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
BOXA * pixaGetBoxa(PIXA *pixa, l_int32 accesstype)
pixaGetBoxa()