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