51 #include <config_auto.h> 55 #include "allheaders.h" 58 static const l_int32 MinDistInPeak = 35;
61 static const l_int32 PeakThresholdRatio = 20;
62 static const l_int32 ZeroThresholdRatio = 100;
65 static const l_int32 DefaultSlices = 10;
66 static const l_int32 DefaultSweepReduction = 2;
67 static const l_int32 DefaultBsReduction = 1;
68 static const l_float32 DefaultSweepRange = 5.;
69 static const l_float32 DefaultSweepDelta = 1.;
70 static const l_float32 DefaultMinbsDelta = 0.01;
73 static const l_float32 OverlapFraction = 0.5;
76 static const l_float32 MinAllowedConfidence = 3.0;
121 l_int32 h, i, j, nbox, val1, val2, ndiff, bx, by, bw, bh;
122 l_int32 imaxloc, peakthresh, zerothresh, inpeak;
123 l_int32 mintosearch, max, maxloc, nloc, locval;
126 BOXA *boxa1, *boxa2, *boxa3;
128 NUMA *nasum, *nadiff, *naloc, *naval;
132 PROCNAME(
"pixFindBaselines");
134 if (ppta) *ppta = NULL;
135 if (!pixs || pixGetDepth(pixs) != 1)
136 return (
NUMA *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
148 return (
NUMA *)ERROR_PTR(
"nasum not made", procName, NULL);
150 h = pixGetHeight(pixs);
153 for (i = 0; i < h - 1; i++) {
162 gplotSimple1(nadiff, GPLOT_PNG,
"/tmp/lept/baseline/diff",
"Diff Sig");
163 pix2 =
pixRead(
"/tmp/lept/baseline/diff.png");
174 peakthresh = (l_int32)maxval / PeakThresholdRatio;
176 zerothresh = (l_int32)maxval / ZeroThresholdRatio;
181 for (i = 0; i < ndiff; i++) {
182 if (inpeak == FALSE) {
183 if (array[i] > peakthresh) {
185 mintosearch = i + MinDistInPeak;
191 if (array[i] > max) {
194 mintosearch = i + MinDistInPeak;
195 }
else if (i > mintosearch && array[i] <= zerothresh) {
211 gplot =
gplotCreate(
"/tmp/lept/baseline/loc", GPLOT_PNG,
"Peak locs",
212 "rasterline",
"height");
213 gplotAddPlot(gplot, naloc, naval, GPLOT_POINTS,
"locs");
216 pix2 =
pixRead(
"/tmp/lept/baseline/loc.png");
232 L_INFO(
"no components after filtering\n", procName);
249 for (i = 0; i < nbox; i++) {
251 for (j = 0; j < nloc; j++) {
253 if (L_ABS(locval - (by + bh)) > 25)
264 l_int32 npts, x1, y1, x2, y2;
267 for (i = 0; i < npts; i += 2) {
272 pixWriteDebug(
"/tmp/lept/baseline/baselines.png", pix1, IFF_PNG);
328 l_float32 sweeprange,
329 l_float32 sweepdelta,
330 l_float32 minbsdelta)
336 PROCNAME(
"pixDeskewLocal");
338 if (!pixs || pixGetDepth(pixs) != 1)
339 return (
PIX *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
344 sweeprange, sweepdelta, minbsdelta,
347 return (
PIX *)ERROR_PTR(
"transform pts not found", procName, NULL);
397 l_float32 sweeprange,
398 l_float32 sweepdelta,
399 l_float32 minbsdelta,
404 l_float32 deg2rad, angr, angd, dely;
408 PROCNAME(
"pixGetLocalSkewTransform");
410 if (!pptas || !pptad)
411 return ERROR_INT(
"&ptas and &ptad not defined", procName, 1);
412 *pptas = *pptad = NULL;
413 if (!pixs || pixGetDepth(pixs) != 1)
414 return ERROR_INT(
"pixs not defined or not 1 bpp", procName, 1);
415 if (nslices < 2 || nslices > 20)
416 nslices = DefaultSlices;
417 if (redsweep < 1 || redsweep > 8)
418 redsweep = DefaultSweepReduction;
419 if (redsearch < 1 || redsearch > redsweep)
420 redsearch = DefaultBsReduction;
421 if (sweeprange == 0.0)
422 sweeprange = DefaultSweepRange;
423 if (sweepdelta == 0.0)
424 sweepdelta = DefaultSweepDelta;
425 if (minbsdelta == 0.0)
426 minbsdelta = DefaultMinbsDelta;
429 sweeprange, sweepdelta, minbsdelta,
432 return ERROR_INT(
"naskew not made", procName, 1);
434 deg2rad = 3.14159265 / 180.;
435 w = pixGetWidth(pixs);
436 h = pixGetHeight(pixs);
443 for (i = 0; i < h; i++) {
445 angr = angd * deg2rad;
446 dely = w * tan(angr);
447 if (i - dely > 0.05 * h)
456 for (i = h - 1; i > 0; i--) {
458 angr = angd * deg2rad;
459 dely = w * tan(angr);
460 if (i - dely < 0.95 * h)
518 l_float32 sweeprange,
519 l_float32 sweepdelta,
520 l_float32 minbsdelta,
525 l_int32 w, h, hs, i, ystart, yend, ovlap, npts;
526 l_float32 angle, conf, ycenter, a, b;
529 NUMA *naskew, *nax, *nay;
533 PROCNAME(
"pixGetLocalSkewAngles");
535 if (!pixs || pixGetDepth(pixs) != 1)
536 return (
NUMA *)ERROR_PTR(
"pixs undefined or not 1 bpp", procName, NULL);
537 if (nslices < 2 || nslices > 20)
538 nslices = DefaultSlices;
539 if (redsweep < 1 || redsweep > 8)
540 redsweep = DefaultSweepReduction;
541 if (redsearch < 1 || redsearch > redsweep)
542 redsearch = DefaultBsReduction;
543 if (sweeprange == 0.0)
544 sweeprange = DefaultSweepRange;
545 if (sweepdelta == 0.0)
546 sweepdelta = DefaultSweepDelta;
547 if (minbsdelta == 0.0)
548 minbsdelta = DefaultMinbsDelta;
552 ovlap = (l_int32)(OverlapFraction * hs);
554 for (i = 0; i < nslices; i++) {
555 ystart = L_MAX(0, hs * i - ovlap);
556 yend = L_MIN(h - 1, hs * (i + 1) + ovlap);
557 ycenter = (l_float32)(ystart + yend) / 2;
558 box =
boxCreate(0, ystart, w, yend - ystart + 1);
561 sweeprange, sweepdelta, minbsdelta);
562 if (conf > MinAllowedConfidence)
571 return (
NUMA *)ERROR_PTR(
"can't fit skew", procName, NULL);
579 for (i = 0; i < h; i++) {
587 gplot =
gplotCreate(
"/tmp/lept/baseline/skew", GPLOT_PNG,
588 "skew as fctn of y",
"y (in raster lines from top)",
589 "angle (in degrees)");
590 gplotAddPlot(gplot, NULL, naskew, GPLOT_POINTS,
"linear lsf");
591 gplotAddPlot(gplot, nax, nay, GPLOT_POINTS,
"actual data pts");
void gplotDestroy(GPLOT **pgplot)
gplotDestroy()
NUMA * pixCountPixelsByRow(PIX *pix, l_int32 *tab8)
pixCountPixelsByRow()
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
BOXA * boxaSort(BOXA *boxas, l_int32 sorttype, l_int32 sortorder, NUMA **pnaindex)
boxaSort()
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
l_ok pixGetLocalSkewTransform(PIX *pixs, l_int32 nslices, l_int32 redsweep, l_int32 redsearch, l_float32 sweeprange, l_float32 sweepdelta, l_float32 minbsdelta, PTA **pptas, PTA **pptad)
pixGetLocalSkewTransform()
l_ok ptaAddPt(PTA *pta, l_float32 x, l_float32 y)
ptaAddPt()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
l_ok gplotMakeOutput(GPLOT *gplot)
gplotMakeOutput()
GPLOT * gplotCreate(const char *rootname, l_int32 outformat, const char *title, const char *xlabel, const char *ylabel)
gplotCreate()
PTA * ptaCreate(l_int32 n)
ptaCreate()
l_ok ptaGetArrays(PTA *pta, NUMA **pnax, NUMA **pnay)
ptaGetArrays()
NUMA * numaCreate(l_int32 n)
numaCreate()
l_int32 ptaGetCount(PTA *pta)
ptaGetCount()
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
PIX * pixDeskewLocal(PIX *pixs, l_int32 nslices, l_int32 redsweep, l_int32 redsearch, l_float32 sweeprange, l_float32 sweepdelta, l_float32 minbsdelta)
pixDeskewLocal()
l_ok ptaGetLinearLSF(PTA *pta, l_float32 *pa, l_float32 *pb, NUMA **pnafit)
ptaGetLinearLSF()
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
l_ok pixRenderLineArb(PIX *pix, l_int32 x1, l_int32 y1, l_int32 x2, l_int32 y2, l_int32 width, l_uint8 rval, l_uint8 gval, l_uint8 bval)
pixRenderLineArb()
l_int32 * numaGetIArray(NUMA *na)
numaGetIArray()
BOXA * pixConnComp(PIX *pixs, PIXA **ppixa, l_int32 connectivity)
pixConnComp()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
BOXA * boxaTransform(BOXA *boxas, l_int32 shiftx, l_int32 shifty, l_float32 scalex, l_float32 scaley)
boxaTransform()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok boxaGetBoxGeometry(BOXA *boxa, l_int32 index, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxaGetBoxGeometry()
PIX * pixMorphSequence(PIX *pixs, const char *sequence, l_int32 dispsep)
pixMorphSequence()
l_ok gplotAddPlot(GPLOT *gplot, NUMA *nax, NUMA *nay, l_int32 plotstyle, const char *plotlabel)
gplotAddPlot()
NUMA * pixFindBaselines(PIX *pixs, PTA **ppta, PIXA *pixadb)
pixFindBaselines()
void pixDestroy(PIX **ppix)
pixDestroy()
void numaDestroy(NUMA **pna)
numaDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixRead(const char *filename)
pixRead()
NUMA * pixGetLocalSkewAngles(PIX *pixs, l_int32 nslices, l_int32 redsweep, l_int32 redsearch, l_float32 sweeprange, l_float32 sweepdelta, l_float32 minbsdelta, l_float32 *pa, l_float32 *pb, l_int32 debug)
pixGetLocalSkewAngles()
void ptaDestroy(PTA **ppta)
ptaDestroy()
l_ok pixFindSkewSweepAndSearch(PIX *pixs, l_float32 *pangle, l_float32 *pconf, l_int32 redsweep, l_int32 redsearch, l_float32 sweeprange, l_float32 sweepdelta, l_float32 minbsdelta)
pixFindSkewSweepAndSearch()
void boxDestroy(BOX **pbox)
boxDestroy()
l_int32 boxaGetCount(BOXA *boxa)
boxaGetCount()
l_ok ptaGetIPt(PTA *pta, l_int32 index, l_int32 *px, l_int32 *py)
ptaGetIPt()
l_ok numaGetMax(NUMA *na, l_float32 *pmaxval, l_int32 *pimaxloc)
numaGetMax()
PIX * pixProjectiveSampledPta(PIX *pixs, PTA *ptad, PTA *ptas, l_int32 incolor)
pixProjectiveSampledPta()
PIX * pixScale(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScale()
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
l_ok gplotSimple1(NUMA *na, l_int32 outformat, const char *outroot, const char *title)
gplotSimple1()