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 * pagesegtest2.c 00018 * 00019 * Demonstrates morphological approach to segmenting images. 00020 * 00021 * pagesegtest2 filein fileout 00022 * 00023 * where: 00024 * filein: 1, 8 or 32 bpp page image 00025 * fileout: photomask for image regions at full resolution 00026 * 00027 * This example shows how to use the morphseq specification of 00028 * a sequence of morphological and reduction/expansion operations. 00029 * 00030 * This is much simpler than generating the structuring elements 00031 * for the morph operations, specifying each of the function 00032 * calls, keeping track of the intermediate images, and removing 00033 * them at the end. 00034 * 00035 * The specific sequences below tend to work ok for images scanned at 00036 * about 600 ppi. 00037 */ 00038 00039 #include <stdio.h> 00040 #include <stdlib.h> 00041 #include "allheaders.h" 00042 00043 /* Mask at 4x reduction */ 00044 static const char *mask_sequence = "r11"; 00045 /* Seed at 4x reduction, formed by doing a 16x reduction, 00046 * an opening, and finally a 4x replicative expansion. */ 00047 static const char *seed_sequence = "r1143 + o5.5+ x4"; 00048 /* Simple dilation */ 00049 static const char *dilation_sequence = "d3.3"; 00050 00051 #define DFLAG 1 00052 00053 main(int argc, 00054 char **argv) 00055 { 00056 char *filein, *fileout; 00057 l_int32 thresh; 00058 PIX *pixs, *pixg, *pixb; 00059 PIX *pixmask4, *pixseed4, *pixsf4, *pixd4, *pixd; 00060 static char mainName[] = "pagesegtest2"; 00061 00062 if (argc != 4) 00063 exit(ERROR_INT(" Syntax: pagesegtest2 filein thresh fileout", 00064 mainName, 1)); 00065 00066 filein = argv[1]; 00067 thresh = atoi(argv[2]); 00068 fileout = argv[3]; 00069 00070 /* Get a 1 bpp version of the page */ 00071 if ((pixs = pixRead(filein)) == NULL) 00072 exit(ERROR_INT("pixs not made", mainName, 1)); 00073 if (pixGetDepth(pixs) == 32) 00074 pixg = pixConvertRGBToGrayFast(pixs); 00075 else 00076 pixg = pixClone(pixs); 00077 if (pixGetDepth(pixg) == 8) 00078 pixb = pixThresholdToBinary(pixg, thresh); 00079 else 00080 pixb = pixClone(pixg); 00081 00082 /* Make seed and mask, and fill seed into mask */ 00083 pixseed4 = pixMorphSequence(pixb, seed_sequence, 0); 00084 pixmask4 = pixMorphSequence(pixb, mask_sequence, 0); 00085 pixsf4 = pixSeedfillBinary(NULL, pixseed4, pixmask4, 8); 00086 pixd4 = pixMorphSequence(pixsf4, dilation_sequence, 0); 00087 00088 /* Mask at full resolution */ 00089 pixd = pixExpandBinaryPower2(pixd4, 4); 00090 pixWrite(fileout, pixd, IFF_TIFF_G4); 00091 00092 /* Extract non-image parts (e.g., text) at full resolution */ 00093 pixSubtract(pixb, pixb, pixd); 00094 00095 pixDisplayWithTitle(pixseed4, 400, 100, "halftone seed", DFLAG); 00096 pixDisplayWithTitle(pixmask4, 100, 100, "halftone seed mask", DFLAG); 00097 pixDisplayWithTitle(pixd4, 700, 100, "halftone mask", DFLAG); 00098 pixDisplayWithTitle(pixb, 1000, 100, "non-halftone", DFLAG); 00099 00100 #if 1 00101 pixWrite("junkseed", pixseed4, IFF_TIFF_G4); 00102 pixWrite("junkmask", pixmask4, IFF_TIFF_G4); 00103 pixWrite("junkfill", pixd4, IFF_TIFF_G4); 00104 pixWrite("junktext", pixb, IFF_TIFF_G4); 00105 #endif 00106 00107 pixDestroy(&pixs); 00108 pixDestroy(&pixg); 00109 pixDestroy(&pixb); 00110 pixDestroy(&pixseed4); 00111 pixDestroy(&pixmask4); 00112 pixDestroy(&pixsf4); 00113 pixDestroy(&pixd4); 00114 pixDestroy(&pixd); 00115 exit(0); 00116 } 00117 00118