Leptonica 1.68
C Image Processing Library

binarize_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  *  binarize_reg.c
00018  *
00019  *     Tests Sauvola local binarization and variants
00020  */
00021 
00022 #include "allheaders.h"
00023 
00024 PIX *PixTest1(PIX *pixs, l_int32 size, l_float32 factor, L_REGPARAMS  *rp);
00025 PIX *PixTest2(PIX *pixs, l_int32 size, l_float32 factor, l_int32 nx,
00026               l_int32 ny, L_REGPARAMS *rp);
00027 void PixTest3(PIX *pixs, l_int32 size, l_float32 factor,
00028               l_int32 nx, l_int32 ny, l_int32 paircount, L_REGPARAMS *rp);
00029 
00030 main(int    argc,
00031      char **argv)
00032 {
00033 PIX          *pixs, *pixt1, *pixt2;
00034 L_REGPARAMS  *rp;
00035 
00036     if (regTestSetup(argc, argv, &rp))
00037         return 1;
00038 
00039     pixs = pixRead("w91frag.jpg");
00040 
00041     PixTest3(pixs, 3, 0.20, 2, 3, 0, rp);
00042     PixTest3(pixs, 6, 0.20, 100, 100, 1, rp);
00043     PixTest3(pixs, 10, 0.40, 10, 10, 2, rp);
00044     PixTest3(pixs, 10, 0.40, 20, 20, 3, rp);
00045     PixTest3(pixs, 20, 0.34, 30, 30, 4, rp);
00046 
00047     pixt1 = PixTest1(pixs, 7, 0.34, rp);
00048     pixt2 = PixTest2(pixs, 7, 0.34, 4, 4, rp);
00049     regTestComparePix(rp, pixt1, pixt2);
00050     pixDestroy(&pixt1);
00051     pixDestroy(&pixt2);
00052 
00053         /* Do combination of contrast norm and sauvola */
00054     pixt1 = pixContrastNorm(NULL, pixs, 100, 100, 55, 1, 1);
00055     pixSauvolaBinarizeTiled(pixt1, 8, 0.34, 1, 1, NULL, &pixt2);
00056     regTestWritePixAndCheck(rp, pixt1, IFF_PNG);
00057     regTestWritePixAndCheck(rp, pixt2, IFF_PNG);
00058     pixDisplayWithTitle(pixt1, 100, 500, NULL, rp->display);
00059     pixDisplayWithTitle(pixt2, 700, 500, NULL, rp->display);
00060     pixDestroy(&pixt1);
00061     pixDestroy(&pixt2);
00062 
00063     regTestCleanup(rp);
00064     pixDestroy(&pixs);
00065     return 0;
00066 }
00067 
00068 
00069 PIX *
00070 PixTest1(PIX          *pixs,
00071          l_int32       size,
00072          l_float32     factor,
00073          L_REGPARAMS  *rp)
00074 {
00075 l_int32  w, h;
00076 PIX     *pixm, *pixsd, *pixth, *pixd, *pixt;
00077 PIXA    *pixa;
00078 
00079     pixm = pixsd = pixth = pixd = NULL;
00080     pixGetDimensions(pixs, &w, &h, NULL);
00081 
00082         /* Get speed */
00083     startTimer();
00084     pixSauvolaBinarize(pixs, size, factor, 1, NULL, NULL, NULL, &pixd);
00085     fprintf(stderr, "\nSpeed: 1 tile,  %7.3f Mpix/sec\n",
00086             (w * h / 1000000.) / stopTimer());
00087     pixDestroy(&pixd);
00088 
00089         /* Get results */
00090     pixSauvolaBinarize(pixs, size, factor, 1, &pixm, &pixsd, &pixth, &pixd);
00091     pixa = pixaCreate(0);
00092     pixSaveTiled(pixm, pixa, 1, 1, 30, 8);
00093     pixSaveTiled(pixsd, pixa, 1, 0, 30, 8);
00094     pixSaveTiled(pixth, pixa, 1, 1, 30, 8);
00095     pixSaveTiled(pixd, pixa, 1, 0, 30, 8);
00096     pixt = pixaDisplay(pixa, 0, 0);
00097     regTestWritePixAndCheck(rp, pixt, IFF_JFIF_JPEG);
00098     if (rp->index < 5)
00099         pixDisplayWithTitle(pixt, 100, 100, NULL, rp->display);
00100     regTestWritePixAndCheck(rp, pixd, IFF_PNG);
00101 
00102     pixaDestroy(&pixa);
00103     pixDestroy(&pixm);
00104     pixDestroy(&pixsd);
00105     pixDestroy(&pixth);
00106     pixDestroy(&pixt);
00107     return pixd;
00108 }
00109    
00110 
00111 PIX *
00112 PixTest2(PIX          *pixs,
00113          l_int32       size,
00114          l_float32     factor,
00115          l_int32       nx,
00116          l_int32       ny,
00117          L_REGPARAMS  *rp)
00118 {
00119 l_int32  w, h;
00120 PIX     *pixth, *pixd, *pixt;
00121 PIXA    *pixa;
00122 
00123     pixth = pixd = NULL;
00124     pixGetDimensions(pixs, &w, &h, NULL);
00125 
00126         /* Get speed */
00127     startTimer();
00128     pixSauvolaBinarizeTiled(pixs, size, factor, nx, ny, NULL, &pixd);
00129     fprintf(stderr, "Speed: %d x %d tiles,  %7.3f Mpix/sec\n",
00130             nx, ny, (w * h / 1000000.) / stopTimer());
00131     pixDestroy(&pixd);
00132 
00133         /* Get results */
00134     pixSauvolaBinarizeTiled(pixs, size, factor, nx, ny, &pixth, &pixd);
00135     regTestWritePixAndCheck(rp, pixth, IFF_JFIF_JPEG);
00136     regTestWritePixAndCheck(rp, pixd, IFF_PNG);
00137     if (rp->index < 5 && rp->display) {
00138         pixa = pixaCreate(0);
00139         pixSaveTiled(pixth, pixa, 1, 1, 30, 8);
00140         pixSaveTiled(pixd, pixa, 1, 0, 30, 8);
00141         pixt = pixaDisplay(pixa, 0, 0);
00142         pixDisplayWithTitle(pixt, 100, 400, NULL, rp->display);
00143         pixDestroy(&pixt);
00144         pixaDestroy(&pixa);
00145     }
00146 
00147     pixDestroy(&pixth);
00148     return pixd;
00149 }
00150  
00151 
00152 void
00153 PixTest3(PIX          *pixs,
00154          l_int32       size,
00155          l_float32     factor,
00156          l_int32       nx,
00157          l_int32       ny,
00158          l_int32       paircount,
00159          L_REGPARAMS  *rp)
00160 {
00161 PIX  *pixt1, *pixt2;
00162 
00163     pixt1 = PixTest1(pixs, size, factor, rp);
00164     pixt2 = PixTest2(pixs, size, factor, nx, ny, rp);
00165     regTestComparePix(rp, pixt1, pixt2);
00166     pixDestroy(&pixt1);
00167     pixDestroy(&pixt2);
00168     return;
00169 }
00170 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines