Leptonica 1.68
C Image Processing Library

rank_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  * rank_reg.c
00018  *
00019  *   Tests grayscale rank functions:
00020  *      (1) pixRankFilterGray()
00021  *      (2) pixScaleGrayMinMax()
00022  *      (3) pixScaleGrayRankCascade()
00023  */
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include "allheaders.h"
00028 
00029 static const l_int32   SIZE = 20;
00030 
00031 
00032 main(int    argc,
00033      char **argv)
00034 {
00035 l_int32       i, j, w, h, same;
00036 l_float32     t, t1, t2;
00037 GPLOT        *gplot;
00038 NUMA         *nax, *nay1, *nay2;
00039 PIX          *pixs, *pixd, *pixt1, *pixt2, *pixt3, *pixt4;
00040 PIXA         *pixa;
00041 static char   mainName[] = "rank_reg";
00042 
00043     if (argc != 1)
00044         exit(ERROR_INT(" Syntax: rank_reg", mainName, 1));
00045 
00046     if ((pixs = pixRead("lucasta.150.jpg")) == NULL)
00047         exit(ERROR_INT("pixs not made", mainName, 1));
00048     pixGetDimensions(pixs, &w, &h, NULL);
00049 
00050     startTimer();
00051     pixd = pixRankFilterGray(pixs, 15, 15, 0.4);
00052     t = stopTimer();
00053     fprintf(stderr, "Time =  %7.3f sec\n", t);
00054     fprintf(stderr, "MPix/sec: %7.3f\n", 0.000001 * w * h / t);
00055     pixDisplay(pixs, 0, 200);
00056     pixDisplay(pixd, 600, 200);
00057     pixWrite("/tmp/junkfilter.png", pixd, IFF_PNG);
00058     pixDestroy(&pixd);
00059 
00060         /* Get results for dilation */
00061     startTimer();
00062     pixt1 = pixDilateGray(pixs, 15, 15);
00063     t = stopTimer();
00064     fprintf(stderr, "Dilation time =  %7.3f sec\n", t);
00065 
00066         /* Get results for erosion */
00067     pixt2 = pixErodeGray(pixs, 15, 15);
00068 
00069         /* Get results using the rank filter for rank = 0.0 and 1.0.
00070          * Don't use 0.0 or 1.0, because those are dispatched
00071          * automatically to erosion and dilation! */
00072     pixt3 = pixRankFilterGray(pixs, 15, 15, 0.0001);
00073     pixt4 = pixRankFilterGray(pixs, 15, 15, 0.9999);
00074 
00075         /* Compare */
00076     pixEqual(pixt1, pixt4, &same);
00077     if (same)
00078         fprintf(stderr, "Correct: dilation results same as rank 1.0\n");
00079     else
00080         fprintf(stderr, "Error: dilation results differ from rank 1.0\n");
00081     pixEqual(pixt2, pixt3, &same);
00082     if (same)
00083         fprintf(stderr, "Correct: erosion results same as rank 0.0\n");
00084     else
00085         fprintf(stderr, "Error: erosion results differ from rank 0.0\n");
00086     pixDestroy(&pixt1);
00087     pixDestroy(&pixt2);
00088     pixDestroy(&pixt3);
00089     pixDestroy(&pixt4);
00090 
00091     fprintf(stderr, "\n----------------------------------------\n");
00092     fprintf(stderr, "The next part takes about 30 seconds\n");
00093     fprintf(stderr, "----------------------------------------\n\n");
00094 
00095     nax = numaMakeSequence(1, 1, SIZE);
00096     nay1 = numaCreate(SIZE);
00097     nay2 = numaCreate(SIZE);
00098     gplot = gplotCreate("/tmp/junkroot", GPLOT_X11, "sec/MPix vs filter size",
00099                         "size", "time");
00100     for (i = 1; i <= SIZE; i++) {
00101         t1 = t2 = 0.0;
00102         for (j = 0; j < 5; j++) {
00103             startTimer();
00104             pixt1 = pixRankFilterGray(pixs, i, SIZE + 1, 0.5);
00105             t1 += stopTimer();
00106             pixDestroy(&pixt1);
00107             startTimer();
00108             pixt1 = pixRankFilterGray(pixs, SIZE + 1, i, 0.5);
00109             t2 += stopTimer();
00110             if (j == 0)
00111                 pixDisplayWrite(pixt1, 1);
00112             pixDestroy(&pixt1);
00113         }
00114         numaAddNumber(nay1, 1000000. * t1 / (5. * w * h));
00115         numaAddNumber(nay2, 1000000. * t2 / (5. * w * h));
00116     }
00117     gplotAddPlot(gplot, nax, nay1, GPLOT_LINES, "vertical");
00118     gplotAddPlot(gplot, nax, nay2, GPLOT_LINES, "horizontal");
00119     gplotMakeOutput(gplot);
00120     gplotDestroy(&gplot);
00121 
00122         /* Display tiled */
00123     pixa = pixaReadFiles("/tmp", "junk_write_display");
00124     pixd = pixaDisplayTiledAndScaled(pixa, 8, 250, 5, 0, 25, 2);
00125     pixWrite("/tmp/junktiles.jpg", pixd, IFF_JFIF_JPEG);
00126     pixDestroy(&pixd);
00127     pixaDestroy(&pixa);
00128     pixDestroy(&pixs);
00129 
00130     pixDisplayWrite(NULL, -1);  /* clear out */
00131 
00132     pixs = pixRead("test8.jpg");
00133     for (i = 1; i <= 4; i++) {
00134         pixt1 = pixScaleGrayRank2(pixs, i);
00135         pixDisplay(pixt1, 300 * (i - 1), 100);
00136         pixDestroy(&pixt1);
00137     }
00138     pixDestroy(&pixs);
00139 
00140     pixs = pixRead("test24.jpg");
00141     pixt1 = pixConvertRGBToLuminance(pixs);
00142     pixt2 = pixScale(pixt1, 1.5, 1.5);
00143     for (i = 1; i <= 4; i++) {
00144         for (j = 1; j <= 4; j++) {
00145             pixt3 = pixScaleGrayRankCascade(pixt2, i, j, 0, 0);
00146             pixDisplayWrite(pixt3, 1);
00147             pixDestroy(&pixt3);
00148         }
00149     }
00150     pixDestroy(&pixt1);
00151     pixDestroy(&pixt2);
00152     pixDestroy(&pixs);
00153     pixDisplayMultiple("/tmp/junk_write_display*");
00154     return 0;
00155 }
00156 
00157 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines