Leptonica 1.68
C Image Processing Library

convolve_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  *  convolve_reg.c
00018  *
00019  *    Tests a number of convolution functions.
00020  */
00021 
00022 #include "allheaders.h"
00023 
00024 static const char  *kdatastr = " 20    50   80  50   20 "
00025                                " 50   100  140  100  50 "
00026                                " 90   160  200  160  90 "
00027                                " 50   100  140  100  50 "
00028                                " 20    50   80   50  20 ";
00029 
00030 
00031 main(int    argc,
00032      char **argv)
00033 {
00034 l_int32       i, j, sizex, sizey;
00035 FPIX         *fpixv, *fpixrv;
00036 L_KERNEL     *kel1, *kel2;
00037 PIX          *pixs, *pixacc, *pixg, *pixt, *pixd;
00038 PIX          *pixb, *pixm, *pixms, *pixrv, *pix1, *pix2, *pix3, *pix4;
00039 L_REGPARAMS  *rp;
00040 
00041     if (regTestSetup(argc, argv, &rp))
00042         return 1;
00043 
00044         /* Test pixBlockconvGray() on 8 bpp */
00045     pixs = pixRead("test8.jpg");
00046     pixacc = pixBlockconvAccum(pixs);
00047     pixd = pixBlockconvGray(pixs, pixacc, 3, 5);
00048     regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG);  /* 0 */
00049     pixDisplayWithTitle(pixd, 100, 0, NULL, rp->display);
00050     pixDestroy(&pixacc);
00051     pixDestroy(&pixd);
00052 
00053         /* Test pixBlockconv() on 8 bpp */
00054     pixd = pixBlockconv(pixs, 9, 8);
00055     regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG);  /* 1 */
00056     pixDisplayWithTitle(pixd, 200, 0, NULL, rp->display);
00057     pixDestroy(&pixd);
00058     pixDestroy(&pixs);
00059 
00060         /* Test pixBlockrank() on 1 bpp */
00061     pixs = pixRead("test1.png");
00062     pixacc = pixBlockconvAccum(pixs);
00063     for (i = 0; i < 3; i++) {
00064         pixd = pixBlockrank(pixs, pixacc, 4, 4, 0.25 + 0.25 * i);
00065         regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 2 - 4 */
00066         pixDisplayWithTitle(pixd, 300 + 100 * i, 0, NULL, rp->display);
00067         pixDestroy(&pixd);
00068     }
00069 
00070         /* Test pixBlocksum() on 1 bpp */
00071     pixd = pixBlocksum(pixs, pixacc, 16, 16);
00072     regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG);  /* 5 */
00073     pixDisplayWithTitle(pixd, 700, 0, NULL, rp->display);
00074     pixDestroy(&pixd);
00075     pixDestroy(&pixacc);
00076     pixDestroy(&pixs);
00077 
00078         /* Test pixCensusTransform() */
00079     pixs = pixRead("test24.jpg");
00080     pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN);
00081     pixd = pixCensusTransform(pixg, 10, NULL);
00082     regTestWritePixAndCheck(rp, pixd, IFF_PNG);  /* 6 */
00083     pixDisplayWithTitle(pixd, 800, 0, NULL, rp->display);
00084     pixDestroy(&pixd);
00085 
00086         /* Test generic convolution with kel1 */
00087     kel1 = kernelCreateFromString(5, 5, 2, 2, kdatastr);
00088     pixd = pixConvolve(pixg, kel1, 8, 1);
00089     regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG);  /* 7 */
00090     pixDisplayWithTitle(pixd, 100, 500, NULL, rp->display);
00091     pixDestroy(&pixd);
00092 
00093         /* Test convolution with flat rectangular kel */
00094     kel2 = kernelCreate(11, 11);
00095     kernelSetOrigin(kel2, 5, 5);
00096     for (i = 0; i < 11; i++) {
00097         for (j = 0; j < 11; j++)
00098             kernelSetElement(kel2, i, j, 1);
00099     }
00100     pixd = pixConvolve(pixg, kel2, 8, 1);
00101     regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG);  /* 8 */
00102     pixDisplayWithTitle(pixd, 200, 500, NULL, rp->display);
00103     pixDestroy(&pixd);
00104     kernelDestroy(&kel1);
00105     kernelDestroy(&kel2);
00106 
00107         /* Test pixBlockconv() on 32 bpp */
00108     pixt = pixScaleBySampling(pixs, 0.5, 0.5);
00109     pixd = pixBlockconv(pixt, 4, 6);
00110     regTestWritePixAndCheck(rp, pixd, IFF_JFIF_JPEG);  /* 9 */
00111     pixDisplayWithTitle(pixd, 300, 500, NULL, rp->display);
00112     pixDestroy(&pixt);
00113     pixDestroy(&pixd);
00114     pixDestroy(&pixs);
00115     pixDestroy(&pixg);
00116 
00117         /* Test pixWindowedMean() and pixWindowedMeanSquare() on 8 bpp */
00118     pixs = pixRead("feyn-fract2.tif");
00119     pixg = pixConvertTo8(pixs, 0);
00120     sizex = 5;
00121     sizey = 20;
00122     pixb = pixAddBorderGeneral(pixg, sizex + 1, sizex + 1,
00123                                sizey + 1, sizey + 1, 0);
00124     pixm = pixWindowedMean(pixb, sizex, sizey, 1, 1);
00125     pixms = pixWindowedMeanSquare(pixb, sizex, sizey, 1);
00126     regTestWritePixAndCheck(rp, pixm, IFF_JFIF_JPEG);  /* 10 */
00127     pixDisplayWithTitle(pixm, 100, 0, NULL, rp->display);
00128     pixDestroy(&pixs);
00129     pixDestroy(&pixb);
00130 
00131         /* Test pixWindowedVariance() on 8 bpp */
00132     pixWindowedVariance(pixm, pixms, &fpixv, &fpixrv);
00133     pixrv = fpixConvertToPix(fpixrv, 8, L_CLIP_TO_ZERO, 1);
00134     regTestWritePixAndCheck(rp, pixrv, IFF_JFIF_JPEG);  /* 11 */
00135     pixDisplayWithTitle(pixrv, 100, 250, NULL, rp->display);
00136     pix1 = fpixDisplayMaxDynamicRange(fpixv);
00137     pix2 = fpixDisplayMaxDynamicRange(fpixrv);
00138     pixDisplayWithTitle(pix1, 100, 500, "Variance", rp->display);
00139     pixDisplayWithTitle(pix2, 100, 750, "RMS deviation", rp->display);
00140     regTestWritePixAndCheck(rp, pix1, IFF_JFIF_JPEG);  /* 12 */
00141     regTestWritePixAndCheck(rp, pix2, IFF_JFIF_JPEG);  /* 13 */
00142     fpixDestroy(&fpixv);
00143     fpixDestroy(&fpixrv);
00144     pixDestroy(&pixm);
00145     pixDestroy(&pixms);
00146     pixDestroy(&pixrv);
00147 
00148 #if 1
00149         /* Test again all windowed functions with simpler interface */
00150     pixWindowedStats(pixg, sizex, sizey, 0, NULL, NULL, &fpixv, &fpixrv);
00151     pix3 = fpixDisplayMaxDynamicRange(fpixv);
00152     pix4 = fpixDisplayMaxDynamicRange(fpixrv);
00153     regTestComparePix(rp, pix1, pix3);  /* 14 */
00154     regTestComparePix(rp, pix2, pix4);  /* 15 */
00155     pixDestroy(&pixg);
00156     pixDestroy(&pix1);
00157     pixDestroy(&pix2);
00158     pixDestroy(&pix3);
00159     pixDestroy(&pix4);
00160     fpixDestroy(&fpixv);
00161     fpixDestroy(&fpixrv);
00162 #endif
00163 
00164     regTestCleanup(rp);
00165     return 0;
00166 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines