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 * jbcorrelation.c 00018 * 00019 * jbcorrelation dirin thresh weight rootname [firstpage npages] 00020 * 00021 * dirin: directory of input pages 00022 * thresh: 0.80 - 0.85 is a reasonable compromise between accuracy 00023 * and number of classes, for characters 00024 * weight: 0.6 seems to work reasonably with thresh = 0.8. 00025 * rootname: used for naming the two output files (templates 00026 * and c.c. data) 00027 * 00028 * Note: all components larger than a default size are not saved. 00029 * The default size is given in jbclass.c. 00030 */ 00031 00032 #include "allheaders.h" 00033 00034 /* Choose one of these */ 00035 #define COMPONENTS JB_CONN_COMPS 00036 /* #define COMPONENTS JB_CHARACTERS */ 00037 /* #define COMPONENTS JB_WORDS */ 00038 00039 #define BUF_SIZE 512 00040 00041 /* select additional debug output */ 00042 #define DEBUG_TEST_DATA_IO 0 00043 #define RENDER_DEBUG 1 00044 #define DISPLAY_DIFFERENCE 0 00045 #define DISPLAY_ALL_INSTANCES 0 00046 00047 /* for display output of all instances, sorted by class */ 00048 #define X_SPACING 10 00049 #define Y_SPACING 15 00050 #define MAX_OUTPUT_WIDTH 400 00051 00052 00053 main(int argc, 00054 char **argv) 00055 { 00056 char filename[BUF_SIZE]; 00057 char *dirin, *rootname, *fname; 00058 l_int32 i, firstpage, npages, nfiles; 00059 l_float32 thresh, weight; 00060 JBDATA *data; 00061 JBCLASSER *classer; 00062 SARRAY *safiles; 00063 PIX *pix, *pixt; 00064 PIXA *pixa, *pixadb; 00065 static char mainName[] = "jbcorrelation"; 00066 00067 if (argc != 5 && argc != 7) 00068 return ERROR_INT(" Syntax: jbcorrelation dirin thresh weight " 00069 "rootname [firstpage, npages]", mainName, 1); 00070 00071 dirin = argv[1]; 00072 thresh = atof(argv[2]); 00073 weight = atof(argv[3]); 00074 rootname = argv[4]; 00075 00076 if (argc == 5) { 00077 firstpage = 0; 00078 npages = 0; 00079 } 00080 else { 00081 firstpage = atoi(argv[5]); 00082 npages = atoi(argv[6]); 00083 } 00084 00085 #if 0 00086 00087 /*--------------------------------------------------------------*/ 00088 00089 jbCorrelation(dirin, thresh, weight, COMPONENTS, rootname, 00090 firstpage, npages, 1); 00091 00092 /*--------------------------------------------------------------*/ 00093 00094 #else 00095 00096 /*--------------------------------------------------------------*/ 00097 00098 safiles = getSortedPathnamesInDirectory(dirin, NULL, firstpage, npages); 00099 nfiles = sarrayGetCount(safiles); 00100 00101 sarrayWriteStream(stderr, safiles); 00102 00103 /* Classify components on requested pages */ 00104 startTimer(); 00105 classer = jbCorrelationInit(COMPONENTS, 0, 0, thresh, weight); 00106 jbAddPages(classer, safiles); 00107 fprintf(stderr, "Time to generate classes: %6.3f sec\n", stopTimer()); 00108 00109 /* Save and write out the result */ 00110 data = jbDataSave(classer); 00111 jbDataWrite(rootname, data); 00112 fprintf(stderr, "Number of classes: %d\n", classer->nclass); 00113 00114 /* Render the pages from the classifier data. 00115 * Use debugflag == FALSE to omit outlines of each component. */ 00116 pixa = jbDataRender(data, FALSE); 00117 00118 /* Write the pages out */ 00119 npages = pixaGetCount(pixa); 00120 if (npages != nfiles) 00121 fprintf(stderr, "npages = %d, nfiles = %d, not equal!\n", 00122 npages, nfiles); 00123 for (i = 0; i < npages; i++) { 00124 pix = pixaGetPix(pixa, i, L_CLONE); 00125 snprintf(filename, BUF_SIZE, "%s.%05d", rootname, i); 00126 fprintf(stderr, "filename: %s\n", filename); 00127 pixWrite(filename, pix, IFF_PNG); 00128 pixDestroy(&pix); 00129 } 00130 00131 #if DISPLAY_DIFFERENCE 00132 fname = sarrayGetString(safiles, 0, 0); 00133 pixt = pixRead(fname); 00134 pix = pixaGetPix(pixa, 0, L_CLONE); 00135 pixXor(pixt, pixt, pix); 00136 pixWrite("junk_output_diff", pixt, IFF_PNG); 00137 pixDestroy(&pix); 00138 pixDestroy(&pixt); 00139 #endif /* DISPLAY_DIFFERENCE */ 00140 00141 #if DEBUG_TEST_DATA_IO 00142 { JBDATA *newdata; 00143 PIX *newpix; 00144 PIXA *newpixa; 00145 l_int32 same, iofail; 00146 /* Read the data back in and render the pages */ 00147 newdata = jbDataRead(rootname); 00148 newpixa = jbDataRender(newdata, FALSE); 00149 iofail = FALSE; 00150 for (i = 0; i < npages; i++) { 00151 pix = pixaGetPix(pixa, i, L_CLONE); 00152 newpix = pixaGetPix(newpixa, i, L_CLONE); 00153 pixEqual(pix, newpix, &same); 00154 if (!same) { 00155 iofail = TRUE; 00156 fprintf(stderr, "pix on page %d are unequal!\n", i); 00157 } 00158 pixDestroy(&pix); 00159 pixDestroy(&newpix); 00160 00161 } 00162 if (iofail) 00163 fprintf(stderr, "read/write for jbdata fails\n"); 00164 else 00165 fprintf(stderr, "read/write for jbdata succeeds\n"); 00166 jbDataDestroy(&newdata); 00167 pixaDestroy(&newpixa); 00168 } 00169 #endif /* DEBUG_TEST_DATA_IO */ 00170 00171 #if RENDER_DEBUG 00172 /* Use debugflag == TRUE to see outlines of each component. */ 00173 pixadb = jbDataRender(data, TRUE); 00174 /* Write the debug pages out */ 00175 npages = pixaGetCount(pixadb); 00176 for (i = 0; i < npages; i++) { 00177 pix = pixaGetPix(pixadb, i, L_CLONE); 00178 snprintf(filename, BUF_SIZE, "%s.db.%05d", rootname, i); 00179 fprintf(stderr, "filename: %s\n", filename); 00180 pixWrite(filename, pix, IFF_PNG); 00181 pixDestroy(&pix); 00182 } 00183 pixaDestroy(&pixadb); 00184 #endif /* RENDER_DEBUG */ 00185 00186 #if DISPLAY_ALL_INSTANCES 00187 /* display all instances, organized by template */ 00188 pix = pixaaDisplayByPixa(classer->pixaa, 00189 X_SPACING, Y_SPACING, MAX_OUTPUT_WIDTH); 00190 pixWrite("output_instances", pix, IFF_PNG); 00191 pixDestroy(&pix); 00192 #endif /* DISPLAY_ALL_INSTANCES */ 00193 00194 pixaDestroy(&pixa); 00195 sarrayDestroy(&safiles); 00196 jbClasserDestroy(&classer); 00197 jbDataDestroy(&data); 00198 00199 /*--------------------------------------------------------------*/ 00200 00201 #endif 00202 00203 return 0; 00204 } 00205