99 #include <config_auto.h>
103 #include "allheaders.h"
106 static const l_float32 DefaultSweepRange = 7.0;
107 static const l_float32 DefaultSweepDelta = 1.0;
113 static const l_float32 DefaultMinbsDelta = 0.01;
116 static const l_int32 DefaultSweepReduction = 4;
117 static const l_int32 DefaultBsReduction = 2;
120 static const l_float32 MinDeskewAngle = 0.1;
123 static const l_float32 MinAllowedConfidence = 3.0;
126 static const l_int32 MinValidMaxscore = 10000;
131 static const l_float32 MinscoreThreshFactor = 0.000002;
134 static const l_int32 DefaultBinaryThreshold = 130;
136 #ifndef NO_CONSOLE_IO
137 #define DEBUG_PRINT_SCORES 0
138 #define DEBUG_PRINT_SWEEP 0
139 #define DEBUG_PRINT_BINARY 0
140 #define DEBUG_PRINT_ORTH 0
141 #define DEBUG_THRESHOLD 0
142 #define DEBUG_PLOT_SCORES 0
169 PIX *pix1, *pix2, *pix3, *pix4;
172 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
174 redsearch = DefaultBsReduction;
175 else if (redsearch != 1 && redsearch != 2 && redsearch != 4)
176 return (
PIX *)ERROR_PTR(
"redsearch not in {1,2,4}", __func__, NULL);
211 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
213 redsearch = DefaultBsReduction;
214 else if (redsearch != 1 && redsearch != 2 && redsearch != 4)
215 return (
PIX *)ERROR_PTR(
"redsearch not in {1,2,4}", __func__, NULL);
247 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
249 redsearch = DefaultBsReduction;
250 else if (redsearch != 1 && redsearch != 2 && redsearch != 4)
251 return (
PIX *)ERROR_PTR(
"redsearch not in {1,2,4}", __func__, NULL);
285 l_float32 sweeprange,
286 l_float32 sweepdelta,
293 l_float32 angle, conf, deg2rad;
296 if (pangle) *pangle = 0.0;
297 if (pconf) *pconf = 0.0;
299 return (
PIX *)ERROR_PTR(
"pixs not defined", __func__, NULL);
301 redsweep = DefaultSweepReduction;
302 else if (redsweep != 1 && redsweep != 2 && redsweep != 4)
303 return (
PIX *)ERROR_PTR(
"redsweep not in {1,2,4}", __func__, NULL);
304 if (sweeprange == 0.0)
305 sweeprange = DefaultSweepRange;
306 if (sweepdelta == 0.0)
307 sweepdelta = DefaultSweepDelta;
309 redsearch = DefaultBsReduction;
310 else if (redsearch != 1 && redsearch != 2 && redsearch != 4)
311 return (
PIX *)ERROR_PTR(
"redsearch not in {1,2,4}", __func__, NULL);
313 thresh = DefaultBinaryThreshold;
315 deg2rad = 3.1415926535 / 180.;
318 depth = pixGetDepth(pixs);
326 sweeprange, sweepdelta,
329 if (pangle) *pangle = angle;
330 if (pconf) *pconf = conf;
334 if (L_ABS(angle) < MinDeskewAngle || conf < MinAllowedConfidence)
370 if (pangle) *pangle = 0.0;
371 if (pconf) *pconf = 0.0;
372 if (!pangle || !pconf)
373 return ERROR_INT(
"&angle and/or &conf not defined", __func__, 1);
375 return ERROR_INT(
"pixs not defined", __func__, 1);
376 if (pixGetDepth(pixs) != 1)
377 return ERROR_INT(
"pixs not 1 bpp", __func__, 1);
380 DefaultSweepReduction,
411 l_float32 sweeprange,
412 l_float32 sweepdelta)
414 l_int32 ret, bzero, i, nangles;
415 l_float32 deg2rad, theta;
416 l_float32 sum, maxscore, maxangle;
417 NUMA *natheta, *nascore;
421 return ERROR_INT(
"&angle not defined", __func__, 1);
424 return ERROR_INT(
"pixs not defined", __func__, 1);
425 if (pixGetDepth(pixs) != 1)
426 return ERROR_INT(
"pixs not 1 bpp", __func__, 1);
427 if (reduction != 1 && reduction != 2 && reduction != 4 && reduction != 8)
428 return ERROR_INT(
"reduction must be in {1,2,4,8}", __func__, 1);
430 deg2rad = 3.1415926535 / 180.;
436 else if (reduction == 2)
438 else if (reduction == 4)
449 nangles = (l_int32)((2. * sweeprange) / sweepdelta + 1);
455 ret = ERROR_INT(
"pix and pixt not both made", __func__, 1);
458 if (!natheta || !nascore) {
459 ret = ERROR_INT(
"natheta and nascore not both made", __func__, 1);
463 for (i = 0; i < nangles; i++) {
464 theta = -sweeprange + i * sweepdelta;
472 #if DEBUG_PRINT_SCORES
473 L_INFO(
"sum(%7.2f) = %7.0f\n", __func__, theta, sum);
484 numaFitMax(nascore, &maxscore, natheta, &maxangle);
487 #if DEBUG_PRINT_SWEEP
488 L_INFO(
" From sweep: angle = %7.3f, score = %7.3f\n", __func__,
492 #if DEBUG_PLOT_SCORES
503 "Sweep. Variance of difference of ON pixels vs. angle",
504 "angle (deg)",
"score");
505 gplotAddPlot(gplot, natheta, nascore, GPLOT_LINES,
"plot1");
506 gplotAddPlot(gplot, natheta, nascore, GPLOT_POINTS,
"plot2");
555 l_float32 sweeprange,
556 l_float32 sweepdelta,
557 l_float32 minbsdelta)
560 redsweep, redsearch, 0.0, sweeprange,
561 sweepdelta, minbsdelta);
607 l_float32 *pendscore,
610 l_float32 sweepcenter,
611 l_float32 sweeprange,
612 l_float32 sweepdelta,
613 l_float32 minbsdelta)
616 redsweep, redsearch, 0.0,
617 sweeprange, sweepdelta,
656 l_float32 *pendscore,
659 l_float32 sweepcenter,
660 l_float32 sweeprange,
661 l_float32 sweepdelta,
662 l_float32 minbsdelta,
665 l_int32 ret, bzero, i, nangles, n, ratio, maxindex, minloc;
666 l_int32 width, height;
667 l_float32 deg2rad, theta, delta;
668 l_float32 sum, maxscore, maxangle;
669 l_float32 centerangle, leftcenterangle, rightcenterangle;
670 l_float32 lefttemp, righttemp;
671 l_float32 bsearchscore[5];
672 l_float32 minscore, minthresh;
674 NUMA *natheta, *nascore;
675 PIX *pixsw, *pixsch, *pixt1, *pixt2;
677 if (pendscore) *pendscore = 0.0;
678 if (pangle) *pangle = 0.0;
679 if (pconf) *pconf = 0.0;
680 if (!pangle || !pconf)
681 return ERROR_INT(
"&angle and/or &conf not defined", __func__, 1);
682 if (!pixs || pixGetDepth(pixs) != 1)
683 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
684 if (redsweep != 1 && redsweep != 2 && redsweep != 4 && redsweep != 8)
685 return ERROR_INT(
"redsweep must be in {1,2,4,8}", __func__, 1);
686 if (redsearch != 1 && redsearch != 2 && redsearch != 4 && redsearch != 8)
687 return ERROR_INT(
"redsearch must be in {1,2,4,8}", __func__, 1);
688 if (redsearch > redsweep)
689 return ERROR_INT(
"redsearch must not exceed redsweep", __func__, 1);
691 return ERROR_INT(
"invalid pivot", __func__, 1);
693 deg2rad = 3.1415926535 / 180.;
699 else if (redsearch == 2)
701 else if (redsearch == 4)
713 ratio = redsweep / redsearch;
731 nangles = (l_int32)((2. * sweeprange) / sweepdelta + 1);
735 if (!pixsch || !pixsw) {
736 ret = ERROR_INT(
"pixsch and pixsw not both made", __func__, 1);
739 if (!pixt1 || !pixt2) {
740 ret = ERROR_INT(
"pixt1 and pixt2 not both made", __func__, 1);
743 if (!natheta || !nascore) {
744 ret = ERROR_INT(
"natheta and nascore not both made", __func__, 1);
749 rangeleft = sweepcenter - sweeprange;
750 for (i = 0; i < nangles; i++) {
751 theta = rangeleft + i * sweepdelta;
762 #if DEBUG_PRINT_SCORES
763 L_INFO(
"sum(%7.2f) = %7.0f\n", __func__, theta, sum);
775 #if DEBUG_PRINT_SWEEP
776 L_INFO(
" From sweep: angle = %7.3f, score = %7.3f\n", __func__,
780 #if DEBUG_PLOT_SCORES
785 "Sweep. Variance of difference of ON pixels vs. angle",
786 "angle (deg)",
"score");
787 gplotAddPlot(gplot, natheta, nascore, GPLOT_LINES,
"plot1");
788 gplotAddPlot(gplot, natheta, nascore, GPLOT_POINTS,
"plot2");
796 if (maxindex == 0 || maxindex == n - 1) {
797 L_WARNING(
"max found at sweep edge\n", __func__);
807 centerangle = maxangle;
836 delta = 0.5 * sweepdelta;
837 while (delta >= minbsdelta)
840 leftcenterangle = centerangle - delta;
852 rightcenterangle = centerangle + delta;
866 maxscore = bsearchscore[1];
868 for (i = 2; i < 4; i++) {
869 if (bsearchscore[i] > maxscore) {
870 maxscore = bsearchscore[i];
876 lefttemp = bsearchscore[maxindex - 1];
877 righttemp = bsearchscore[maxindex + 1];
878 bsearchscore[2] = maxscore;
879 bsearchscore[0] = lefttemp;
880 bsearchscore[4] = righttemp;
883 centerangle = centerangle + delta * (maxindex - 2);
886 *pangle = centerangle;
888 #if DEBUG_PRINT_SCORES
889 L_INFO(
" Binary search score = %7.3f\n", __func__, bsearchscore[2]);
893 *pendscore = bsearchscore[2];
907 width = pixGetWidth(pixsch);
908 height = pixGetHeight(pixsch);
909 minthresh = MinscoreThreshFactor * width * width * height;
912 L_INFO(
" minthresh = %10.2f, minscore = %10.2f\n", __func__,
913 minthresh, minscore);
914 L_INFO(
" maxscore = %10.2f\n", __func__, maxscore);
917 if (minscore > minthresh)
918 *pconf = maxscore / minscore;
924 if ((centerangle > rangeleft + 2 * sweeprange - sweepdelta) ||
925 (centerangle < rangeleft + sweepdelta) ||
926 (maxscore < MinValidMaxscore))
929 #if DEBUG_PRINT_BINARY
930 lept_stderr(
"Binary search: angle = %7.3f, score ratio = %6.2f\n",
935 #if DEBUG_PLOT_SCORES
942 "Binary search. Variance of difference of ON pixels vs. angle",
943 "angle (deg)",
"score");
944 gplotAddPlot(gplot, natheta, nascore, GPLOT_POINTS,
"plot1");
1024 pixFindSkewOrthogonalRange(
PIX *pixs,
1029 l_float32 sweeprange,
1030 l_float32 sweepdelta,
1031 l_float32 minbsdelta,
1032 l_float32 confprior)
1034 l_float32 angle1, conf1, score1, angle2, conf2, score2;
1037 if (pangle) *pangle = 0.0;
1038 if (pconf) *pconf = 0.0;
1039 if (!pangle || !pconf)
1040 return ERROR_INT(
"&angle and/or &conf not defined", __func__, 1);
1041 if (!pixs || pixGetDepth(pixs) != 1)
1042 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
1045 redsweep, redsearch, 0.0,
1046 sweeprange, sweepdelta, minbsdelta,
1050 redsweep, redsearch, 0.0,
1051 sweeprange, sweepdelta, minbsdelta,
1055 if (conf1 > conf2 - confprior) {
1059 *pangle = -90.0 + angle2;
1063 #if DEBUG_PRINT_ORTH
1064 lept_stderr(
" About 0: angle1 = %7.3f, conf1 = %7.3f, score1 = %f\n",
1065 angle1, conf1, score1);
1066 lept_stderr(
" About 90: angle2 = %7.3f, conf2 = %7.3f, score2 = %f\n",
1067 angle2, conf2, score2);
1068 lept_stderr(
" Final: angle = %7.3f, conf = %7.3f\n", *pangle, *pconf);
1099 l_int32 w, h, skiph, skip, nskip;
1100 l_float32 val1, val2, diff, sum;
1104 return ERROR_INT(
"&sum not defined", __func__, 1);
1107 return ERROR_INT(
"pixs not defined", __func__, 1);
1112 return ERROR_INT(
"na not made", __func__, 1);
1117 w = pixGetWidth(pixs);
1118 h = pixGetHeight(pixs);
1119 skiph = (l_int32)(0.05 * w);
1120 skip = L_MIN(h / 10, skiph);
1121 nskip = L_MAX(skip / 2, 1);
1127 for (i = nskip; i < n - nskip; i++) {
1171 l_int32 i, w, h, empty;
1172 l_float32 sum, sumsq, uniform, val;
1176 if (phratio) *phratio = 0.0;
1177 if (pvratio) *pvratio = 0.0;
1178 if (pfract) *pfract = 0.0;
1179 if (!phratio && !pvratio)
1180 return ERROR_INT(
"nothing to do", __func__, 1);
1181 if (!pixs || pixGetDepth(pixs) != 1)
1182 return ERROR_INT(
"pixs not defined or not 1 bpp", __func__, 1);
1189 if (pfract) *pfract = sum / (l_float32)(w * h);
1191 uniform = sum * sum / h;
1193 for (i = 0; i < h; i++) {
1197 *phratio = sumsq / uniform;
1205 if (empty == 1)
return 1;
1209 if (pfract) *pfract = sum / (l_float32)(w * h);
1211 uniform = sum * sum / w;
1213 for (i = 0; i < w; i++) {
1217 *pvratio = sumsq / uniform;
PIX * pixReduceRankBinaryCascade(PIX *pixs, l_int32 level1, l_int32 level2, l_int32 level3, l_int32 level4)
pixReduceRankBinaryCascade()
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 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_ok numaEmpty(NUMA *na)
numaEmpty()
l_int32 numaGetCount(NUMA *na)
numaGetCount()
l_ok numaGetMin(NUMA *na, l_float32 *pminval, l_int32 *piminloc)
numaGetMin()
l_ok numaGetMax(NUMA *na, l_float32 *pmaxval, l_int32 *pimaxloc)
numaGetMax()
l_ok numaFitMax(NUMA *na, l_float32 *pmaxval, NUMA *naloc, l_float32 *pmaxloc)
numaFitMax()
l_ok numaGetSum(NUMA *na, l_float32 *psum)
numaGetSum()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCreateTemplate(const PIX *pixs)
pixCreateTemplate()
PIX * pixClone(PIX *pixs)
pixClone()
l_ok pixZero(PIX *pix, l_int32 *pempty)
pixZero()
NUMA * pixCountPixelsByRow(PIX *pix, l_int32 *tab8)
pixCountPixelsByRow()
PIX * pixConvertTo1(PIX *pixs, l_int32 threshold)
pixConvertTo1()
PIX * pixRotate(PIX *pixs, l_float32 angle, l_int32 type, l_int32 incolor, l_int32 width, l_int32 height)
pixRotate()
PIX * pixRotate90(PIX *pixs, l_int32 direction)
pixRotate90()
PIX * pixRotateOrth(PIX *pixs, l_int32 quads)
pixRotateOrth()
PIX * pixVShearCenter(PIX *pixd, PIX *pixs, l_float32 radang, l_int32 incolor)
pixVShearCenter()
PIX * pixVShearCorner(PIX *pixd, PIX *pixs, l_float32 radang, l_int32 incolor)
pixVShearCorner()
PIX * pixDeskewBoth(PIX *pixs, l_int32 redsearch)
pixDeskewBoth()
l_ok pixFindSkewSweepAndSearchScore(PIX *pixs, l_float32 *pangle, l_float32 *pconf, l_float32 *pendscore, l_int32 redsweep, l_int32 redsearch, l_float32 sweepcenter, l_float32 sweeprange, l_float32 sweepdelta, l_float32 minbsdelta)
pixFindSkewSweepAndSearchScore()
l_ok pixFindSkewSweep(PIX *pixs, l_float32 *pangle, l_int32 reduction, l_float32 sweeprange, l_float32 sweepdelta)
pixFindSkewSweep()
l_ok pixFindNormalizedSquareSum(PIX *pixs, l_float32 *phratio, l_float32 *pvratio, l_float32 *pfract)
pixFindNormalizedSquareSum()
l_ok pixFindSkewSweepAndSearchScorePivot(PIX *pixs, l_float32 *pangle, l_float32 *pconf, l_float32 *pendscore, l_int32 redsweep, l_int32 redsearch, l_float32 sweepcenter, l_float32 sweeprange, l_float32 sweepdelta, l_float32 minbsdelta, l_int32 pivot)
pixFindSkewSweepAndSearchScorePivot()
PIX * pixDeskew(PIX *pixs, l_int32 redsearch)
pixDeskew()
PIX * pixFindSkewAndDeskew(PIX *pixs, l_int32 redsearch, l_float32 *pangle, l_float32 *pconf)
pixFindSkewAndDeskew()
l_ok pixFindSkew(PIX *pixs, l_float32 *pangle, l_float32 *pconf)
pixFindSkew()
l_ok pixFindDifferentialSquareSum(PIX *pixs, l_float32 *psum)
pixFindDifferentialSquareSum()
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()
PIX * pixDeskewGeneral(PIX *pixs, l_int32 redsweep, l_float32 sweeprange, l_float32 sweepdelta, l_int32 redsearch, l_int32 thresh, l_float32 *pangle, l_float32 *pconf)
pixDeskewGeneral()
void lept_stderr(const char *fmt,...)
lept_stderr()