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 if (ppta) *ppta = NULL;
133 if (!pixs || pixGetDepth(pixs) != 1)
134 return (
NUMA *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
146 return (
NUMA *)ERROR_PTR(
"nasum not made", __func__, NULL);
148 h = pixGetHeight(pixs);
151 for (i = 0; i < h - 1; i++) {
160 gplotSimple1(nadiff, GPLOT_PNG,
"/tmp/lept/baseline/diff",
"Diff Sig");
161 pix2 =
pixRead(
"/tmp/lept/baseline/diff.png");
172 peakthresh = (l_int32)maxval / PeakThresholdRatio;
174 zerothresh = (l_int32)maxval / ZeroThresholdRatio;
179 for (i = 0; i < ndiff; i++) {
180 if (inpeak == FALSE) {
181 if (array[i] > peakthresh) {
183 mintosearch = i + MinDistInPeak;
189 if (array[i] > max) {
192 mintosearch = i + MinDistInPeak;
193 }
else if (i > mintosearch && array[i] <= zerothresh) {
209 gplot =
gplotCreate(
"/tmp/lept/baseline/loc", GPLOT_PNG,
"Peak locs",
210 "rasterline",
"height");
211 gplotAddPlot(gplot, naloc, naval, GPLOT_POINTS,
"locs");
214 pix2 =
pixRead(
"/tmp/lept/baseline/loc.png");
230 L_INFO(
"no components after filtering\n", __func__);
247 for (i = 0; i < nbox; i++) {
249 for (j = 0; j < nloc; j++) {
251 if (L_ABS(locval - (by + bh)) > 25)
262 l_int32 npts, x1, y1, x2, y2;
265 for (i = 0; i < npts; i += 2) {
270 pixWriteDebug(
"/tmp/lept/baseline/baselines.png", pix1, IFF_PNG);
326 l_float32 sweeprange,
327 l_float32 sweepdelta,
328 l_float32 minbsdelta)
334 if (!pixs || pixGetDepth(pixs) != 1)
335 return (
PIX *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
340 sweeprange, sweepdelta, minbsdelta,
343 return (
PIX *)ERROR_PTR(
"transform pts not found", __func__, NULL);
393 l_float32 sweeprange,
394 l_float32 sweepdelta,
395 l_float32 minbsdelta,
400 l_float32 deg2rad, angr, angd, dely;
404 if (!pptas || !pptad)
405 return ERROR_INT(
"&ptas and &ptad not defined", __func__, 1);
406 *pptas = *pptad = NULL;
407 if (!pixs || pixGetDepth(pixs) != 1)
408 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
409 if (nslices < 2 || nslices > 20)
410 nslices = DefaultSlices;
411 if (redsweep < 1 || redsweep > 8)
412 redsweep = DefaultSweepReduction;
413 if (redsearch < 1 || redsearch > redsweep)
414 redsearch = DefaultBsReduction;
415 if (sweeprange == 0.0)
416 sweeprange = DefaultSweepRange;
417 if (sweepdelta == 0.0)
418 sweepdelta = DefaultSweepDelta;
419 if (minbsdelta == 0.0)
420 minbsdelta = DefaultMinbsDelta;
423 sweeprange, sweepdelta, minbsdelta,
426 return ERROR_INT(
"naskew not made", __func__, 1);
428 deg2rad = 3.14159265 / 180.;
429 w = pixGetWidth(pixs);
430 h = pixGetHeight(pixs);
437 for (i = 0; i < h; i++) {
439 angr = angd * deg2rad;
440 dely = w * tan(angr);
441 if (i - dely > 0.05 * h)
450 for (i = h - 1; i > 0; i--) {
452 angr = angd * deg2rad;
453 dely = w * tan(angr);
454 if (i - dely < 0.95 * h)
512 l_float32 sweeprange,
513 l_float32 sweepdelta,
514 l_float32 minbsdelta,
519 l_int32 w, h, hs, i, ystart, yend, ovlap, npts;
520 l_float32 angle, conf, ycenter, a, b;
523 NUMA *naskew, *nax, *nay;
527 if (!pixs || pixGetDepth(pixs) != 1)
528 return (
NUMA *)ERROR_PTR(
"pixs undefined or not 1 bpp", __func__, NULL);
529 if (nslices < 2 || nslices > 20)
530 nslices = DefaultSlices;
531 if (redsweep < 1 || redsweep > 8)
532 redsweep = DefaultSweepReduction;
533 if (redsearch < 1 || redsearch > redsweep)
534 redsearch = DefaultBsReduction;
535 if (sweeprange == 0.0)
536 sweeprange = DefaultSweepRange;
537 if (sweepdelta == 0.0)
538 sweepdelta = DefaultSweepDelta;
539 if (minbsdelta == 0.0)
540 minbsdelta = DefaultMinbsDelta;
544 ovlap = (l_int32)(OverlapFraction * hs);
546 for (i = 0; i < nslices; i++) {
547 ystart = L_MAX(0, hs * i - ovlap);
548 yend = L_MIN(h - 1, hs * (i + 1) + ovlap);
549 ycenter = (l_float32)(ystart + yend) / 2;
550 box =
boxCreate(0, ystart, w, yend - ystart + 1);
553 sweeprange, sweepdelta, minbsdelta);
554 if (conf > MinAllowedConfidence)
563 return (
NUMA *)ERROR_PTR(
"can't fit skew", __func__, NULL);
571 for (i = 0; i < h; i++) {
579 gplot =
gplotCreate(
"/tmp/lept/baseline/skew", GPLOT_PNG,
580 "skew as fctn of y",
"y (in raster lines from top)",
581 "angle (in degrees)");
582 gplotAddPlot(gplot, NULL, naskew, GPLOT_POINTS,
"linear lsf");
583 gplotAddPlot(gplot, nax, nay, GPLOT_POINTS,
"actual data pts");
NUMA * pixFindBaselines(PIX *pixs, PTA **ppta, PIXA *pixadb)
pixFindBaselines()
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()
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()
PIX * pixDeskewLocal(PIX *pixs, l_int32 nslices, l_int32 redsweep, l_int32 redsearch, l_float32 sweeprange, l_float32 sweepdelta, l_float32 minbsdelta)
pixDeskewLocal()
void boxDestroy(BOX **pbox)
boxDestroy()
l_ok boxaGetBoxGeometry(BOXA *boxa, l_int32 index, l_int32 *px, l_int32 *py, l_int32 *pw, l_int32 *ph)
boxaGetBoxGeometry()
void boxaDestroy(BOXA **pboxa)
boxaDestroy()
l_int32 boxaGetCount(const BOXA *boxa)
boxaGetCount()
BOX * boxCreate(l_int32 x, l_int32 y, l_int32 w, l_int32 h)
boxCreate()
BOXA * boxaTransform(BOXA *boxas, l_int32 shiftx, l_int32 shifty, l_float32 scalex, l_float32 scaley)
boxaTransform()
BOXA * boxaSort(BOXA *boxas, l_int32 sorttype, l_int32 sortorder, NUMA **pnaindex)
boxaSort()
BOXA * pixConnComp(PIX *pixs, PIXA **ppixa, l_int32 connectivity)
pixConnComp()
l_ok gplotAddPlot(GPLOT *gplot, NUMA *nax, NUMA *nay, l_int32 plotstyle, const char *plotlabel)
gplotAddPlot()
l_ok gplotMakeOutput(GPLOT *gplot)
gplotMakeOutput()
GPLOT * gplotCreate(const char *rootname, l_int32 outformat, const char *title, const char *xlabel, const char *ylabel)
gplotCreate()
void gplotDestroy(GPLOT **pgplot)
gplotDestroy()
l_ok gplotSimple1(NUMA *na, l_int32 outformat, const char *outroot, const char *title)
gplotSimple1()
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()
PIX * pixMorphSequence(PIX *pixs, const char *sequence, l_int32 dispsep)
pixMorphSequence()
l_ok numaAddNumber(NUMA *na, l_float32 val)
numaAddNumber()
l_ok numaGetFValue(NUMA *na, l_int32 index, l_float32 *pval)
numaGetFValue()
NUMA * numaCreate(l_int32 n)
numaCreate()
void numaDestroy(NUMA **pna)
numaDestroy()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok numaGetIValue(NUMA *na, l_int32 index, l_int32 *pival)
numaGetIValue()
l_int32 * numaGetIArray(NUMA *na)
numaGetIArray()
l_ok numaGetMax(NUMA *na, l_float32 *pmaxval, l_int32 *pimaxloc)
numaGetMax()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
NUMA * pixCountPixelsByRow(PIX *pix, l_int32 *tab8)
pixCountPixelsByRow()
PIX * pixClipRectangle(PIX *pixs, BOX *box, BOX **pboxc)
pixClipRectangle()
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
PIX * pixProjectiveSampledPta(PIX *pixs, PTA *ptad, PTA *ptas, l_int32 incolor)
pixProjectiveSampledPta()
l_ok ptaGetIPt(PTA *pta, l_int32 index, l_int32 *px, l_int32 *py)
ptaGetIPt()
l_ok ptaAddPt(PTA *pta, l_float32 x, l_float32 y)
ptaAddPt()
l_ok ptaGetArrays(PTA *pta, NUMA **pnax, NUMA **pnay)
ptaGetArrays()
l_int32 ptaGetCount(PTA *pta)
ptaGetCount()
PTA * ptaCreate(l_int32 n)
ptaCreate()
void ptaDestroy(PTA **ppta)
ptaDestroy()
l_ok ptaGetLinearLSF(PTA *pta, l_float32 *pa, l_float32 *pb, NUMA **pnafit)
ptaGetLinearLSF()
PIX * pixRead(const char *filename)
pixRead()
PIX * pixScale(PIX *pixs, l_float32 scalex, l_float32 scaley)
pixScale()
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()
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()