411 l_float32 sweeprange,
412 l_float32 sweepdelta)
414l_int32 ret, bzero, i, nangles;
415l_float32 deg2rad, theta;
416l_float32 sum, maxscore, maxangle;
417NUMA *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.1415926535f / 180.f;
435 pix = pixClone(pixs);
436 else if (reduction == 2)
437 pix = pixReduceRankBinaryCascade(pixs, 1, 0, 0, 0);
438 else if (reduction == 4)
439 pix = pixReduceRankBinaryCascade(pixs, 1, 1, 0, 0);
441 pix = pixReduceRankBinaryCascade(pixs, 1, 1, 2, 0);
443 pixZero(pix, &bzero);
449 nangles = (l_int32)((2. * sweeprange) / sweepdelta + 1);
450 natheta = numaCreate(nangles);
451 nascore = numaCreate(nangles);
452 pixt = pixCreateTemplate(pix);
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);
477 numaAddNumber(nascore, sum);
478 numaAddNumber(natheta, theta);
484 numaFitMax(nascore, &maxscore, natheta, &maxangle);
488 L_INFO(
" From sweep: angle = %7.3f, score = %7.3f\n", __func__,
502 gplot = gplotCreate(
"sweep_output", GPLOT_PNG,
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");
507 gplotMakeOutput(gplot);
508 gplotDestroy(&gplot);
515 numaDestroy(&nascore);
516 numaDestroy(&natheta);
656 l_float32 *pendscore,
659 l_float32 sweepcenter,
660 l_float32 sweeprange,
661 l_float32 sweepdelta,
662 l_float32 minbsdelta,
665l_int32 ret, bzero, i, nangles, n, ratio, maxindex, minloc;
666l_int32 width, height;
667l_float32 deg2rad, theta, delta;
668l_float32 sum, maxscore, maxangle;
669l_float32 centerangle, leftcenterangle, rightcenterangle;
670l_float32 lefttemp, righttemp;
671l_float32 bsearchscore[5];
672l_float32 minscore, minthresh;
674NUMA *natheta, *nascore;
675PIX *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.1415926535f / 180.f;
698 pixsch = pixClone(pixs);
699 else if (redsearch == 2)
700 pixsch = pixReduceRankBinaryCascade(pixs, 1, 0, 0, 0);
701 else if (redsearch == 4)
702 pixsch = pixReduceRankBinaryCascade(pixs, 1, 1, 0, 0);
704 pixsch = pixReduceRankBinaryCascade(pixs, 1, 1, 2, 0);
706 pixZero(pixsch, &bzero);
713 ratio = redsweep / redsearch;
715 pixsw = pixClone(pixsch);
718 pixsw = pixReduceRankBinaryCascade(pixsch, 1, 0, 0, 0);
720 pixsw = pixReduceRankBinaryCascade(pixsch, 1, 2, 0, 0);
722 pixsw = pixReduceRankBinaryCascade(pixsch, 1, 2, 2, 0);
725 pixt1 = pixCreateTemplate(pixsw);
727 pixt2 = pixClone(pixt1);
729 pixt2 = pixCreateTemplate(pixsch);
731 nangles = (l_int32)((2. * sweeprange) / sweepdelta + 1);
732 natheta = numaCreate(nangles);
733 nascore = numaCreate(nangles);
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);
767 numaAddNumber(nascore, sum);
768 numaAddNumber(natheta, theta);
772 numaGetMax(nascore, &maxscore, &maxindex);
773 numaGetFValue(natheta, maxindex, &maxangle);
776 L_INFO(
" From sweep: angle = %7.3f, score = %7.3f\n", __func__,
784 gplot = gplotCreate(
"sweep_output", GPLOT_PNG,
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");
789 gplotMakeOutput(gplot);
790 gplotDestroy(&gplot);
795 n = numaGetCount(natheta);
796 if (maxindex == 0 || maxindex == n - 1) {
797 L_WARNING(
"max found at sweep edge\n", __func__);
807 centerangle = maxangle;
811 pixVShearCorner(pixt2, pixsch, deg2rad * (centerangle - sweepdelta),
814 pixVShearCorner(pixt2, pixsch, deg2rad * (centerangle + sweepdelta),
820 pixVShearCenter(pixt2, pixsch, deg2rad * (centerangle - sweepdelta),
823 pixVShearCenter(pixt2, pixsch, deg2rad * (centerangle + sweepdelta),
828 numaAddNumber(nascore, bsearchscore[2]);
829 numaAddNumber(natheta, centerangle);
830 numaAddNumber(nascore, bsearchscore[0]);
831 numaAddNumber(natheta, centerangle - sweepdelta);
832 numaAddNumber(nascore, bsearchscore[4]);
833 numaAddNumber(natheta, centerangle + sweepdelta);
836 delta = 0.5f * sweepdelta;
837 while (delta >= minbsdelta)
840 leftcenterangle = centerangle - delta;
842 pixVShearCorner(pixt2, pixsch, deg2rad * leftcenterangle,
845 pixVShearCenter(pixt2, pixsch, deg2rad * leftcenterangle,
848 numaAddNumber(nascore, bsearchscore[1]);
849 numaAddNumber(natheta, leftcenterangle);
852 rightcenterangle = centerangle + delta;
854 pixVShearCorner(pixt2, pixsch, deg2rad * rightcenterangle,
857 pixVShearCenter(pixt2, pixsch, deg2rad * rightcenterangle,
860 numaAddNumber(nascore, bsearchscore[3]);
861 numaAddNumber(natheta, rightcenterangle);
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);
884 delta = 0.5f * delta;
886 *pangle = centerangle;
888#if DEBUG_PRINT_SCORES
889 L_INFO(
" Binary search score = %7.3f\n", __func__, bsearchscore[2]);
893 *pendscore = bsearchscore[2];
906 numaGetMin(nascore, &minscore, &minloc);
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",
932 lept_stderr(
" max score = %8.0f\n", maxscore);
941 gplot = gplotCreate(
"search_output", GPLOT_PNG,
942 "Binary search. Variance of difference of ON pixels vs. angle",
943 "angle (deg)",
"score");
944 gplotAddPlot(gplot, natheta, nascore, GPLOT_POINTS,
"plot1");
945 gplotMakeOutput(gplot);
946 gplotDestroy(&gplot);
955 numaDestroy(&nascore);
956 numaDestroy(&natheta);