Leptonica 1.68
C Image Processing Library

adaptnorm_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  *  adaptnorm_reg.c
00018  *
00019  *    Image normalization for two extreme cases:
00020  *       * variable and low contrast
00021  *       * good contrast but fast varying background
00022  */
00023 
00024 #include "allheaders.h"
00025 
00026 main(int    argc,
00027      char **argv)
00028 {
00029 l_int32      w, h;
00030 l_float32    mps;
00031 PIX         *pixs, *pixt, *pixmin, *pixd;
00032 PIX         *pixt1, *pixt2, *pixt3, *pixt4, *pixt5, *pixt6;
00033 PIX         *pixt7, *pixt8, *pixt9, *pixt10, *pixt11, *pixt12;
00034 PIX         *pixt13, *pixt14, *pixt15, *pixt16;
00035 PIXA        *pixac;
00036 static char  mainName[] = "adaptnorm_reg";
00037 
00038 
00039     /* ---------------------------------------------------------- *
00040      *     Normalize by adaptively expanding the dynamic range    *
00041      * ---------------------------------------------------------- */
00042     pixac = pixaCreate(0);
00043     pixs = pixRead("lighttext.jpg");
00044     pixGetDimensions(pixs, &w, &h, NULL);
00045     pixSaveTiled(pixs, pixac, 1, 1, 20, 8);
00046     startTimer();
00047     pixt1 = pixContrastNorm(NULL, pixs, 10, 10, 40, 2, 2);
00048     mps = 0.000001 * w * h / stopTimer();
00049     fprintf(stderr, "Time: Contrast norm: %7.3f Mpix/sec\n", mps);
00050     pixSaveTiled(pixt1, pixac, 1, 1, 40, 8);
00051     pixWrite("/tmp/junkpixt1.png", pixt1, IFF_PNG);
00052 
00053          /* Apply a gamma to clean up the remaining background */
00054     pixt2 = pixGammaTRC(NULL, pixt1, 1.5, 50, 235);
00055     pixSaveTiled(pixt2, pixac, 1, 0, 40, 8);
00056     pixWrite("/tmp/junkpixt2.png", pixt2, IFF_PNG);
00057 
00058          /* Here are two possible output display images; a dithered
00059           * 2 bpp image and a 7 level thresholded 4 bpp image */
00060     pixt3 = pixDitherTo2bpp(pixt2, 1);
00061     pixSaveTiled(pixt3, pixac, 1, 0, 40, 8);
00062     pixWrite("/tmp/junkpixt3.png", pixt3, IFF_PNG);
00063     pixt4 = pixThresholdTo4bpp(pixt2, 7, 1);
00064     pixSaveTiled(pixt4, pixac, 1, 0, 40, 8);
00065     pixWrite("/tmp/junkpixt4.png", pixt4, IFF_PNG);
00066 
00067          /* Binary image produced from 8 bpp normalized ones,
00068           * before and after the gamma correction. */
00069     pixt5 = pixThresholdToBinary(pixt1, 180);
00070     pixSaveTiled(pixt5, pixac, 1, 1, 40, 8);
00071     pixWrite("/tmp/junkpixt5.png", pixt5, IFF_PNG);
00072     pixt6 = pixThresholdToBinary(pixt2, 200);
00073     pixSaveTiled(pixt6, pixac, 1, 0, 40, 8);
00074     pixWrite("/tmp/junkpixt6.png", pixt6, IFF_PNG);
00075 
00076     pixDestroy(&pixs);
00077     pixDestroy(&pixt1);
00078     pixDestroy(&pixt2);
00079     pixDestroy(&pixt3);
00080     pixDestroy(&pixt4);
00081     pixDestroy(&pixt5);
00082     pixDestroy(&pixt6);
00083 
00084     pixd = pixaDisplay(pixac, 0, 0);
00085     pixDisplay(pixd, 100, 100);
00086     pixWrite("/tmp/junknorm.png", pixd, IFF_PNG);
00087     pixDestroy(&pixd);
00088     pixaDestroy(&pixac);
00089 
00090 
00091     /* ---------------------------------------------------------- *
00092      *          Normalize for rapidly varying background          *
00093      * ---------------------------------------------------------- */
00094     pixac = pixaCreate(0);
00095     pixs = pixRead("w91frag.jpg");
00096     pixGetDimensions(pixs, &w, &h, NULL);
00097     pixSaveTiled(pixs, pixac, 1, 1, 20, 8);
00098     startTimer();
00099     pixt7 = pixBackgroundNormFlex(pixs, 7, 7, 1, 1, 10);
00100     mps = 0.000001 * w * h / stopTimer();
00101     fprintf(stderr, "Time: Flexible bg norm: %7.3f Mpix/sec\n", mps);
00102     pixSaveTiled(pixt7, pixac, 1, 0, 40, 8);
00103     pixWrite("/tmp/junkpixt7.png", pixt7, IFF_PNG);
00104 
00105         /* Now do it again in several steps */
00106     pixt8 = pixScaleSmooth(pixs, 1./7., 1./7.);
00107     pixt = pixScale(pixt8, 7.0, 7.0);
00108     pixSaveTiled(pixt, pixac, 1, 1, 20, 8);
00109     pixDestroy(&pixt);
00110     pixLocalExtrema(pixt8, 0, 0, &pixmin, NULL);  /* 1's at minima */
00111     pixt9 = pixExpandBinaryReplicate(pixmin, 7);
00112     pixSaveTiled(pixt9, pixac, 1, 0, 20, 8);
00113     pixt10 = pixSeedfillGrayBasin(pixmin, pixt8, 10, 4);
00114     pixt11 = pixExtendByReplication(pixt10, 1, 1);
00115     pixt12 = pixGetInvBackgroundMap(pixt11, 200, 1, 1);  /* smoothing incl. */
00116     pixt13 = pixApplyInvBackgroundGrayMap(pixs, pixt12, 7, 7);
00117     pixSaveTiled(pixt13, pixac, 1, 0, 20, 8);
00118 
00119         /* Process the result for gray and binary output */
00120     pixt14 = pixGammaTRCMasked(NULL, pixt7, NULL, 1.0, 100, 175);
00121     pixSaveTiled(pixt14, pixac, 1, 1, 20, 8);
00122     pixt15 = pixThresholdTo4bpp(pixt14, 10, 1);
00123     pixSaveTiled(pixt15, pixac, 1, 0, 20, 8);
00124     pixt16 = pixThresholdToBinary(pixt14, 190);
00125     pixSaveTiled(pixt16, pixac, 1, 0, 20, 8);
00126 
00127     pixDestroy(&pixs);
00128     pixDestroy(&pixt7);
00129     pixDestroy(&pixmin);
00130     pixDestroy(&pixt8);
00131     pixDestroy(&pixt9);
00132     pixDestroy(&pixt10);
00133     pixDestroy(&pixt11);
00134     pixDestroy(&pixt12);
00135     pixDestroy(&pixt13);
00136     pixDestroy(&pixt14);
00137     pixDestroy(&pixt15);
00138     pixDestroy(&pixt16);
00139 
00140     pixd = pixaDisplay(pixac, 0, 0);
00141     pixDisplay(pixd, 100, 100);
00142     pixWrite("/tmp/junkflex.png", pixd, IFF_PNG);
00143     pixDestroy(&pixd);
00144     pixaDestroy(&pixac);
00145     return 0;
00146 }
00147 
00148 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines