Leptonica  1.77.0
Image processing and image analysis suite
recogident.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
92 #include <string.h>
93 #include "allheaders.h"
94 
95  /* There are two methods for splitting characters: DID and greedy.
96  * The default method is DID. */
97 #define SPLIT_WITH_DID 1
98 
99  /* Padding on pix1: added before correlations and removed from result */
100 static const l_int32 LeftRightPadding = 32;
101 
102  /* Parameters for filtering and sorting connected components in splitter */
103 static const l_float32 MinFillFactor = 0.10;
104 static const l_int32 DefaultMinHeight = 15; /* min unscaled height */
105 static const l_int32 MinOverlap1 = 6; /* in pass 1 of boxaSort2d() */
106 static const l_int32 MinOverlap2 = 6; /* in pass 2 of boxaSort2d() */
107 static const l_int32 MinHeightPass1 = 5; /* min height to start pass 1 */
108 
109 
110 static l_int32 pixCorrelationBestShift(PIX *pix1, PIX *pix2, NUMA *nasum1,
111  NUMA *namoment1, l_int32 area2,
112  l_int32 ycent2, l_int32 maxyshift,
113  l_int32 *tab8, l_int32 *pdelx,
114  l_int32 *pdely, l_float32 *pscore,
115  l_int32 debugflag );
116 static L_RCH *rchCreate(l_int32 index, l_float32 score, char *text,
117  l_int32 sample, l_int32 xloc, l_int32 yloc,
118  l_int32 width);
119 static L_RCHA *rchaCreate();
120 static l_int32 transferRchToRcha(L_RCH *rch, L_RCHA *rcha);
121 static PIX *recogPreSplittingFilter(L_RECOG *recog, PIX *pixs, l_int32 minh,
122  l_float32 minaf, l_int32 debug);
123 static l_int32 recogSplittingFilter(L_RECOG *recog, PIX *pixs, l_int32 min,
124  l_float32 minaf, l_int32 *premove,
125  l_int32 debug);
126 static void l_showIndicatorSplitValues(NUMA *na1, NUMA *na2, NUMA *na3,
127  NUMA *na4, NUMA *na5, NUMA *na6);
128 
129 /*------------------------------------------------------------------------*
130  * Identification
131  *------------------------------------------------------------------------*/
157 l_ok
159  PIX *pixs,
160  l_int32 minh,
161  l_int32 skipsplit,
162  BOXA **pboxa,
163  PIXA **ppixa,
164  PIX **ppixdb,
165  l_int32 debugsplit)
166 {
167 l_int32 n;
168 BOXA *boxa;
169 PIX *pixb;
170 PIXA *pixa;
171 
172  PROCNAME("recogIdentifyMultiple");
173 
174  if (pboxa) *pboxa = NULL;
175  if (ppixa) *ppixa = NULL;
176  if (ppixdb) *ppixdb = NULL;
177  if (!recog)
178  return ERROR_INT("recog not defined", procName, 2);
179  if (!recog->train_done)
180  return ERROR_INT("training not finished", procName, 2);
181  if (!pixs)
182  return ERROR_INT("pixs not defined", procName, 2);
183 
184  /* Binarize if necessary */
185  if (pixGetDepth(pixs) > 1)
186  pixb = pixConvertTo1(pixs, recog->threshold);
187  else
188  pixb = pixClone(pixs);
189 
190  /* Noise removal and splitting of touching characters */
191  recogSplitIntoCharacters(recog, pixb, minh, skipsplit, &boxa, &pixa,
192  debugsplit);
193  pixDestroy(&pixb);
194  if (!pixa || (n = pixaGetCount(pixa)) == 0) {
195  pixaDestroy(&pixa);
196  boxaDestroy(&boxa);
197  L_WARNING("nothing found\n", procName);
198  return 1;
199  }
200 
201  recogIdentifyPixa(recog, pixa, ppixdb);
202  if (pboxa)
203  *pboxa = boxa;
204  else
205  boxaDestroy(&boxa);
206  if (ppixa)
207  *ppixa = pixa;
208  else
209  pixaDestroy(&pixa);
210  return 0;
211 }
212 
213 
214 /*------------------------------------------------------------------------*
215  * Segmentation and noise removal *
216  *------------------------------------------------------------------------*/
245 l_ok
247  PIX *pixs,
248  l_int32 minh,
249  l_int32 skipsplit,
250  BOXA **pboxa,
251  PIXA **ppixa,
252  l_int32 debug)
253 {
254 static l_int32 ind = 0;
255 char buf[32];
256 l_int32 i, xoff, yoff, empty, maxw, bw, ncomp, scaling;
257 BOX *box;
258 BOXA *boxa1, *boxa2, *boxa3, *boxa4, *boxad;
259 BOXAA *baa;
260 PIX *pix, *pix1, *pix2, *pix3;
261 PIXA *pixa;
262 
263  PROCNAME("recogSplitIntoCharacters");
264 
265  lept_mkdir("lept/recog");
266 
267  if (pboxa) *pboxa = NULL;
268  if (ppixa) *ppixa = NULL;
269  if (!pboxa || !ppixa)
270  return ERROR_INT("&boxa and &pixa not defined", procName, 1);
271  if (!recog)
272  return ERROR_INT("recog not defined", procName, 1);
273  if (!recog->train_done)
274  return ERROR_INT("training not finished", procName, 1);
275  if (!pixs || pixGetDepth(pixs) != 1)
276  return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
277  if (minh <= 0) minh = DefaultMinHeight;
278  pixZero(pixs, &empty);
279  if (empty) return 1;
280 
281  /* Small vertical close for consolidation. Don't do a horizontal
282  * closing, because it might join separate characters. */
283  pix1 = pixMorphSequence(pixs, "c1.3", 0);
284 
285  /* Carefully filter out noise */
286  pix2 = recogPreSplittingFilter(recog, pix1, minh, MinFillFactor, debug);
287  pixDestroy(&pix1);
288 
289  /* Get the 8-connected components to be split/identified */
290  boxa1 = pixConnComp(pix2, NULL, 8);
291  pixDestroy(&pix2);
292  ncomp = boxaGetCount(boxa1);
293  if (ncomp == 0) {
294  boxaDestroy(&boxa1);
295  L_WARNING("all components removed\n", procName);
296  return 1;
297  }
298 
299  /* Save everything and split the large components */
300  boxa2 = boxaCreate(ncomp);
301  maxw = recog->maxwidth_u + 5;
302  scaling = (recog->scalew > 0 || recog->scaleh > 0) ? TRUE : FALSE;
303  pixa = (debug) ? pixaCreate(ncomp) : NULL;
304  for (i = 0; i < ncomp; i++) {
305  box = boxaGetBox(boxa1, i, L_CLONE);
306  boxGetGeometry(box, &xoff, &yoff, &bw, NULL);
307  /* Treat as one character if it is small, if the images
308  * have been scaled, or if splitting is not to be run. */
309  if (bw <= maxw || scaling || skipsplit) {
310  boxaAddBox(boxa2, box, L_INSERT);
311  } else {
312  pix = pixClipRectangle(pixs, box, NULL);
313 #if SPLIT_WITH_DID
314  if (!debug) {
315  boxa3 = recogDecode(recog, pix, 2, NULL);
316  } else {
317  boxa3 = recogDecode(recog, pix, 2, &pix2);
318  pixaAddPix(pixa, pix2, L_INSERT);
319  }
320 #else /* use greedy splitting */
321  recogCorrelationBestRow(recog, pix, &boxa3, NULL, NULL,
322  NULL, debug);
323  if (debug) {
324  pix2 = pixConvertTo32(pix);
325  pixRenderBoxaArb(pix2, boxa3, 2, 255, 0, 0);
326  pixaAddPix(pixa, pix2, L_INSERT);
327  }
328 #endif /* SPLIT_WITH_DID */
329  pixDestroy(&pix);
330  boxDestroy(&box);
331  if (!boxa3) {
332  L_ERROR("boxa3 not found for component %d\n", procName, i);
333  } else {
334  boxa4 = boxaTransform(boxa3, xoff, yoff, 1.0, 1.0);
335  boxaJoin(boxa2, boxa4, 0, -1);
336  boxaDestroy(&boxa3);
337  boxaDestroy(&boxa4);
338  }
339  }
340  }
341  boxaDestroy(&boxa1);
342  if (pixa) { /* debug */
343  pix3 = pixaDisplayTiledInColumns(pixa, 1, 1.0, 20, 2);
344  snprintf(buf, sizeof(buf), "/tmp/lept/recog/decode-%d.png", ind++);
345  pixWrite(buf, pix3, IFF_PNG);
346  pixaDestroy(&pixa);
347  pixDestroy(&pix3);
348  }
349 
350  /* Do a 2D sort on the bounding boxes, and flatten the result to 1D.
351  * For the 2D sort, to add a box to an existing boxa, we require
352  * specified minimum vertical overlaps for the first two passes
353  * of the 2D sort. In pass 1, only components with sufficient
354  * height can start a new boxa. */
355  baa = boxaSort2d(boxa2, NULL, MinOverlap1, MinOverlap2, MinHeightPass1);
356  boxa3 = boxaaFlattenToBoxa(baa, NULL, L_CLONE);
357  boxaaDestroy(&baa);
358  boxaDestroy(&boxa2);
359 
360  /* Remove smaller components of overlapping pairs.
361  * We only remove the small component if the overlap is
362  * at least half its area and if its area is no more
363  * than 30% of the area of the large component. Because the
364  * components are in a flattened 2D sort, we don't need to
365  * look far ahead in the array to find all overlapping boxes;
366  * 10 boxes is plenty. */
367  boxad = boxaHandleOverlaps(boxa3, L_COMBINE, 10, 0.5, 0.3, NULL);
368  boxaDestroy(&boxa3);
369 
370  /* Extract and save the image pieces from the input image. */
371  *ppixa = pixClipRectangles(pixs, boxad);
372  *pboxa = boxad;
373  return 0;
374 }
375 
376 
377 /*------------------------------------------------------------------------*
378  * Greedy character splitting *
379  *------------------------------------------------------------------------*/
400 l_ok
402  PIX *pixs,
403  BOXA **pboxa,
404  NUMA **pnascore,
405  NUMA **pnaindex,
406  SARRAY **psachar,
407  l_int32 debug)
408 {
409 char *charstr;
410 l_int32 index, remove, w, h, bx, bw, bxc, bwc, w1, w2, w3;
411 l_float32 score;
412 BOX *box, *boxc, *boxtrans, *boxl, *boxr, *boxlt, *boxrt;
413 BOXA *boxat;
414 NUMA *nascoret, *naindext, *nasort;
415 PIX *pixb, *pixc, *pixl, *pixr, *pixdb, *pixd;
416 PIXA *pixar, *pixadb;
417 SARRAY *sachart;
418 
419 l_int32 iter;
420 
421  PROCNAME("recogCorrelationBestRow");
422 
423  if (pnascore) *pnascore = NULL;
424  if (pnaindex) *pnaindex = NULL;
425  if (psachar) *psachar = NULL;
426  if (!pboxa)
427  return ERROR_INT("&boxa not defined", procName, 1);
428  *pboxa = NULL;
429  if (!recog)
430  return ERROR_INT("recog not defined", procName, 1);
431  if (!pixs || pixGetDepth(pixs) != 1)
432  return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
433  if (pixGetWidth(pixs) < recog->minwidth_u - 4)
434  return ERROR_INT("pixs too narrow", procName, 1);
435  if (!recog->train_done)
436  return ERROR_INT("training not finished", procName, 1);
437 
438  /* Binarize and crop to foreground if necessary */
439  pixb = recogProcessToIdentify(recog, pixs, 0);
440 
441  /* Initialize the arrays */
442  boxat = boxaCreate(4);
443  nascoret = numaCreate(4);
444  naindext = numaCreate(4);
445  sachart = sarrayCreate(4);
446  pixadb = (debug) ? pixaCreate(4) : NULL;
447 
448  /* Initialize the images remaining to be processed with the input.
449  * These are stored in pixar, which is used here as a queue,
450  * on which we only put image fragments that are large enough to
451  * contain at least one character. */
452  pixar = pixaCreate(1);
453  pixGetDimensions(pixb, &w, &h, NULL);
454  box = boxCreate(0, 0, w, h);
455  pixaAddPix(pixar, pixb, L_INSERT);
456  pixaAddBox(pixar, box, L_INSERT);
457 
458  /* Successively split on the best match until nothing is left.
459  * To be safe, we limit the search to 10 characters. */
460  for (iter = 0; iter < 11; iter++) {
461  if (pixaGetCount(pixar) == 0)
462  break;
463  if (iter == 10) {
464  L_WARNING("more than 10 chars; ending search\n", procName);
465  break;
466  }
467 
468  /* Pop one from the queue */
469  pixaRemovePixAndSave(pixar, 0, &pixc, &boxc);
470  boxGetGeometry(boxc, &bxc, NULL, &bwc, NULL);
471 
472  /* This is a single component; if noise, remove it */
473  recogSplittingFilter(recog, pixc, 0, MinFillFactor, &remove, debug);
474  if (debug)
475  fprintf(stderr, "iter = %d, removed = %d\n", iter, remove);
476  if (remove) {
477  pixDestroy(&pixc);
478  boxDestroy(&boxc);
479  continue;
480  }
481 
482  /* Find the best character match */
483  if (debug) {
484  recogCorrelationBestChar(recog, pixc, &box, &score,
485  &index, &charstr, &pixdb);
486  pixaAddPix(pixadb, pixdb, L_INSERT);
487  } else {
488  recogCorrelationBestChar(recog, pixc, &box, &score,
489  &index, &charstr, NULL);
490  }
491 
492  /* Find the box in original coordinates, and append
493  * the results to the arrays. */
494  boxtrans = boxTransform(box, bxc, 0, 1.0, 1.0);
495  boxaAddBox(boxat, boxtrans, L_INSERT);
496  numaAddNumber(nascoret, score);
497  numaAddNumber(naindext, index);
498  sarrayAddString(sachart, charstr, L_INSERT);
499 
500  /* Split the current pixc into three regions and save
501  * each region if it is large enough. */
502  boxGetGeometry(box, &bx, NULL, &bw, NULL);
503  w1 = bx;
504  w2 = bw;
505  w3 = bwc - bx - bw;
506  if (debug)
507  fprintf(stderr, " w1 = %d, w2 = %d, w3 = %d\n", w1, w2, w3);
508  if (w1 < recog->minwidth_u - 4) {
509  if (debug) L_INFO("discarding width %d on left\n", procName, w1);
510  } else { /* extract and save left region */
511  boxl = boxCreate(0, 0, bx + 1, h);
512  pixl = pixClipRectangle(pixc, boxl, NULL);
513  boxlt = boxTransform(boxl, bxc, 0, 1.0, 1.0);
514  pixaAddPix(pixar, pixl, L_INSERT);
515  pixaAddBox(pixar, boxlt, L_INSERT);
516  boxDestroy(&boxl);
517  }
518  if (w3 < recog->minwidth_u - 4) {
519  if (debug) L_INFO("discarding width %d on right\n", procName, w3);
520  } else { /* extract and save left region */
521  boxr = boxCreate(bx + bw - 1, 0, w3 + 1, h);
522  pixr = pixClipRectangle(pixc, boxr, NULL);
523  boxrt = boxTransform(boxr, bxc, 0, 1.0, 1.0);
524  pixaAddPix(pixar, pixr, L_INSERT);
525  pixaAddBox(pixar, boxrt, L_INSERT);
526  boxDestroy(&boxr);
527  }
528  pixDestroy(&pixc);
529  boxDestroy(&box);
530  boxDestroy(&boxc);
531  }
532  pixaDestroy(&pixar);
533 
534 
535  /* Sort the output results by left-to-right in the boxa */
536  *pboxa = boxaSort(boxat, L_SORT_BY_X, L_SORT_INCREASING, &nasort);
537  if (pnascore)
538  *pnascore = numaSortByIndex(nascoret, nasort);
539  if (pnaindex)
540  *pnaindex = numaSortByIndex(naindext, nasort);
541  if (psachar)
542  *psachar = sarraySortByIndex(sachart, nasort);
543  numaDestroy(&nasort);
544  boxaDestroy(&boxat);
545  numaDestroy(&nascoret);
546  numaDestroy(&naindext);
547  sarrayDestroy(&sachart);
548 
549  /* Final debug output */
550  if (debug) {
551  pixd = pixaDisplayTiledInRows(pixadb, 32, 2000, 1.0, 0, 15, 2);
552  pixDisplay(pixd, 400, 400);
553  pixaAddPix(recog->pixadb_split, pixd, L_INSERT);
554  pixaDestroy(&pixadb);
555  }
556  return 0;
557 }
558 
559 
582 l_ok
584  PIX *pixs,
585  BOX **pbox,
586  l_float32 *pscore,
587  l_int32 *pindex,
588  char **pcharstr,
589  PIX **ppixdb)
590 {
591 l_int32 i, n, w1, h1, w2, area2, ycent2, delx, dely;
592 l_int32 bestdelx, bestdely, bestindex;
593 l_float32 score, bestscore;
594 BOX *box;
595 BOXA *boxa;
596 NUMA *nasum, *namoment;
597 PIX *pix1, *pix2;
598 
599  PROCNAME("recogCorrelationBestChar");
600 
601  if (pindex) *pindex = 0;
602  if (pcharstr) *pcharstr = NULL;
603  if (ppixdb) *ppixdb = NULL;
604  if (pbox) *pbox = NULL;
605  if (pscore) *pscore = 0.0;
606  if (!pbox || !pscore)
607  return ERROR_INT("&box and &score not both defined", procName, 1);
608  if (!recog)
609  return ERROR_INT("recog not defined", procName, 1);
610  if (!pixs || pixGetDepth(pixs) != 1)
611  return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
612  if (!recog->train_done)
613  return ERROR_INT("training not finished", procName, 1);
614 
615  /* Binarize and crop to foreground if necessary. Add padding
616  * to both the left and right side; this is compensated for
617  * when reporting the bounding box of the best matched character. */
618  pix1 = recogProcessToIdentify(recog, pixs, LeftRightPadding);
619  pixGetDimensions(pix1, &w1, &h1, NULL);
620 
621  /* Compute vertical sum and moment arrays */
623  namoment = pixGetMomentByColumn(pix1, 1);
624 
625  /* Do shifted correlation against all averaged templates. */
626  n = recog->setsize;
627  boxa = boxaCreate(n); /* location of best fits for each character */
628  bestscore = 0.0;
629  bestindex = bestdelx = bestdely = 0;
630  for (i = 0; i < n; i++) {
631  pix2 = pixaGetPix(recog->pixa_u, i, L_CLONE);
632  w2 = pixGetWidth(pix2);
633  /* Note that the slightly expended w1 is typically larger
634  * than w2 (the template). */
635  if (w1 >= w2) {
636  numaGetIValue(recog->nasum_u, i, &area2);
637  ptaGetIPt(recog->pta_u, i, NULL, &ycent2);
638  pixCorrelationBestShift(pix1, pix2, nasum, namoment, area2, ycent2,
639  recog->maxyshift, recog->sumtab, &delx,
640  &dely, &score, 1);
641  if (ppixdb) {
642  fprintf(stderr,
643  "Best match template %d: (x,y) = (%d,%d), score = %5.3f\n",
644  i, delx, dely, score);
645  }
646  /* Compensate for padding */
647  box = boxCreate(delx - LeftRightPadding, 0, w2, h1);
648  if (score > bestscore) {
649  bestscore = score;
650  bestdelx = delx - LeftRightPadding;
651  bestdely = dely;
652  bestindex = i;
653  }
654  } else {
655  box = boxCreate(0, 0, 1, 1); /* placeholder */
656  if (ppixdb)
657  fprintf(stderr, "Component too thin: w1 = %d, w2 = %d\n",
658  w1, w2);
659  }
660  boxaAddBox(boxa, box, L_INSERT);
661  pixDestroy(&pix2);
662  }
663 
664  *pscore = bestscore;
665  *pbox = boxaGetBox(boxa, bestindex, L_COPY);
666  if (pindex) *pindex = bestindex;
667  if (pcharstr)
668  recogGetClassString(recog, bestindex, pcharstr);
669 
670  if (ppixdb) {
671  L_INFO("Best match: class %d; shifts (%d, %d)\n",
672  procName, bestindex, bestdelx, bestdely);
673  pix2 = pixaGetPix(recog->pixa_u, bestindex, L_CLONE);
674  *ppixdb = recogShowMatch(recog, pix1, pix2, NULL, -1, 0.0);
675  pixDestroy(&pix2);
676  }
677 
678  pixDestroy(&pix1);
679  boxaDestroy(&boxa);
680  numaDestroy(&nasum);
682  return 0;
683 }
684 
685 
725 static l_int32
727  PIX *pix2,
728  NUMA *nasum1,
729  NUMA *namoment1,
730  l_int32 area2,
731  l_int32 ycent2,
732  l_int32 maxyshift,
733  l_int32 *tab8,
734  l_int32 *pdelx,
735  l_int32 *pdely,
736  l_float32 *pscore,
737  l_int32 debugflag)
738 {
739 l_int32 w1, w2, h1, h2, i, j, nx, shifty, delx, dely;
740 l_int32 sum, moment, count;
741 l_int32 *tab, *area1, *arraysum, *arraymoment;
742 l_float32 maxscore, score;
743 l_float32 *ycent1;
744 FPIX *fpix;
745 PIX *pixt, *pixt1, *pixt2;
746 
747  PROCNAME("pixCorrelationBestShift");
748 
749  if (pdelx) *pdelx = 0;
750  if (pdely) *pdely = 0;
751  if (pscore) *pscore = 0.0;
752  if (!pix1 || pixGetDepth(pix1) != 1)
753  return ERROR_INT("pix1 not defined or not 1 bpp", procName, 1);
754  if (!pix2 || pixGetDepth(pix2) != 1)
755  return ERROR_INT("pix2 not defined or not 1 bpp", procName, 1);
756  if (!nasum1 || !namoment1)
757  return ERROR_INT("nasum1 and namoment1 not both defined", procName, 1);
758  if (area2 <= 0 || ycent2 <= 0)
759  return ERROR_INT("area2 and ycent2 must be > 0", procName, 1);
760 
761  /* If pix1 (the unknown image) is narrower than pix2,
762  * don't bother to try the match. pix1 is already padded with
763  * 2 pixels on each side. */
764  pixGetDimensions(pix1, &w1, &h1, NULL);
765  pixGetDimensions(pix2, &w2, &h2, NULL);
766  if (w1 < w2) {
767  if (debugflag > 0) {
768  L_INFO("skipping match with w1 = %d and w2 = %d\n",
769  procName, w1, w2);
770  }
771  return 0;
772  }
773  nx = w1 - w2 + 1;
774 
775  if (debugflag > 0)
776  fpix = fpixCreate(nx, 2 * maxyshift + 1);
777  if (!tab8)
778  tab = makePixelSumTab8();
779  else
780  tab = tab8;
781 
782  /* Set up the arrays for area1 and ycent1. We have to do this
783  * for each template (pix2) because the window width is w2. */
784  area1 = (l_int32 *)LEPT_CALLOC(nx, sizeof(l_int32));
785  ycent1 = (l_float32 *)LEPT_CALLOC(nx, sizeof(l_int32));
786  arraysum = numaGetIArray(nasum1);
787  arraymoment = numaGetIArray(namoment1);
788  for (i = 0, sum = 0, moment = 0; i < w2; i++) {
789  sum += arraysum[i];
790  moment += arraymoment[i];
791  }
792  for (i = 0; i < nx - 1; i++) {
793  area1[i] = sum;
794  ycent1[i] = (sum == 0) ? ycent2 : (l_float32)moment / (l_float32)sum;
795  sum += arraysum[w2 + i] - arraysum[i];
796  moment += arraymoment[w2 + i] - arraymoment[i];
797  }
798  area1[nx - 1] = sum;
799  ycent1[nx - 1] = (sum == 0) ? ycent2 : (l_float32)moment / (l_float32)sum;
800 
801  /* Find the best match location for pix2. At each location,
802  * to insure that pixels are ON only within the intersection of
803  * pix and the shifted pix2:
804  * (1) Start with pixt cleared and equal in size to pix1.
805  * (2) Blit the shifted pix2 onto pixt. Then all ON pixels
806  * are within the intersection of pix1 and the shifted pix2.
807  * (3) AND pix1 with pixt. */
808  pixt = pixCreate(w2, h1, 1);
809  maxscore = 0;
810  delx = 0;
811  dely = 0; /* amount to shift pix2 relative to pix1 to get alignment */
812  for (i = 0; i < nx; i++) {
813  shifty = (l_int32)(ycent1[i] - ycent2 + 0.5);
814  for (j = -maxyshift; j <= maxyshift; j++) {
815  pixClearAll(pixt);
816  pixRasterop(pixt, 0, shifty + j, w2, h2, PIX_SRC, pix2, 0, 0);
817  pixRasterop(pixt, 0, 0, w2, h1, PIX_SRC & PIX_DST, pix1, i, 0);
818  pixCountPixels(pixt, &count, tab);
819  score = (l_float32)count * (l_float32)count /
820  ((l_float32)area1[i] * (l_float32)area2);
821  if (score > maxscore) {
822  maxscore = score;
823  delx = i;
824  dely = shifty + j;
825  }
826 
827  if (debugflag > 0)
828  fpixSetPixel(fpix, i, maxyshift + j, 1000.0 * score);
829  }
830  }
831 
832  if (debugflag > 0) {
833  lept_mkdir("lept/recog");
834  char buf[128];
835  pixt1 = fpixDisplayMaxDynamicRange(fpix);
836  pixt2 = pixExpandReplicate(pixt1, 5);
837  snprintf(buf, sizeof(buf), "/tmp/lept/recog/junkbs_%d.png", debugflag);
838  pixWrite(buf, pixt2, IFF_PNG);
839  pixDestroy(&pixt1);
840  pixDestroy(&pixt2);
841  fpixDestroy(&fpix);
842  }
843 
844  if (pdelx) *pdelx = delx;
845  if (pdely) *pdely = dely;
846  if (pscore) *pscore = maxscore;
847  if (!tab8) LEPT_FREE(tab);
848  LEPT_FREE(area1);
849  LEPT_FREE(ycent1);
850  LEPT_FREE(arraysum);
851  LEPT_FREE(arraymoment);
852  pixDestroy(&pixt);
853  return 0;
854 }
855 
856 
857 /*------------------------------------------------------------------------*
858  * Low-level identification *
859  *------------------------------------------------------------------------*/
878 l_ok
880  PIXA *pixa,
881  PIX **ppixdb)
882 {
883 char *text;
884 l_int32 i, n, fail, index, depth;
885 l_float32 score;
886 PIX *pix1, *pix2, *pix3;
887 PIXA *pixa1;
888 L_RCH *rch;
889 
890  PROCNAME("recogIdentifyPixa");
891 
892  if (ppixdb) *ppixdb = NULL;
893  if (!recog)
894  return ERROR_INT("recog not defined", procName, 1);
895  if (!pixa)
896  return ERROR_INT("pixa not defined", procName, 1);
897 
898  /* Run the recognizer on the set of images. This writes
899  * the text string into each pix in pixa. */
900  n = pixaGetCount(pixa);
901  rchaDestroy(&recog->rcha);
902  recog->rcha = rchaCreate();
903  pixa1 = (ppixdb) ? pixaCreate(n) : NULL;
904  depth = 1;
905  for (i = 0; i < n; i++) {
906  pix1 = pixaGetPix(pixa, i, L_CLONE);
907  pix2 = NULL;
908  fail = FALSE;
909  if (!ppixdb)
910  fail = recogIdentifyPix(recog, pix1, NULL);
911  else
912  fail = recogIdentifyPix(recog, pix1, &pix2);
913  if (fail)
914  recogSkipIdentify(recog);
915  if ((rch = recog->rch) == NULL) {
916  L_ERROR("rch not found for char %d\n", procName, i);
917  pixDestroy(&pix1);
918  pixDestroy(&pix2);
919  continue;
920  }
921  rchExtract(rch, NULL, NULL, &text, NULL, NULL, NULL, NULL);
922  pixSetText(pix1, text);
923  LEPT_FREE(text);
924  if (ppixdb) {
925  rchExtract(rch, &index, &score, NULL, NULL, NULL, NULL, NULL);
926  pix3 = recogShowMatch(recog, pix2, NULL, NULL, index, score);
927  if (i == 0) depth = pixGetDepth(pix3);
928  pixaAddPix(pixa1, pix3, L_INSERT);
929  pixDestroy(&pix2);
930  }
931  transferRchToRcha(rch, recog->rcha);
932  pixDestroy(&pix1);
933  }
934 
935  /* Package the images for debug */
936  if (ppixdb) {
937  *ppixdb = pixaDisplayTiledInRows(pixa1, depth, 2500, 1.0, 0, 20, 1);
938  pixaDestroy(&pixa1);
939  }
940 
941  return 0;
942 }
943 
944 
971 l_ok
973  PIX *pixs,
974  PIX **ppixdb)
975 {
976 char *text;
977 l_int32 i, j, n, bestindex, bestsample, area1, area2;
978 l_int32 shiftx, shifty, bestdelx, bestdely, bestwidth, maxyshift;
979 l_float32 x1, y1, x2, y2, delx, dely, score, maxscore;
980 NUMA *numa;
981 PIX *pix0, *pix1, *pix2;
982 PIXA *pixa;
983 PTA *pta;
984 
985  PROCNAME("recogIdentifyPix");
986 
987  if (ppixdb) *ppixdb = NULL;
988  if (!recog)
989  return ERROR_INT("recog not defined", procName, 1);
990  if (!pixs || pixGetDepth(pixs) != 1)
991  return ERROR_INT("pixs not defined or not 1 bpp", procName, 1);
992 
993  /* Do the averaging if required and not yet done. */
994  if (recog->templ_use == L_USE_AVERAGE_TEMPLATES && !recog->ave_done) {
995  recogAverageSamples(&recog, 0);
996  if (!recog)
997  return ERROR_INT("averaging failed", procName, 1);
998  }
999 
1000  /* Binarize and crop to foreground if necessary */
1001  if ((pix0 = recogProcessToIdentify(recog, pixs, 0)) == NULL)
1002  return ERROR_INT("no fg pixels in pix0", procName, 1);
1003 
1004  /* Optionally scale and/or convert to fixed stroke width */
1005  pix1 = recogModifyTemplate(recog, pix0);
1006  pixDestroy(&pix0);
1007  if (!pix1)
1008  return ERROR_INT("no fg pixels in pix1", procName, 1);
1009 
1010  /* Do correlation at all positions within +-maxyshift of
1011  * the nominal centroid alignment. */
1012  pixCountPixels(pix1, &area1, recog->sumtab);
1013  pixCentroid(pix1, recog->centtab, recog->sumtab, &x1, &y1);
1014  bestindex = bestsample = bestdelx = bestdely = bestwidth = 0;
1015  maxscore = 0.0;
1016  maxyshift = recog->maxyshift;
1017  if (recog->templ_use == L_USE_AVERAGE_TEMPLATES) {
1018  for (i = 0; i < recog->setsize; i++) {
1019  numaGetIValue(recog->nasum, i, &area2);
1020  if (area2 == 0) continue; /* no template available */
1021  pix2 = pixaGetPix(recog->pixa, i, L_CLONE);
1022  ptaGetPt(recog->pta, i, &x2, &y2);
1023  delx = x1 - x2;
1024  dely = y1 - y2;
1025  for (shifty = -maxyshift; shifty <= maxyshift; shifty++) {
1026  for (shiftx = -maxyshift; shiftx <= maxyshift; shiftx++) {
1027  pixCorrelationScoreSimple(pix1, pix2, area1, area2,
1028  delx + shiftx, dely + shifty,
1029  5, 5, recog->sumtab, &score);
1030  if (score > maxscore) {
1031  bestindex = i;
1032  bestdelx = delx + shiftx;
1033  bestdely = dely + shifty;
1034  maxscore = score;
1035  }
1036  }
1037  }
1038  pixDestroy(&pix2);
1039  }
1040  } else { /* use all the samples */
1041  for (i = 0; i < recog->setsize; i++) {
1042  pixa = pixaaGetPixa(recog->pixaa, i, L_CLONE);
1043  n = pixaGetCount(pixa);
1044  if (n == 0) {
1045  pixaDestroy(&pixa);
1046  continue;
1047  }
1048  numa = numaaGetNuma(recog->naasum, i, L_CLONE);
1049  pta = ptaaGetPta(recog->ptaa, i, L_CLONE);
1050  for (j = 0; j < n; j++) {
1051  pix2 = pixaGetPix(pixa, j, L_CLONE);
1052  numaGetIValue(numa, j, &area2);
1053  ptaGetPt(pta, j, &x2, &y2);
1054  delx = x1 - x2;
1055  dely = y1 - y2;
1056  for (shifty = -maxyshift; shifty <= maxyshift; shifty++) {
1057  for (shiftx = -maxyshift; shiftx <= maxyshift; shiftx++) {
1058  pixCorrelationScoreSimple(pix1, pix2, area1, area2,
1059  delx + shiftx, dely + shifty,
1060  5, 5, recog->sumtab, &score);
1061  if (score > maxscore) {
1062  bestindex = i;
1063  bestsample = j;
1064  bestdelx = delx + shiftx;
1065  bestdely = dely + shifty;
1066  maxscore = score;
1067  bestwidth = pixGetWidth(pix2);
1068  }
1069  }
1070  }
1071  pixDestroy(&pix2);
1072  }
1073  pixaDestroy(&pixa);
1074  numaDestroy(&numa);
1075  ptaDestroy(&pta);
1076  }
1077  }
1078 
1079  /* Package up the results */
1080  recogGetClassString(recog, bestindex, &text);
1081  rchDestroy(&recog->rch);
1082  recog->rch = rchCreate(bestindex, maxscore, text, bestsample,
1083  bestdelx, bestdely, bestwidth);
1084 
1085  if (ppixdb) {
1086  if (recog->templ_use == L_USE_AVERAGE_TEMPLATES) {
1087  L_INFO("Best match: str %s; class %d; sh (%d, %d); score %5.3f\n",
1088  procName, text, bestindex, bestdelx, bestdely, maxscore);
1089  pix2 = pixaGetPix(recog->pixa, bestindex, L_CLONE);
1090  } else { /* L_USE_ALL_TEMPLATES */
1091  L_INFO("Best match: str %s; sample %d in class %d; score %5.3f\n",
1092  procName, text, bestsample, bestindex, maxscore);
1093  if (maxyshift > 0 && (L_ABS(bestdelx) > 0 || L_ABS(bestdely) > 0)) {
1094  L_INFO(" Best shift: (%d, %d)\n",
1095  procName, bestdelx, bestdely);
1096  }
1097  pix2 = pixaaGetPix(recog->pixaa, bestindex, bestsample, L_CLONE);
1098  }
1099  *ppixdb = recogShowMatch(recog, pix1, pix2, NULL, -1, 0.0);
1100  pixDestroy(&pix2);
1101  }
1102 
1103  pixDestroy(&pix1);
1104  return 0;
1105 }
1106 
1107 
1120 l_ok
1122 {
1123  PROCNAME("recogSkipIdentify");
1124 
1125  if (!recog)
1126  return ERROR_INT("recog not defined", procName, 1);
1127 
1128  /* Package up placeholder results */
1129  rchDestroy(&recog->rch);
1130  recog->rch = rchCreate(0, 0.0, stringNew(""), 0, 0, 0, 0);
1131  return 0;
1132 }
1133 
1134 
1135 /*------------------------------------------------------------------------*
1136  * Operations for handling identification results *
1137  *------------------------------------------------------------------------*/
1146 static L_RCHA *
1148 {
1149 L_RCHA *rcha;
1150 
1151  rcha = (L_RCHA *)LEPT_CALLOC(1, sizeof(L_RCHA));
1152  rcha->naindex = numaCreate(0);
1153  rcha->nascore = numaCreate(0);
1154  rcha->satext = sarrayCreate(0);
1155  rcha->nasample = numaCreate(0);
1156  rcha->naxloc = numaCreate(0);
1157  rcha->nayloc = numaCreate(0);
1158  rcha->nawidth = numaCreate(0);
1159  return rcha;
1160 }
1161 
1162 
1168 void
1170 {
1171 L_RCHA *rcha;
1172 
1173  PROCNAME("rchaDestroy");
1174 
1175  if (prcha == NULL) {
1176  L_WARNING("&rcha is null!\n", procName);
1177  return;
1178  }
1179  if ((rcha = *prcha) == NULL)
1180  return;
1181 
1182  numaDestroy(&rcha->naindex);
1183  numaDestroy(&rcha->nascore);
1184  sarrayDestroy(&rcha->satext);
1185  numaDestroy(&rcha->nasample);
1186  numaDestroy(&rcha->naxloc);
1187  numaDestroy(&rcha->nayloc);
1188  numaDestroy(&rcha->nawidth);
1189  LEPT_FREE(rcha);
1190  *prcha = NULL;
1191  return;
1192 }
1193 
1194 
1214 static L_RCH *
1215 rchCreate(l_int32 index,
1216  l_float32 score,
1217  char *text,
1218  l_int32 sample,
1219  l_int32 xloc,
1220  l_int32 yloc,
1221  l_int32 width)
1222 {
1223 L_RCH *rch;
1224 
1225  rch = (L_RCH *)LEPT_CALLOC(1, sizeof(L_RCH));
1226  rch->index = index;
1227  rch->score = score;
1228  rch->text = text;
1229  rch->sample = sample;
1230  rch->xloc = xloc;
1231  rch->yloc = yloc;
1232  rch->width = width;
1233  return rch;
1234 }
1235 
1236 
1242 void
1244 {
1245 L_RCH *rch;
1246 
1247  PROCNAME("rchDestroy");
1248 
1249  if (prch == NULL) {
1250  L_WARNING("&rch is null!\n", procName);
1251  return;
1252  }
1253  if ((rch = *prch) == NULL)
1254  return;
1255  LEPT_FREE(rch->text);
1256  LEPT_FREE(rch);
1257  *prch = NULL;
1258  return;
1259 }
1260 
1261 
1281 l_ok
1283  NUMA **pnaindex,
1284  NUMA **pnascore,
1285  SARRAY **psatext,
1286  NUMA **pnasample,
1287  NUMA **pnaxloc,
1288  NUMA **pnayloc,
1289  NUMA **pnawidth)
1290 {
1291  PROCNAME("rchaExtract");
1292 
1293  if (pnaindex) *pnaindex = NULL;
1294  if (pnascore) *pnascore = NULL;
1295  if (psatext) *psatext = NULL;
1296  if (pnasample) *pnasample = NULL;
1297  if (pnaxloc) *pnaxloc = NULL;
1298  if (pnayloc) *pnayloc = NULL;
1299  if (pnawidth) *pnawidth = NULL;
1300  if (!rcha)
1301  return ERROR_INT("rcha not defined", procName, 1);
1302 
1303  if (pnaindex) *pnaindex = numaClone(rcha->naindex);
1304  if (pnascore) *pnascore = numaClone(rcha->nascore);
1305  if (psatext) *psatext = sarrayClone(rcha->satext);
1306  if (pnasample) *pnasample = numaClone(rcha->nasample);
1307  if (pnaxloc) *pnaxloc = numaClone(rcha->naxloc);
1308  if (pnayloc) *pnayloc = numaClone(rcha->nayloc);
1309  if (pnawidth) *pnawidth = numaClone(rcha->nawidth);
1310  return 0;
1311 }
1312 
1313 
1327 l_ok
1329  l_int32 *pindex,
1330  l_float32 *pscore,
1331  char **ptext,
1332  l_int32 *psample,
1333  l_int32 *pxloc,
1334  l_int32 *pyloc,
1335  l_int32 *pwidth)
1336 {
1337  PROCNAME("rchExtract");
1338 
1339  if (pindex) *pindex = 0;
1340  if (pscore) *pscore = 0.0;
1341  if (ptext) *ptext = NULL;
1342  if (psample) *psample = 0;
1343  if (pxloc) *pxloc = 0;
1344  if (pyloc) *pyloc = 0;
1345  if (pwidth) *pwidth = 0;
1346  if (!rch)
1347  return ERROR_INT("rch not defined", procName, 1);
1348 
1349  if (pindex) *pindex = rch->index;
1350  if (pscore) *pscore = rch->score;
1351  if (ptext) *ptext = stringNew(rch->text); /* new string: owned by caller */
1352  if (psample) *psample = rch->sample;
1353  if (pxloc) *pxloc = rch->xloc;
1354  if (pyloc) *pyloc = rch->yloc;
1355  if (pwidth) *pwidth = rch->width;
1356  return 0;
1357 }
1358 
1359 
1373 static l_int32
1375  L_RCHA *rcha)
1376 {
1377 
1378  PROCNAME("transferRchToRcha");
1379 
1380  if (!rch)
1381  return ERROR_INT("rch not defined", procName, 1);
1382  if (!rcha)
1383  return ERROR_INT("rcha not defined", procName, 1);
1384 
1385  numaAddNumber(rcha->naindex, rch->index);
1386  numaAddNumber(rcha->nascore, rch->score);
1387  sarrayAddString(rcha->satext, rch->text, L_COPY);
1388  numaAddNumber(rcha->nasample, rch->sample);
1389  numaAddNumber(rcha->naxloc, rch->xloc);
1390  numaAddNumber(rcha->nayloc, rch->yloc);
1391  numaAddNumber(rcha->nawidth, rch->width);
1392  return 0;
1393 }
1394 
1395 
1396 /*------------------------------------------------------------------------*
1397  * Preprocessing and filtering *
1398  *------------------------------------------------------------------------*/
1415 PIX *
1417  PIX *pixs,
1418  l_int32 pad)
1419 {
1420 l_int32 canclip;
1421 PIX *pix1, *pix2, *pixd;
1422 
1423  PROCNAME("recogProcessToIdentify");
1424 
1425  if (!recog)
1426  return (PIX *)ERROR_PTR("recog not defined", procName, NULL);
1427  if (!pixs)
1428  return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
1429 
1430  if (pixGetDepth(pixs) != 1)
1431  pix1 = pixThresholdToBinary(pixs, recog->threshold);
1432  else
1433  pix1 = pixClone(pixs);
1434  pixTestClipToForeground(pix1, &canclip);
1435  if (canclip)
1436  pixClipToForeground(pix1, &pix2, NULL);
1437  else
1438  pix2 = pixClone(pix1);
1439  pixDestroy(&pix1);
1440  if (!pix2)
1441  return (PIX *)ERROR_PTR("no foreground pixels", procName, NULL);
1442 
1443  pixd = pixAddBorderGeneral(pix2, pad, pad, 0, 0, 0);
1444  pixDestroy(&pix2);
1445  return pixd;
1446 }
1447 
1448 
1459 static PIX *
1461  PIX *pixs,
1462  l_int32 minh,
1463  l_float32 minaf,
1464  l_int32 debug)
1465 {
1466 l_int32 scaling, minsplitw, maxsplith, maxasp;
1467 BOXA *boxas;
1468 NUMA *naw, *nah, *na1, *na1c, *na2, *na3, *na4, *na5, *na6, *na7;
1469 PIX *pixd;
1470 PIXA *pixas;
1471 
1472  PROCNAME("recogPreSplittingFilter");
1473 
1474  if (!recog)
1475  return (PIX *)ERROR_PTR("recog not defined", procName, NULL);
1476  if (!pixs)
1477  return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
1478 
1479  /* If there is scaling, do not remove components based on the
1480  * values of min_splitw and max_splith. */
1481  scaling = (recog->scalew > 0 || recog->scaleh > 0) ? TRUE : FALSE;
1482  minsplitw = (scaling) ? 1 : recog->min_splitw - 3;
1483  maxsplith = (scaling) ? 150 : recog->max_splith;
1484  maxasp = recog->max_wh_ratio;
1485 
1486  /* Generate an indicator array of connected components to remove:
1487  * short stuff
1488  * tall stuff
1489  * components with large width/height ratio
1490  * components with small area fill fraction */
1491  boxas = pixConnComp(pixs, &pixas, 8);
1492  pixaFindDimensions(pixas, &naw, &nah);
1493  na1 = numaMakeThresholdIndicator(naw, minsplitw, L_SELECT_IF_LT);
1494  na1c = numaCopy(na1);
1495  na2 = numaMakeThresholdIndicator(nah, minh, L_SELECT_IF_LT);
1496  na3 = numaMakeThresholdIndicator(nah, maxsplith, L_SELECT_IF_GT);
1497  na4 = pixaFindWidthHeightRatio(pixas);
1498  na5 = numaMakeThresholdIndicator(na4, maxasp, L_SELECT_IF_GT);
1499  na6 = pixaFindAreaFraction(pixas);
1500  na7 = numaMakeThresholdIndicator(na6, minaf, L_SELECT_IF_LT);
1501  numaLogicalOp(na1, na1, na2, L_UNION);
1502  numaLogicalOp(na1, na1, na3, L_UNION);
1503  numaLogicalOp(na1, na1, na5, L_UNION);
1504  numaLogicalOp(na1, na1, na7, L_UNION);
1505  pixd = pixCopy(NULL, pixs);
1506  pixRemoveWithIndicator(pixd, pixas, na1);
1507  if (debug)
1508  l_showIndicatorSplitValues(na1c, na2, na3, na5, na7, na1);
1509  numaDestroy(&naw);
1510  numaDestroy(&nah);
1511  numaDestroy(&na1);
1512  numaDestroy(&na1c);
1513  numaDestroy(&na2);
1514  numaDestroy(&na3);
1515  numaDestroy(&na4);
1516  numaDestroy(&na5);
1517  numaDestroy(&na6);
1518  numaDestroy(&na7);
1519  boxaDestroy(&boxas);
1520  pixaDestroy(&pixas);
1521  return pixd;
1522 }
1523 
1524 
1536 static l_int32
1538  PIX *pixs,
1539  l_int32 minh,
1540  l_float32 minaf,
1541  l_int32 *premove,
1542  l_int32 debug)
1543 {
1544 l_int32 w, h;
1545 l_float32 aspratio, fract;
1546 
1547  PROCNAME("recogSplittingFilter");
1548 
1549  if (!premove)
1550  return ERROR_INT("&remove not defined", procName, 1);
1551  *premove = 0;
1552  if (!recog)
1553  return ERROR_INT("recog not defined", procName, 1);
1554  if (!pixs)
1555  return ERROR_INT("pixs not defined", procName, 1);
1556  if (minh <= 0) minh = DefaultMinHeight;
1557 
1558  /* Remove from further consideration:
1559  * small stuff
1560  * components with large width/height ratio
1561  * components with small area fill fraction */
1562  pixGetDimensions(pixs, &w, &h, NULL);
1563  if (w < recog->min_splitw) {
1564  if (debug) L_INFO("w = %d < %d\n", procName, w, recog->min_splitw);
1565  *premove = 1;
1566  return 0;
1567  }
1568  if (h < minh) {
1569  if (debug) L_INFO("h = %d < %d\n", procName, h, minh);
1570  *premove = 1;
1571  return 0;
1572  }
1573  aspratio = (l_float32)w / (l_float32)h;
1574  if (aspratio > recog->max_wh_ratio) {
1575  if (debug) L_INFO("w/h = %5.3f too large\n", procName, aspratio);
1576  *premove = 1;
1577  return 0;
1578  }
1579  pixFindAreaFraction(pixs, recog->sumtab, &fract);
1580  if (fract < minaf) {
1581  if (debug) L_INFO("area fill fract %5.3f < %5.3f\n",
1582  procName, fract, minaf);
1583  *premove = 1;
1584  return 0;
1585  }
1586 
1587  return 0;
1588 }
1589 
1590 
1591 /*------------------------------------------------------------------------*
1592  * Postprocessing *
1593  *------------------------------------------------------------------------*/
1628 SARRAY *
1630  BOXA *boxas,
1631  l_float32 scorethresh,
1632  l_int32 spacethresh,
1633  BOXAA **pbaa,
1634  NUMAA **pnaa)
1635 {
1636 char *str, *text;
1637 l_int32 i, n, x1, x2, h_sep, v_sep;
1638 l_float32 score;
1639 BOX *box, *prebox;
1640 BOXA *ba;
1641 BOXAA *baa;
1642 NUMA *nascore, *na;
1643 NUMAA *naa;
1644 SARRAY *satext, *sa, *saout;
1645 
1646  PROCNAME("recogExtractNumbers");
1647 
1648  if (pbaa) *pbaa = NULL;
1649  if (pnaa) *pnaa = NULL;
1650  if (!recog || !recog->rcha)
1651  return (SARRAY *)ERROR_PTR("recog and rcha not both defined",
1652  procName, NULL);
1653  if (!boxas)
1654  return (SARRAY *)ERROR_PTR("boxas not defined", procName, NULL);
1655 
1656  if (spacethresh < 0)
1657  spacethresh = L_MAX(recog->maxheight_u, 20);
1658  rchaExtract(recog->rcha, NULL, &nascore, &satext, NULL, NULL, NULL, NULL);
1659  if (!nascore || !satext) {
1660  numaDestroy(&nascore);
1661  sarrayDestroy(&satext);
1662  return (SARRAY *)ERROR_PTR("nascore and satext not both returned",
1663  procName, NULL);
1664  }
1665 
1666  saout = sarrayCreate(0);
1667  naa = numaaCreate(0);
1668  baa = boxaaCreate(0);
1669  prebox = NULL;
1670  n = numaGetCount(nascore);
1671  for (i = 0; i < n; i++) {
1672  numaGetFValue(nascore, i, &score);
1673  text = sarrayGetString(satext, i, L_NOCOPY);
1674  if (prebox == NULL) { /* no current run */
1675  if (score < scorethresh) {
1676  continue;
1677  } else { /* start a number run */
1678  sa = sarrayCreate(0);
1679  ba = boxaCreate(0);
1680  na = numaCreate(0);
1681  sarrayAddString(sa, text, L_COPY);
1682  prebox = boxaGetBox(boxas, i, L_CLONE);
1683  boxaAddBox(ba, prebox, L_COPY);
1684  numaAddNumber(na, score);
1685  }
1686  } else { /* in a current number run */
1687  box = boxaGetBox(boxas, i, L_CLONE);
1688  boxGetGeometry(prebox, &x1, NULL, NULL, NULL);
1689  boxGetGeometry(box, &x2, NULL, NULL, NULL);
1690  boxSeparationDistance(box, prebox, &h_sep, &v_sep);
1691  boxDestroy(&prebox);
1692  if (x1 < x2 && h_sep <= spacethresh &&
1693  v_sep < 0 && score >= scorethresh) { /* add to number */
1694  sarrayAddString(sa, text, L_COPY);
1695  boxaAddBox(ba, box, L_COPY);
1696  numaAddNumber(na, score);
1697  prebox = box;
1698  } else { /* save the completed number */
1699  str = sarrayToString(sa, 0);
1700  sarrayAddString(saout, str, L_INSERT);
1701  sarrayDestroy(&sa);
1702  boxaaAddBoxa(baa, ba, L_INSERT);
1703  numaaAddNuma(naa, na, L_INSERT);
1704  boxDestroy(&box);
1705  if (score >= scorethresh) { /* start a new number */
1706  i--;
1707  continue;
1708  }
1709  }
1710  }
1711  }
1712 
1713  if (prebox) { /* save the last number */
1714  str = sarrayToString(sa, 0);
1715  sarrayAddString(saout, str, L_INSERT);
1716  boxaaAddBoxa(baa, ba, L_INSERT);
1717  numaaAddNuma(naa, na, L_INSERT);
1718  sarrayDestroy(&sa);
1719  boxDestroy(&prebox);
1720  }
1721 
1722  numaDestroy(&nascore);
1723  sarrayDestroy(&satext);
1724  if (sarrayGetCount(saout) == 0) {
1725  sarrayDestroy(&saout);
1726  boxaaDestroy(&baa);
1727  numaaDestroy(&naa);
1728  L_INFO("saout has no identified text\n", procName);
1729  return NULL;
1730  }
1731 
1732  if (pbaa)
1733  *pbaa = baa;
1734  else
1735  boxaaDestroy(&baa);
1736  if (pnaa)
1737  *pnaa = naa;
1738  else
1739  numaaDestroy(&naa);
1740  return saout;
1741 }
1742 
1761 PIXA *
1763  SARRAY *sa,
1764  BOXAA *baa,
1765  NUMAA *naa,
1766  PIX **ppixdb)
1767 {
1768 char buf[128];
1769 char *textstr, *scorestr;
1770 l_int32 i, j, n, nchar, len;
1771 l_float32 score;
1772 L_BMF *bmf;
1773 BOX *box1, *box2;
1774 BOXA *ba;
1775 NUMA *na;
1776 PIX *pix1, *pix2, *pix3, *pix4;
1777 PIXA *pixa;
1778 
1779  PROCNAME("showExtractNumbers");
1780 
1781  if (ppixdb) *ppixdb = NULL;
1782  if (!pixs)
1783  return (PIXA *)ERROR_PTR("pixs not defined", procName, NULL);
1784  if (!sa)
1785  return (PIXA *)ERROR_PTR("sa not defined", procName, NULL);
1786  if (!baa)
1787  return (PIXA *)ERROR_PTR("baa not defined", procName, NULL);
1788  if (!naa)
1789  return (PIXA *)ERROR_PTR("naa not defined", procName, NULL);
1790 
1791  n = sarrayGetCount(sa);
1792  pixa = pixaCreate(n);
1793  bmf = bmfCreate(NULL, 6);
1794  if (ppixdb) *ppixdb = pixConvertTo8(pixs, 1);
1795  for (i = 0; i < n; i++) {
1796  textstr = sarrayGetString(sa, i, L_NOCOPY);
1797  ba = boxaaGetBoxa(baa, i, L_CLONE);
1798  na = numaaGetNuma(naa, i, L_CLONE);
1799  boxaGetExtent(ba, NULL, NULL, &box1);
1800  box2 = boxAdjustSides(NULL, box1, -5, 5, -5, 5);
1801  if (ppixdb) pixRenderBoxArb(*ppixdb, box2, 3, 255, 0, 0);
1802  pix1 = pixClipRectangle(pixs, box1, NULL);
1803  len = strlen(textstr) + 1;
1804  pix2 = pixAddBlackOrWhiteBorder(pix1, 14 * len, 14 * len,
1805  5, 3, L_SET_WHITE);
1806  pix3 = pixConvertTo8(pix2, 1);
1807  nchar = numaGetCount(na);
1808  scorestr = NULL;
1809  for (j = 0; j < nchar; j++) {
1810  numaGetFValue(na, j, &score);
1811  snprintf(buf, sizeof(buf), "%d", (l_int32)(100 * score));
1812  stringJoinIP(&scorestr, buf);
1813  if (j < nchar - 1) stringJoinIP(&scorestr, ",");
1814  }
1815  snprintf(buf, sizeof(buf), "%s: %s\n", textstr, scorestr);
1816  pix4 = pixAddTextlines(pix3, bmf, buf, 0xff000000, L_ADD_BELOW);
1817  pixaAddPix(pixa, pix4, L_INSERT);
1818  boxDestroy(&box1);
1819  boxDestroy(&box2);
1820  pixDestroy(&pix1);
1821  pixDestroy(&pix2);
1822  pixDestroy(&pix3);
1823  boxaDestroy(&ba);
1824  numaDestroy(&na);
1825  LEPT_FREE(scorestr);
1826  }
1827 
1828  bmfDestroy(&bmf);
1829  return pixa;
1830 }
1831 
1832 
1833 /*------------------------------------------------------------------------*
1834  * Static debug helper *
1835  *------------------------------------------------------------------------*/
1848 static void
1850  NUMA *na2,
1851  NUMA *na3,
1852  NUMA *na4,
1853  NUMA *na5,
1854  NUMA *na6)
1855 {
1856 l_int32 i, n;
1857 
1858  n = numaGetCount(na1);
1859  fprintf(stderr, "================================================\n");
1860  fprintf(stderr, "lt minw: ");
1861  for (i = 0; i < n; i++)
1862  fprintf(stderr, "%4d ", (l_int32)na1->array[i]);
1863  fprintf(stderr, "\nlt minh: ");
1864  for (i = 0; i < n; i++)
1865  fprintf(stderr, "%4d ", (l_int32)na2->array[i]);
1866  fprintf(stderr, "\ngt maxh: ");
1867  for (i = 0; i < n; i++)
1868  fprintf(stderr, "%4d ", (l_int32)na3->array[i]);
1869  fprintf(stderr, "\ngt maxasp: ");
1870  for (i = 0; i < n; i++)
1871  fprintf(stderr, "%4d ", (l_int32)na4->array[i]);
1872  fprintf(stderr, "\nlt minaf: ");
1873  for (i = 0; i < n; i++)
1874  fprintf(stderr, "%4d ", (l_int32)na5->array[i]);
1875  fprintf(stderr, "\n------------------------------------------------");
1876  fprintf(stderr, "\nresult: ");
1877  for (i = 0; i < n; i++)
1878  fprintf(stderr, "%4d ", (l_int32)na6->array[i]);
1879  fprintf(stderr, "\n================================================\n");
1880 }
struct Numa * nasum
Definition: recog.h:218
void bmfDestroy(L_BMF **pbmf)
bmfDestroy()
Definition: bmf.c:166
PIX * pixConvertTo1(PIX *pixs, l_int32 threshold)
pixConvertTo1()
Definition: pixconv.c:2933
l_int32 index
Definition: recog.h:183
l_ok rchaExtract(L_RCHA *rcha, NUMA **pnaindex, NUMA **pnascore, SARRAY **psatext, NUMA **pnasample, NUMA **pnaxloc, NUMA **pnayloc, NUMA **pnawidth)
rchaExtract()
Definition: recogident.c:1282
void rchaDestroy(L_RCHA **prcha)
rchaDestroy()
Definition: recogident.c:1169
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
Definition: numabasic.c:692
NUMA * pixCountPixelsByColumn(PIX *pix)
pixCountPixelsByColumn()
Definition: pix3.c:2063
l_int32 width
Definition: recog.h:190
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
Definition: utils2.c:1944
char * sarrayToString(SARRAY *sa, l_int32 addnlflag)
sarrayToString()
Definition: sarray1.c:763
l_ok boxaJoin(BOXA *boxad, BOXA *boxas, l_int32 istart, l_int32 iend)
boxaJoin()
Definition: boxfunc1.c:2351
Definition: pix.h:717
l_int32 threshold
Definition: recog.h:128
struct Pixa * pixa_u
Definition: recog.h:158
l_int32 minwidth_u
Definition: recog.h:135
l_ok pixCentroid(PIX *pix, l_int32 *centtab, l_int32 *sumtab, l_float32 *pxave, l_float32 *pyave)
pixCentroid()
Definition: morphapp.c:1528
l_ok recogSkipIdentify(L_RECOG *recog)
recogSkipIdentify()
Definition: recogident.c:1121
NUMA * pixaFindAreaFraction(PIXA *pixa)
pixaFindAreaFraction()
Definition: pix5.c:435
BOXA * boxaSort(BOXA *boxas, l_int32 sorttype, l_int32 sortorder, NUMA **pnaindex)
boxaSort()
Definition: boxfunc2.c:568
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
Definition: pixconv.c:3233
l_int32 xloc
Definition: recog.h:188
l_ok recogSplitIntoCharacters(L_RECOG *recog, PIX *pixs, l_int32 minh, l_int32 skipsplit, BOXA **pboxa, PIXA **ppixa, l_int32 debug)
recogSplitIntoCharacters()
Definition: recogident.c:246
NUMA * numaSortByIndex(NUMA *nas, NUMA *naindex)
numaSortByIndex()
Definition: numafunc1.c:2786
struct Pta * pta
Definition: recog.h:162
struct Sarray * satext
Definition: recog.h:200
struct Numa * nayloc
Definition: recog.h:203
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
Definition: numabasic.c:473
PIXA * pixaCreate(l_int32 n)
pixaCreate()
Definition: pixabasic.c:163
l_ok pixaRemovePixAndSave(PIXA *pixa, l_int32 index, PIX **ppix, BOX **pbox)
pixaRemovePixAndSave()
Definition: pixabasic.c:1451
Definition: recog.h:116
Definition: pix.h:716
l_int32 scalew
Definition: recog.h:117
char * stringNew(const char *src)
stringNew()
Definition: utils2.c:215
struct Pixa * pixadb_split
Definition: recog.h:170
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()
Definition: rop.c:193
PIXA * pixaaGetPixa(PIXAA *paa, l_int32 index, l_int32 accesstype)
pixaaGetPixa()
Definition: pixabasic.c:2168
l_int32 recogGetClassString(L_RECOG *recog, l_int32 index, char **pcharstr)
recogGetClassString()
Definition: recogbasic.c:752
struct Numa * nasum
Definition: recog.h:163
struct Numa * nasum_u
Definition: recog.h:160
PIX * pixConvertTo8(PIX *pixs, l_int32 cmapflag)
pixConvertTo8()
Definition: pixconv.c:3041
struct Numa * nasample
Definition: recog.h:201
static l_int32 recogSplittingFilter(L_RECOG *recog, PIX *pixs, l_int32 min, l_float32 minaf, l_int32 *premove, l_int32 debug)
recogSplittingFilter()
Definition: recogident.c:1537
void rchDestroy(L_RCH **prch)
rchDestroy()
Definition: recogident.c:1243
static L_RCHA * rchaCreate()
rchaCreate()
Definition: recogident.c:1147
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
Definition: pix1.c:302
static L_RCH * rchCreate(l_int32 index, l_float32 score, char *text, l_int32 sample, l_int32 xloc, l_int32 yloc, l_int32 width)
rchCreate()
Definition: recogident.c:1215
SARRAY * sarrayCreate(l_int32 n)
sarrayCreate()
Definition: sarray1.c:163
BOXA * recogDecode(L_RECOG *recog, PIX *pixs, l_int32 nlevels, PIX **ppixdb)
recogDecode()
Definition: recogdid.c:215
NUMA * pixaFindWidthHeightRatio(PIXA *pixa)
pixaFindWidthHeightRatio()
Definition: pix5.c:664
l_float32 max_wh_ratio
Definition: recog.h:144
NUMA * numaCreate(l_int32 n)
numaCreate()
Definition: numabasic.c:187
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
Definition: boxbasic.c:580
PIX * pixAddBlackOrWhiteBorder(PIX *pixs, l_int32 left, l_int32 right, l_int32 top, l_int32 bot, l_int32 op)
pixAddBlackOrWhiteBorder()
Definition: pix2.c:1788
BOX * boxTransform(BOX *box, l_int32 shiftx, l_int32 shifty, l_float32 scalex, l_float32 scaley)
boxTransform()
Definition: boxfunc2.c:141
PIX * pixThresholdToBinary(PIX *pixs, l_int32 thresh)
pixThresholdToBinary()
Definition: grayquant.c:443
BOXA * boxaHandleOverlaps(BOXA *boxas, l_int32 op, l_int32 range, l_float32 min_overlap, l_float32 max_ratio, NUMA **pnamap)
boxaHandleOverlaps()
Definition: boxfunc1.c:853
SARRAY * recogExtractNumbers(L_RECOG *recog, BOXA *boxas, l_float32 scorethresh, l_int32 spacethresh, BOXAA **pbaa, NUMAA **pnaa)
recogExtractNumbers()
Definition: recogident.c:1629
NUMAA * numaaCreate(l_int32 n)
numaaCreate()
Definition: numabasic.c:1340
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
Definition: pix5.c:1020
l_ok recogCorrelationBestChar(L_RECOG *recog, PIX *pixs, BOX **pbox, l_float32 *pscore, l_int32 *pindex, char **pcharstr, PIX **ppixdb)
recogCorrelationBestChar()
Definition: recogident.c:583
struct Numaa * naasum
Definition: recog.h:157
Definition: pix.h:492
PIX * pixaDisplayTiledInColumns(PIXA *pixas, l_int32 nx, l_float32 scalefactor, l_int32 spacing, l_int32 border)
pixaDisplayTiledInColumns()
Definition: pixafunc2.c:1020
static l_int32 transferRchToRcha(L_RCH *rch, L_RCHA *rcha)
transferRchToRcha()
Definition: recogident.c:1374
struct Boxa * boxa
Definition: recog.h:229
struct L_Rcha * rcha
Definition: recog.h:175
void numaaDestroy(NUMAA **pnaa)
numaaDestroy()
Definition: numabasic.c:1444
Definition: bmf.h:45
PIX * pixaaGetPix(PIXAA *paa, l_int32 index, l_int32 ipix, l_int32 accessflag)
pixaaGetPix()
Definition: pixabasic.c:2230
Definition: array.h:116
l_ok fpixSetPixel(FPIX *fpix, l_int32 x, l_int32 y, l_float32 val)
fpixSetPixel()
Definition: fpix1.c:683
struct Numa * nawidth
Definition: recog.h:204
l_ok pixSetText(PIX *pix, const char *textstring)
pixSetText()
Definition: pix1.c:1483
Definition: pix.h:502
struct Pix * pixs
Definition: recog.h:212
l_ok pixClipToForeground(PIX *pixs, PIX **ppixd, BOX **pbox)
pixClipToForeground()
Definition: pix5.c:1660
struct Numa * naxloc
Definition: recog.h:202
l_ok boxSeparationDistance(BOX *box1, BOX *box2, l_int32 *ph_sep, l_int32 *pv_sep)
boxSeparationDistance()
Definition: boxfunc1.c:973
PIX * fpixDisplayMaxDynamicRange(FPIX *fpixs)
fpixDisplayMaxDynamicRange()
Definition: fpix2.c:424
l_int32 * numaGetIArray(NUMA *na)
numaGetIArray()
Definition: numabasic.c:820
l_ok pixaFindDimensions(PIXA *pixa, NUMA **pnaw, NUMA **pnah)
pixaFindDimensions()
Definition: pix5.c:134
l_ok boxaaAddBoxa(BOXAA *baa, BOXA *ba, l_int32 copyflag)
boxaaAddBoxa()
Definition: boxbasic.c:1327
BOXA * pixConnComp(PIX *pixs, PIXA **ppixa, l_int32 connectivity)
pixConnComp()
Definition: conncomp.c:147
NUMA * numaaGetNuma(NUMAA *naa, l_int32 index, l_int32 accessflag)
numaaGetNuma()
Definition: numabasic.c:1659
PTA * ptaaGetPta(PTAA *ptaa, l_int32 index, l_int32 accessflag)
ptaaGetPta()
Definition: ptabasic.c:1094
static void l_showIndicatorSplitValues(NUMA *na1, NUMA *na2, NUMA *na3, NUMA *na4, NUMA *na5, NUMA *na6)
l_showIndicatorSplitValues()
Definition: recogident.c:1849
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
Definition: numabasic.c:727
l_int32 setsize
Definition: recog.h:127
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
Definition: pixabasic.c:503
Definition: array.h:59
SARRAY * sarraySortByIndex(SARRAY *sain, NUMA *naindex)
sarraySortByIndex()
Definition: sarray2.c:145
void boxaaDestroy(BOXAA **pbaa)
boxaaDestroy()
Definition: boxbasic.c:1289
l_ok recogIdentifyPix(L_RECOG *recog, PIX *pixs, PIX **ppixdb)
recogIdentifyPix()
Definition: recogident.c:972
BOXA * boxaTransform(BOXA *boxas, l_int32 shiftx, l_int32 shifty, l_float32 scalex, l_float32 scaley)
boxaTransform()
Definition: boxfunc2.c:93
l_int32 numaGetCount(NUMA *na)
numaGetCount()
Definition: numabasic.c:631
l_int32 * sumtab
Definition: recog.h:151
l_ok sarrayAddString(SARRAY *sa, const char *string, l_int32 copyflag)
sarrayAddString()
Definition: sarray1.c:446
l_int32 max_splith
Definition: recog.h:147
l_ok pixClearAll(PIX *pix)
pixClearAll()
Definition: pix2.c:712
struct Numa * nascore
Definition: recog.h:230
l_int32 scaleh
Definition: recog.h:119
struct Numa * naindex
Definition: recog.h:198
PIX * pixaDisplayTiledInRows(PIXA *pixa, l_int32 outdepth, l_int32 maxwidth, l_float32 scalefactor, l_int32 background, l_int32 spacing, l_int32 border)
pixaDisplayTiledInRows()
Definition: pixafunc2.c:836
PIX * pixMorphSequence(PIX *pixs, const char *sequence, l_int32 dispsep)
pixMorphSequence()
Definition: morphseq.c:133
l_ok stringJoinIP(char **psrc1, const char *src2)
stringJoinIP()
Definition: utils2.c:564
l_ok pixCountPixels(PIX *pixs, l_int32 *pcount, l_int32 *tab8)
pixCountPixels()
Definition: pix3.c:1823
l_ok boxaAddBox(BOXA *boxa, BOX *box, l_int32 copyflag)
boxaAddBox()
Definition: boxbasic.c:618
l_float32 * array
Definition: array.h:66
l_ok ptaGetPt(PTA *pta, l_int32 index, l_float32 *px, l_float32 *py)
ptaGetPt()
Definition: ptabasic.c:525
l_int32 * makePixelSumTab8(void)
makePixelSumTab8()
Definition: pix3.c:2297
struct Pta * pta_u
Definition: recog.h:159
PIXA * pixClipRectangles(PIX *pixs, BOXA *boxa)
pixClipRectangles()
Definition: pix5.c:954
l_ok recogCorrelationBestRow(L_RECOG *recog, PIX *pixs, BOXA **pboxa, NUMA **pnascore, NUMA **pnaindex, SARRAY **psachar, l_int32 debug)
recogCorrelationBestRow()
Definition: recogident.c:401
BOX * boxAdjustSides(BOX *boxd, BOX *boxs, l_int32 delleft, l_int32 delright, l_int32 deltop, l_int32 delbot)
boxAdjustSides()
Definition: boxfunc1.c:1807
l_ok pixRemoveWithIndicator(PIX *pixs, PIXA *pixa, NUMA *na)
pixRemoveWithIndicator()
Definition: pixafunc1.c:1087
l_int32 maxheight_u
Definition: recog.h:138
NUMA * numaMakeThresholdIndicator(NUMA *nas, l_float32 thresh, l_int32 type)
numaMakeThresholdIndicator()
Definition: numafunc1.c:1153
char * sarrayGetString(SARRAY *sa, l_int32 index, l_int32 copyflag)
sarrayGetString()
Definition: sarray1.c:681
struct Ptaa * ptaa
Definition: recog.h:156
PIX * pixClone(PIX *pixs)
pixClone()
Definition: pix1.c:515
Definition: array.h:71
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:543
BOX * boxaGetBox(BOXA *boxa, l_int32 index, l_int32 accessflag)
boxaGetBox()
Definition: boxbasic.c:763
PIX * recogProcessToIdentify(L_RECOG *recog, PIX *pixs, l_int32 pad)
recogProcessToIdentify()
Definition: recogident.c:1416
static PIX * recogPreSplittingFilter(L_RECOG *recog, PIX *pixs, l_int32 minh, l_float32 minaf, l_int32 debug)
recogPreSplittingFilter()
Definition: recogident.c:1460
NUMA * pixGetMomentByColumn(PIX *pix, l_int32 order)
pixGetMomentByColumn()
Definition: pix3.c:2167
l_int32 recogAverageSamples(L_RECOG **precog, l_int32 debug)
recogAverageSamples()
Definition: recogtrain.c:486
NUMA * numaCopy(NUMA *na)
numaCopy()
Definition: numabasic.c:394
struct Numa * namoment
Definition: recog.h:219
SARRAY * sarrayClone(SARRAY *sa)
sarrayClone()
Definition: sarray1.c:420
Definition: pix.h:454
l_ok boxaGetExtent(BOXA *boxa, l_int32 *pw, l_int32 *ph, BOX **pbox)
boxaGetExtent()
Definition: boxfunc4.c:943
void numaDestroy(NUMA **pna)
numaDestroy()
Definition: numabasic.c:360
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1065
PIX * recogShowMatch(L_RECOG *recog, PIX *pix1, PIX *pix2, BOX *box, l_int32 index, l_float32 score)
recogShowMatch()
Definition: recogtrain.c:2425
l_int32 maxwidth_u
Definition: recog.h:136
struct Pixaa * pixaa
Definition: recog.h:155
BOXAA * boxaaCreate(l_int32 n)
boxaaCreate()
Definition: boxbasic.c:1223
l_int32 sarrayGetCount(SARRAY *sa)
sarrayGetCount()
Definition: sarray1.c:621
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()
Definition: recogident.c:1328
l_int32 ave_done
Definition: recog.h:141
PIX * pixExpandReplicate(PIX *pixs, l_int32 factor)
pixExpandReplicate()
Definition: scale2.c:867
FPIX * fpixCreate(l_int32 width, l_int32 height)
fpixCreate()
Definition: fpix1.c:153
l_ok pixRenderBoxaArb(PIX *pix, BOXA *boxa, l_int32 width, l_uint8 rval, l_uint8 gval, l_uint8 bval)
pixRenderBoxaArb()
Definition: graphics.c:1759
l_int32 yloc
Definition: recog.h:189
l_int32 train_done
Definition: recog.h:142
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
Definition: pixabasic.c:672
Definition: pix.h:718
l_ok pixRenderBoxArb(PIX *pix, BOX *box, l_int32 width, l_uint8 rval, l_uint8 gval, l_uint8 bval)
pixRenderBoxArb()
Definition: graphics.c:1642
l_ok recogIdentifyPixa(L_RECOG *recog, PIXA *pixa, PIX **ppixdb)
recogIdentifyPixa()
Definition: recogident.c:879
Definition: recog.h:182
NUMA * numaLogicalOp(NUMA *nad, NUMA *na1, NUMA *na2, l_int32 op)
numaLogicalOp()
Definition: numafunc1.c:246
l_float32 score
Definition: recog.h:184
Definition: pix.h:134
l_int32 min_splitw
Definition: recog.h:146
Definition: pix.h:719
void ptaDestroy(PTA **ppta)
ptaDestroy()
Definition: ptabasic.c:192
l_ok pixZero(PIX *pix, l_int32 *pempty)
pixZero()
Definition: pix3.c:1701
BOXA * boxaCreate(l_int32 n)
boxaCreate()
Definition: boxbasic.c:499
#define PIX_SRC
Definition: pix.h:327
PIX * pixCopy(PIX *pixd, PIX *pixs)
pixCopy()
Definition: pix1.c:628
PIX * recogModifyTemplate(L_RECOG *recog, PIX *pixs)
recogModifyTemplate()
Definition: recogtrain.c:417
void boxDestroy(BOX **pbox)
boxDestroy()
Definition: boxbasic.c:278
BOXAA * boxaSort2d(BOXA *boxas, NUMAA **pnaad, l_int32 delta1, l_int32 delta2, l_int32 minh1)
boxaSort2d()
Definition: boxfunc2.c:845
l_int32 sample
Definition: recog.h:186
NUMA * numaClone(NUMA *na)
numaClone()
Definition: numabasic.c:423
PIX * pixAddTextlines(PIX *pixs, L_BMF *bmf, const char *textstr, l_uint32 val, l_int32 location)
pixAddTextlines()
Definition: textops.c:270
l_int32 boxaGetCount(BOXA *boxa)
boxaGetCount()
Definition: boxbasic.c:718
l_ok recogIdentifyMultiple(L_RECOG *recog, PIX *pixs, l_int32 minh, l_int32 skipsplit, BOXA **pboxa, PIXA **ppixa, PIX **ppixdb, l_int32 debugsplit)
recogIdentifyMultiple()
Definition: recogident.c:158
l_int32 * centtab
Definition: recog.h:150
PIXA * showExtractNumbers(PIX *pixs, SARRAY *sa, BOXAA *baa, NUMAA *naa, PIX **ppixdb)
showExtractNumbers()
Definition: recogident.c:1762
l_ok pixFindAreaFraction(PIX *pixs, l_int32 *tab, l_float32 *pfract)
pixFindAreaFraction()
Definition: pix5.c:478
l_ok ptaGetIPt(PTA *pta, l_int32 index, l_int32 *px, l_int32 *py)
ptaGetIPt()
Definition: ptabasic.c:555
l_ok pixTestClipToForeground(PIX *pixs, l_int32 *pcanclip)
pixTestClipToForeground()
Definition: pix5.c:1760
BOXA * boxaaFlattenToBoxa(BOXAA *baa, NUMA **pnaindex, l_int32 copyflag)
boxaaFlattenToBoxa()
Definition: boxfunc2.c:1509
l_int32 maxyshift
Definition: recog.h:129
Definition: recog.h:197
struct Numa * nascore
Definition: recog.h:199
static l_int32 pixCorrelationBestShift(PIX *pix1, PIX *pix2, NUMA *nasum1, NUMA *namoment1, l_int32 area2, l_int32 ycent2, l_int32 maxyshift, l_int32 *tab8, l_int32 *pdelx, l_int32 *pdely, l_float32 *pscore, l_int32 debugflag)
pixCorrelationBestShift()
Definition: recogident.c:726
char * text
Definition: recog.h:185
void fpixDestroy(FPIX **pfpix)
fpixDestroy()
Definition: fpix1.c:373
l_ok pixaAddBox(PIXA *pixa, BOX *box, l_int32 copyflag)
pixaAddBox()
Definition: pixabasic.c:547
l_ok boxGetGeometry(BOX *box, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxGetGeometry()
Definition: boxbasic.c:310
Definition: pix.h:480
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
Definition: pixabasic.c:408
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
Definition: boxbasic.c:165
struct Pixa * pixa
Definition: recog.h:161
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
Definition: pixabasic.c:631
l_ok numaaAddNuma(NUMAA *naa, NUMA *na, l_int32 copyflag)
numaaAddNuma()
Definition: numabasic.c:1482
PIX * pixAddBorderGeneral(PIX *pixs, l_int32 left, l_int32 right, l_int32 top, l_int32 bot, l_uint32 val)
pixAddBorderGeneral()
Definition: pix2.c:1842
Definition: pix.h:517
#define PIX_DST
Definition: pix.h:328
Definition: pix.h:582
BOXA * boxaaGetBoxa(BOXAA *baa, l_int32 index, l_int32 accessflag)
boxaaGetBoxa()
Definition: boxbasic.c:1471
l_int32 templ_use
Definition: recog.h:123
void sarrayDestroy(SARRAY **psa)
sarrayDestroy()
Definition: sarray1.c:355
struct L_Rch * rch
Definition: recog.h:174
L_BMF * bmfCreate(const char *dir, l_int32 fontsize)
bmfCreate()
Definition: bmf.c:114