Leptonica 1.68
C Image Processing Library
|
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 /* 00018 * skewtest.c 00019 * 00020 * Tests various skew finding methods, optionally deskewing 00021 * the input (binary) image. The best version does a linear 00022 * sweep followed by a binary (angle-splitting) search. 00023 * The basic method is to find the vertical shear angle such 00024 * that the differential variance of ON pixels between each 00025 * line and it's neighbor, when summed over all lines, is 00026 * maximized. 00027 */ 00028 00029 #include "allheaders.h" 00030 00031 /* deskew */ 00032 #define DESKEW_REDUCTION 2 /* 1, 2 or 4 */ 00033 00034 /* sweep only */ 00035 #define SWEEP_RANGE 10. /* degrees */ 00036 #define SWEEP_DELTA 0.2 /* degrees */ 00037 #define SWEEP_REDUCTION 2 /* 1, 2, 4 or 8 */ 00038 00039 /* sweep and search */ 00040 #define SWEEP_RANGE2 10. /* degrees */ 00041 #define SWEEP_DELTA2 1. /* degrees */ 00042 #define SWEEP_REDUCTION2 2 /* 1, 2, 4 or 8 */ 00043 #define SEARCH_REDUCTION 2 /* 1, 2, 4 or 8 */ 00044 #define SEARCH_MIN_DELTA 0.01 /* degrees */ 00045 00046 00047 main(int argc, 00048 char **argv) 00049 { 00050 char *filein, *fileout; 00051 l_int32 ret; 00052 l_float32 deg2rad; 00053 l_float32 angle, conf, score; 00054 PIX *pix, *pixs, *pixd; 00055 static char mainName[] = "skewtest"; 00056 00057 if (argc != 3) 00058 return ERROR_INT(" Syntax: skewtest filein fileout", mainName, 1); 00059 00060 filein = argv[1]; 00061 fileout = argv[2]; 00062 pixd = NULL; 00063 00064 deg2rad = 3.1415926535 / 180.; 00065 00066 if ((pixs = pixRead(filein)) == NULL) 00067 return ERROR_INT("pixs not made", mainName, 1); 00068 00069 /* Find the skew angle various ways */ 00070 pix = pixConvertTo1(pixs, 130); 00071 pixWrite("/tmp/binarized.tif", pix, IFF_TIFF_G4); 00072 pixFindSkew(pix, &angle, &conf); 00073 fprintf(stderr, "pixFindSkew():\n" 00074 " conf = %5.3f, angle = %7.3f degrees\n", conf, angle); 00075 00076 pixFindSkewSweepAndSearchScorePivot(pix, &angle, &conf, &score, 00077 SWEEP_REDUCTION2, SEARCH_REDUCTION, 00078 0.0, SWEEP_RANGE2, SWEEP_DELTA2, 00079 SEARCH_MIN_DELTA, 00080 L_SHEAR_ABOUT_CORNER); 00081 fprintf(stderr, "pixFind...Pivot(about corner):\n" 00082 " conf = %5.3f, angle = %7.3f degrees, score = %f\n", 00083 conf, angle, score); 00084 00085 pixFindSkewSweepAndSearchScorePivot(pix, &angle, &conf, &score, 00086 SWEEP_REDUCTION2, SEARCH_REDUCTION, 00087 0.0, SWEEP_RANGE2, SWEEP_DELTA2, 00088 SEARCH_MIN_DELTA, 00089 L_SHEAR_ABOUT_CENTER); 00090 fprintf(stderr, "pixFind...Pivot(about center):\n" 00091 " conf = %5.3f, angle = %7.3f degrees, score = %f\n", 00092 conf, angle, score); 00093 00094 /* Use top-level */ 00095 pixd = pixDeskew(pixs, 0); 00096 pixWriteImpliedFormat(fileout, pixd, 0, 0); 00097 00098 00099 #if 0 00100 /* Do it piecemeal; fails if outside the range */ 00101 if (pixGetDepth(pixs) == 1) { 00102 pixd = pixDeskew(pix, DESKEW_REDUCTION); 00103 pixWrite(fileout, pixd, IFF_PNG); 00104 } 00105 else { 00106 ret = pixFindSkewSweepAndSearch(pix, &angle, &conf, SWEEP_REDUCTION2, 00107 SEARCH_REDUCTION, SWEEP_RANGE2, 00108 SWEEP_DELTA2, SEARCH_MIN_DELTA); 00109 if (ret) 00110 L_WARNING("skew angle not valid", mainName); 00111 else { 00112 fprintf(stderr, "conf = %5.3f, angle = %7.3f degrees\n", 00113 conf, angle); 00114 if (conf > 2.5) 00115 pixd = pixRotate(pixs, angle * deg2rad, L_ROTATE_AREA_MAP, 00116 L_BRING_IN_WHITE, 0, 0); 00117 else 00118 pixd = pixClone(pixs); 00119 pixWrite(fileout, pixd, IFF_PNG); 00120 pixDestroy(&pixd); 00121 } 00122 } 00123 #endif 00124 00125 pixDestroy(&pixs); 00126 pixDestroy(&pix); 00127 pixDestroy(&pixd); 00128 return 0; 00129 } 00130