Leptonica 1.68
C Image Processing Library

convolvetest.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  * convolvetest.c
00018  *
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include "allheaders.h"
00024 
00025 static const char  *kdatastr = " 20    50   80  50   20 "
00026                                " 50   100  140  100  50 "
00027                                " 90   160  200  160  90 "
00028                                " 50   100  140  100  50 "
00029                                " 20    50   80   50  20 ";
00030 
00031 #define  NTIMES   100
00032 
00033 main(int    argc,
00034      char **argv)
00035 {
00036 l_int32      i, j, wc, hc, d;
00037 L_KERNEL    *kel1, *kel2;
00038 PIX         *pixs, *pixg, *pixacc, *pixd, *pixt;
00039 char        *filein, *fileout;
00040 static char  mainName[] = "convolvetest";
00041 
00042     if (argc != 5)
00043         exit(ERROR_INT(" Syntax:  convolvetest filein wc hc fileout", mainName, 1));
00044 
00045     filein = argv[1];
00046     wc = atoi(argv[2]);
00047     hc = atoi(argv[3]);
00048     fileout = argv[4];
00049 
00050     if ((pixs = pixRead(filein)) == NULL)
00051         exit(ERROR_INT("pix not made", mainName, 1));
00052 
00053 #if 0  /* Measure speed */
00054     pixacc = pixBlockconvAccum(pixs);
00055     for (i = 0; i < NTIMES; i++) {
00056         pixd = pixBlockconvGray(pixs, pixacc, wc, hc);
00057         if ((i+1) % 10 == 0)
00058             fprintf(stderr, "%d iters\n", i + 1);
00059         pixDestroy(&pixd);
00060     }
00061     pixd = pixBlockconvGray(pixs, pixacc, wc, hc);
00062     pixWrite(fileout, pixd, IFF_JFIF_JPEG);
00063     pixDestroy(&pixacc);
00064 #endif
00065 
00066 #if 0  /* Test pixBlockconvGray() */
00067     pixacc = pixBlockconvAccum(pixs);
00068     pixd = pixBlockconvGray(pixs, pixacc, wc, hc);
00069     pixWrite(fileout, pixd, IFF_JFIF_JPEG);
00070     pixDestroy(&pixacc);
00071 #endif
00072 
00073 #if 0  /* Test pixBlockconv() */
00074     pixd = pixBlockconv(pixs, wc, hc);
00075     pixWrite(fileout, pixd, IFF_JFIF_JPEG);
00076 #endif
00077 
00078 #if 0  /* Test pixBlockrank() */
00079     pixacc = pixBlockconvAccum(pixs);
00080     pixd = pixBlockrank(pixs, pixacc, wc, hc, 0.5);
00081     pixWrite(fileout, pixd, IFF_TIFF_G4);
00082     pixDestroy(&pixacc);
00083 #endif
00084 
00085 #if 0  /* Test pixBlocksum() */
00086     pixacc = pixBlockconvAccum(pixs);
00087     pixd = pixBlocksum(pixs, pixacc, wc, hc);
00088     pixInvert(pixd, pixd);
00089     pixWrite(fileout, pixd, IFF_JFIF_JPEG);
00090     pixDestroy(&pixacc);
00091 #endif
00092 
00093 #if 0  /* Test pixCensusTransform() */
00094     d = pixGetDepth(pixs);
00095     if (d == 32)
00096         pixt = pixConvertRGBToLuminance(pixs);
00097     else
00098         pixt = pixClone(pixs);
00099     pixacc = pixBlockconvAccum(pixt);
00100     pixd = pixCensusTransform(pixt, wc, NULL);
00101     pixDestroy(&pixt);
00102     pixDestroy(&pixacc);
00103     pixWrite(fileout, pixd, IFF_PNG);
00104 #endif
00105 
00106 #if 1   /* Test generic convolution with kel1 */
00107     if (pixGetDepth(pixs) == 32)
00108         pixg = pixScaleRGBToGrayFast(pixs, 2, COLOR_GREEN);
00109     else
00110         pixg = pixScale(pixs, 0.5, 0.5);
00111     pixDisplay(pixg, 0, 600);
00112     kel1 = kernelCreateFromString(5, 5, 2, 2, kdatastr);
00113     pixd = pixConvolve(pixg, kel1, 8, 1);
00114     pixDisplay(pixd, 700, 0);
00115     pixWrite("/tmp/junkpixd4.bmp", pixd, IFF_BMP);
00116     pixDestroy(&pixd);
00117     kernelDestroy(&kel1);
00118 
00119         /* Test convolution with flat rectangular kel */
00120     kel2 = kernelCreate(11, 11);
00121     kernelSetOrigin(kel2, 5, 5);
00122     for (i = 0; i < 11; i++) {
00123         for (j = 0; j < 11; j++)
00124             kernelSetElement(kel2, i, j, 1);
00125     }
00126     startTimer();
00127     pixd = pixConvolve(pixg, kel2, 8, 1);
00128     fprintf(stderr, "Generic convolution: %7.3f sec\n", stopTimer());
00129     pixDisplay(pixd, 1200, 0);
00130     pixWrite("/tmp/junkpixd5.bmp", pixd, IFF_BMP);
00131     startTimer();
00132     pixt = pixBlockconv(pixg, 5, 5);
00133     fprintf(stderr, "Block convolution: %7.3f sec\n", stopTimer());
00134     pixDisplay(pixd, 1200, 600);
00135     pixWrite("/tmp/junkpixd6.bmp", pixt, IFF_BMP);
00136     pixCompareGray(pixd, pixt, L_COMPARE_ABS_DIFF, GPLOT_X11, NULL,
00137                    NULL, NULL, NULL);
00138     pixDestroy(&pixg);
00139     pixDestroy(&pixt);
00140     kernelDestroy(&kel2);
00141 #endif
00142 
00143     pixDestroy(&pixs);
00144     pixDestroy(&pixd);
00145     return 0;
00146 }
00147 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines