Leptonica 1.68
C Image Processing Library

livre_adapt.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  * livre_adapt.c
00018  *
00019  * This shows two ways to normalize a document image for uneven
00020  * illumination.  It is somewhat more complicated than using the
00021  * morphological tophat.
00022  */
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include "allheaders.h"
00027 
00028 
00029 main(int    argc,
00030      char **argv)
00031 {
00032 l_int32      d;
00033 PIX         *pixs, *pixc, *pixr, *pixg, *pixb, *pixsg, *pixsm, *pixd;
00034 PIXA        *pixa;
00035 static char  mainName[] = "livre_adapt";
00036 
00037     if (argc != 1)
00038         exit(ERROR_INT(" Syntax:  livre_adapt", mainName, 1));
00039 
00040         /* Read the image in at 150 ppi. */
00041     pixDisplayWrite(NULL, -1);
00042     if ((pixs = pixRead("brothers.150.jpg")) == NULL)
00043         exit(ERROR_INT("pix not made", mainName, 1));
00044     pixDisplayWriteFormat(pixs, 2, IFF_JFIF_JPEG);
00045 
00046         /* Normalize for uneven illumination on RGB image */
00047     pixBackgroundNormRGBArraysMorph(pixs, NULL, 4, 5, 200,
00048                                     &pixr, &pixg, &pixb);
00049     pixd = pixApplyInvBackgroundRGBMap(pixs, pixr, pixg, pixb, 4, 4);
00050     pixDisplayWriteFormat(pixd, 2, IFF_JFIF_JPEG); 
00051     pixDestroy(&pixr);
00052     pixDestroy(&pixg);
00053     pixDestroy(&pixb);
00054     pixDestroy(&pixd);
00055 
00056         /* Convert the RGB image to grayscale. */
00057     pixsg = pixConvertRGBToLuminance(pixs);
00058     pixDisplayWriteFormat(pixsg, 2, IFF_JFIF_JPEG);
00059 
00060         /* Remove the text in the fg. */
00061     pixc = pixCloseGray(pixsg, 25, 25);
00062     pixDisplayWriteFormat(pixc, 2, IFF_JFIF_JPEG);
00063 
00064         /* Smooth the bg with a convolution. */
00065     pixsm = pixBlockconv(pixc, 15, 15);
00066     pixDisplayWriteFormat(pixsm, 2, IFF_JFIF_JPEG);
00067     pixDestroy(&pixc);
00068 
00069         /* Normalize for uneven illumination on gray image. */
00070     pixBackgroundNormGrayArrayMorph(pixsg, NULL, 4, 5, 200, &pixg);
00071     pixc = pixApplyInvBackgroundGrayMap(pixsg, pixg, 4, 4);
00072     pixDisplayWriteFormat(pixc, 2, IFF_JFIF_JPEG);
00073     pixDestroy(&pixg);
00074 
00075         /* Increase the dynamic range. */
00076     pixd = pixGammaTRC(NULL, pixc, 1.0, 30, 180);
00077     pixDisplayWriteFormat(pixd, 2, IFF_JFIF_JPEG);
00078     pixDestroy(&pixc);
00079 
00080         /* Threshold to 1 bpp. */
00081     pixb = pixThresholdToBinary(pixd, 120);
00082     pixDisplayWriteFormat(pixb, 2, IFF_PNG);
00083     pixDestroy(&pixd);
00084     pixDestroy(&pixb);
00085 
00086             /* Generate the output image */
00087     pixa = pixaReadFiles("/tmp", "junk_write_display");
00088     pixd = pixaDisplayTiledAndScaled(pixa, 8, 350, 4, 0, 25, 2);
00089     pixWrite("/tmp/adapt.jpg", pixd, IFF_JFIF_JPEG);
00090     pixDisplayWithTitle(pixd, 100, 100, NULL, 1);
00091     pixDestroy(&pixd);
00092 
00093     pixDestroy(&pixs);
00094     pixDestroy(&pixsg);
00095     return 0;
00096 }
00097 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines