Leptonica 1.68
C Image Processing Library

skew_reg.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 /*
00017  * skew_reg.c
00018  *
00019  *     Regression test for skew detection.
00020  */
00021 
00022 #include "allheaders.h"
00023 
00024     /* deskew */
00025 #define   DESKEW_REDUCTION      4      /* 1, 2 or 4 */
00026 
00027     /* sweep only */
00028 #define   SWEEP_RANGE           5.     /* degrees */
00029 #define   SWEEP_DELTA           0.2    /* degrees */
00030 #define   SWEEP_REDUCTION       2      /* 1, 2, 4 or 8 */
00031 
00032     /* sweep and search */
00033 #define   SWEEP_RANGE2          5.     /* degrees */
00034 #define   SWEEP_DELTA2          1.     /* degrees */
00035 #define   SWEEP_REDUCTION2      2      /* 1, 2, 4 or 8 */
00036 #define   SEARCH_REDUCTION      2      /* 1, 2, 4 or 8 */
00037 #define   SEARCH_MIN_DELTA      0.01   /* degrees */
00038 
00039 static const l_int32  BORDER = 150;
00040 
00041 
00042 main(int    argc,
00043      char **argv)
00044 {
00045 l_int32       w, h, wd, hd;
00046 l_float32     deg2rad, angle, conf, score;
00047 PIX          *pixs, *pixb1, *pixb2, *pixr, *pixf, *pixd, *pixc;
00048 PIXA         *pixa;
00049 L_REGPARAMS  *rp;
00050 
00051     if (regTestSetup(argc, argv, &rp))
00052         return 1;
00053 
00054     deg2rad = 3.1415926535 / 180.;
00055 
00056     pixa = pixaCreate(0);
00057     pixs = pixRead("feyn.tif");
00058     pixSetOrClearBorder(pixs, 100, 250, 100, 0, PIX_CLR);
00059     pixb1 = pixReduceRankBinaryCascade(pixs, 2, 2, 0, 0);
00060     regTestWritePixAndCheck(rp, pixb1, IFF_PNG);  /* 0 */
00061     pixDisplayWithTitle(pixb1, 0, 100, NULL, rp->display);
00062 
00063         /* Add a border and locate and deskew a 40 degree rotation */
00064     pixb2 = pixAddBorder(pixb1, BORDER, 0);
00065     pixGetDimensions(pixb2, &w, &h, NULL);
00066     pixSaveTiled(pixb2, pixa, 2, 1, 20, 8);
00067     pixr = pixRotateBySampling(pixb2, w / 2, h / 2,
00068                                     deg2rad * 40., L_BRING_IN_WHITE);
00069     regTestWritePixAndCheck(rp, pixr, IFF_PNG);  /* 1 */
00070     pixSaveTiled(pixr, pixa, 2, 0, 20, 0);
00071     pixFindSkewSweepAndSearchScorePivot(pixr, &angle, &conf, NULL, 1, 1,
00072                                         0.0, 45.0, 2.0, 0.03,
00073                                         L_SHEAR_ABOUT_CENTER);
00074     fprintf(stderr, "Should be 40 degrees: angle = %7.3f, conf = %7.3f\n",
00075             angle, conf);
00076     pixf = pixRotateBySampling(pixr, w / 2, h / 2,
00077                                     deg2rad * angle, L_BRING_IN_WHITE);
00078     pixd = pixRemoveBorder(pixf, BORDER);
00079     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 2 */
00080     pixSaveTiled(pixd, pixa, 2, 0, 20, 0);
00081     pixDestroy(&pixr);
00082     pixDestroy(&pixf);
00083     pixDestroy(&pixd);
00084 
00085         /* Do a rotation larger than 90 degrees using embedding;
00086          * Use 2 sets of measurements at 90 degrees to scan the
00087          * full range of possible rotation angles. */
00088     pixGetDimensions(pixb1, &w, &h, NULL);
00089     pixr = pixRotate(pixb1, deg2rad * 37., L_ROTATE_SAMPLING,
00090                      L_BRING_IN_WHITE, w, h);
00091     regTestWritePixAndCheck(rp, pixr, IFF_PNG);  /* 3 */
00092     pixSaveTiled(pixr, pixa, 2, 1, 20, 0);
00093     startTimer();
00094     pixFindSkewOrthogonalRange(pixr, &angle, &conf, 2, 1,
00095                                47.0, 1.0, 0.03, 0.0);
00096     fprintf(stderr, "Orth search time: %7.3f sec\n", stopTimer());
00097     fprintf(stderr, "Should be about -128 degrees: angle = %7.3f\n", angle);
00098     pixd = pixRotate(pixr, deg2rad * angle, L_ROTATE_SAMPLING,
00099                      L_BRING_IN_WHITE, w, h);
00100     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 4 */
00101     pixGetDimensions(pixd, &wd, &hd, NULL);
00102     pixc = pixCreate(w, h, 1);
00103     pixRasterop(pixc, 0, 0, w, h, PIX_SRC, pixd, (wd - w) / 2, (hd - h) / 2);
00104     regTestWritePixAndCheck(rp, pixc, IFF_PNG);  /* 5 */
00105     pixSaveTiled(pixc, pixa, 2, 0, 20, 0);
00106     pixDestroy(&pixr);
00107     pixDestroy(&pixf);
00108     pixDestroy(&pixd);
00109     pixDestroy(&pixc);
00110 
00111     pixd = pixaDisplay(pixa, 0, 0);
00112     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 6 */
00113     pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display);
00114     pixDestroy(&pixd);
00115 
00116     pixDestroy(&pixs);
00117     pixDestroy(&pixb1);
00118     pixDestroy(&pixb2);
00119     pixaDestroy(&pixa);
00120     regTestCleanup(rp);
00121     return 0;
00122 }
00123 
00124 #if 0
00125     pixFindSkewSweepAndSearchScore(pixs, &angle, &conf, &endscore,
00126                                    4, 2, 0.0, 5.0, 1.0, 0.01);
00127     fprintf(stderr, "angle = %8.4f, conf = %8.4f, endscore = %f\n",
00128             angle, conf, endscore);
00129     startTimer();
00130     pixd = pixDeskew(pixs, DESKEW_REDUCTION);
00131     fprintf(stderr, "Time to deskew = %7.4f sec\n", stopTimer());
00132     pixWrite(fileout, pixd, IFF_BMP);
00133     pixDestroy(&pixd);
00134 #endif
00135 
00136 
00137 #if 0
00138     if (pixFindSkew(pixs, &angle)) {
00139         L_WARNING("skew angle not valid", mainName);
00140         exit(1);
00141     }
00142 #endif
00143 
00144 #if 0
00145     if (pixFindSkewSweep(pixs, &angle, SWEEP_REDUCTION,
00146                          SWEEP_RANGE, SWEEP_DELTA)) {
00147         L_WARNING("skew angle not valid", mainName);
00148         exit(1);
00149     }
00150 #endif
00151 
00152 #if 0
00153     if (pixFindSkewSweepAndSearch(pixs, &angle, SWEEP_REDUCTION2,
00154                          SEARCH_REDUCTION, SWEEP_RANGE2, SWEEP_DELTA2,
00155                          SEARCH_MIN_DELTA)) {
00156         L_WARNING("skew angle not valid", mainName);
00157         exit(1);
00158     }
00159 #endif
00160 
00161 
00162 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines