Leptonica 1.68
C Image Processing Library

rankhisto_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  * rankhisto_reg.c
00018  *
00019  *   Tests grayscale rank functions:
00020  *      (1) pixGetRankColorArray()
00021  *      (2) numaDiscretizeRankAndIntensity()
00022  */
00023 
00024 #include <math.h>
00025 #ifndef  _WIN32
00026 #include <unistd.h>
00027 #else
00028 #include <windows.h>   /* for Sleep() */
00029 #endif  /* _WIN32 */
00030 #include "allheaders.h"
00031 
00032 static PIXA *PixSavePlots1(void);
00033 static PIXA *PixSavePlots2(void);
00034 
00035 main(int    argc,
00036      char **argv)
00037 {
00038 char          fname[256];
00039 l_int32       i, w, h, nbins, factor;
00040 l_int32       spike;
00041 l_uint32     *array, *marray;
00042 NUMA         *na, *nan, *nai, *narbin;
00043 PIX          *pixs, *pixt, *pixd;
00044 PIXA         *pixa;
00045 L_REGPARAMS  *rp;
00046 
00047     if (regTestSetup(argc, argv, &rp))
00048         return 1;
00049 
00050         /* Find the rank bin colors */
00051     pixs = pixRead("map1.jpg");
00052     pixGetDimensions(pixs, &w, &h, NULL);
00053     factor = L_MAX(1, (l_int32)sqrt((l_float64)(w * h / 20000.0)));
00054     nbins = 10;
00055     pixGetRankColorArray(pixs, nbins, L_SELECT_MIN, factor, &array, 2);
00056     if (!array)
00057         return ERROR_INT("\n\n\nFAILURE!\n\n\n", rp->testname, 1);
00058     for (i = 0; i < nbins; i++)
00059         fprintf(stderr, "%d: %x\n", i, array[i]);
00060     pixd = pixDisplayColorArray(array, nbins, 200, 5, 1);
00061     pixWrite("/tmp/rankhisto.0.png", pixd, IFF_PNG);
00062     regTestCheckFile(rp, "/tmp/rankhisto.0.png");  /* 0 */
00063     pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display);
00064     pixDestroy(&pixd);
00065 
00066         /* Modify the rank bin colors by mapping them such
00067          * that the lightest color is mapped to white */
00068     marray = (l_uint32 *)lept_calloc(nbins, sizeof(l_uint32));
00069     for (i = 0; i < nbins; i++)
00070         pixelLinearMapToTargetColor(array[i], array[nbins - 1],
00071                                     0xffffff00, &marray[i]);
00072     pixd = pixDisplayColorArray(marray, nbins, 200, 5, 1);
00073     pixWrite("/tmp/rankhisto.1.png", pixd, IFF_PNG);
00074     regTestCheckFile(rp, "/tmp/rankhisto.1.png");  /* 1 */
00075     pixDisplayWithTitle(pixd, 100, 600, NULL, rp->display);
00076     pixDestroy(&pixd);
00077     lept_free(marray);
00078 
00079         /* Save the histogram plots */
00080 #ifndef  _WIN32
00081     sleep(2);  /* give gnuplot time to write out the files */
00082 #else
00083     Sleep(2000);
00084 #endif  /* _WIN32 */
00085     pixa = PixSavePlots1();
00086     pixd = pixaDisplay(pixa, 0, 0);
00087     pixWrite("/tmp/rankhisto.2.png", pixd, IFF_PNG);
00088     regTestCheckFile(rp, "/tmp/rankhisto.2.png");  /* 2 */
00089     pixDisplayWithTitle(pixd, 100, 600, NULL, rp->display);
00090     pixaDestroy(&pixa);
00091     pixDestroy(&pixd);
00092 
00093         /* Map to the lightest bin; then do TRC adjustment */
00094     pixt = pixLinearMapToTargetColor(NULL, pixs, array[nbins - 1], 0xffffff00);
00095     pixd = pixGammaTRC(NULL, pixt, 1.0, 0, 240);
00096     pixWrite("/tmp/rankhisto.3.png", pixd, IFF_PNG);
00097     regTestCheckFile(rp, "/tmp/rankhisto.3.png");  /* 3 */
00098     pixDisplayWithTitle(pixd, 600, 100, NULL, rp->display);
00099     pixDestroy(&pixt);
00100     pixDestroy(&pixd);
00101 
00102         /* Now test the edge cases for the histogram and rank LUT,
00103          * where all the histo data is piled up at one place. 
00104          * We only require that the result be sensible. */
00105     for (i = 0; i < 3; i++) {
00106         if (i == 0)
00107             spike = 0;
00108         else if (i == 1)
00109             spike = 50;
00110         else
00111             spike = 99;
00112         na = numaMakeConstant(0, 100);
00113         numaReplaceNumber(na, spike, 200.0);
00114         nan = numaNormalizeHistogram(na, 1.0);
00115         numaDiscretizeRankAndIntensity(nan, 10, &narbin, &nai, NULL, NULL);
00116         snprintf(fname, sizeof(fname), "/tmp/rtnan%d", i + 1);
00117         gplotSimple1(nan, GPLOT_PNG, fname, "Normalized Histogram");
00118         snprintf(fname, sizeof(fname), "/tmp/rtnai%d", i + 1);
00119         gplotSimple1(nai, GPLOT_PNG, fname, "Intensity vs. rank bin");
00120         snprintf(fname, sizeof(fname), "/tmp/rtnarbin%d", i + 1);
00121         gplotSimple1(narbin, GPLOT_PNG, fname, "LUT: rank bin vs. Intensity");
00122         numaDestroy(&na);
00123         numaDestroy(&nan);
00124         numaDestroy(&narbin);
00125         numaDestroy(&nai);
00126     }
00127 #ifndef  _WIN32
00128     sleep(2);  /* give gnuplot time to write out the files */
00129 #else
00130     Sleep(2000);
00131 #endif  /* _WIN32 */
00132     pixa = PixSavePlots2();
00133     pixd = pixaDisplay(pixa, 0, 0);
00134     pixWrite("/tmp/rankhisto.4.png", pixd, IFF_PNG);
00135     regTestCheckFile(rp, "/tmp/rankhisto.4.png");  /* 4 */
00136     pixDisplayWithTitle(pixd, 500, 600, NULL, rp->display);
00137     pixaDestroy(&pixa);
00138     pixDestroy(&pixd);
00139 
00140     pixDestroy(&pixs);
00141     lept_free(array);
00142     regTestCleanup(rp);
00143     return 0;
00144 }
00145 
00146 
00147 static PIXA *
00148 PixSavePlots1(void)
00149 {
00150 PIX    *pixt;
00151 PIXA   *pixa;
00152 
00153     pixa = pixaCreate(8);
00154     pixt = pixRead("/tmp/rtnan.png");
00155     pixSaveTiled(pixt, pixa, 1, 1, 20, 8);
00156     pixDestroy(&pixt);
00157     pixt = pixRead("/tmp/rtnar.png");
00158     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00159     pixDestroy(&pixt);
00160     pixt = pixRead("/tmp/rtnai.png");
00161     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00162     pixDestroy(&pixt);
00163     pixt = pixRead("/tmp/rtnarbin.png");
00164     pixSaveTiled(pixt, pixa, 1, 1, 20, 8);
00165     pixDestroy(&pixt);
00166     pixt = pixRead("/tmp/rtnabb.png");
00167     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00168     pixDestroy(&pixt);
00169     pixt = pixRead("/tmp/rtnared.png");
00170     pixSaveTiled(pixt, pixa, 1, 1, 20, 8);
00171     pixDestroy(&pixt);
00172     pixt = pixRead("/tmp/rtnagreen.png");
00173     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00174     pixDestroy(&pixt);
00175     pixt = pixRead("/tmp/rtnablue.png");
00176     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00177     pixDestroy(&pixt);
00178     return pixa;
00179 }
00180 
00181 
00182 static PIXA *
00183 PixSavePlots2(void)
00184 {
00185 PIX    *pixt;
00186 PIXA   *pixa;
00187 
00188     pixa = pixaCreate(9);
00189     pixt = pixRead("/tmp/rtnan1.png");
00190     pixSaveTiled(pixt, pixa, 1, 1, 20, 8);
00191     pixDestroy(&pixt);
00192     pixt = pixRead("/tmp/rtnai1.png");
00193     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00194     pixDestroy(&pixt);
00195     pixt = pixRead("/tmp/rtnarbin1.png");
00196     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00197     pixDestroy(&pixt);
00198     pixt = pixRead("/tmp/rtnan2.png");
00199     pixSaveTiled(pixt, pixa, 1, 1, 20, 8);
00200     pixDestroy(&pixt);
00201     pixt = pixRead("/tmp/rtnai2.png");
00202     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00203     pixDestroy(&pixt);
00204     pixt = pixRead("/tmp/rtnarbin2.png");
00205     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00206     pixDestroy(&pixt);
00207     pixt = pixRead("/tmp/rtnan3.png");
00208     pixSaveTiled(pixt, pixa, 1, 1, 20, 8);
00209     pixDestroy(&pixt);
00210     pixt = pixRead("/tmp/rtnai3.png");
00211     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00212     pixDestroy(&pixt);
00213     pixt = pixRead("/tmp/rtnarbin3.png");
00214     pixSaveTiled(pixt, pixa, 1, 0, 20, 8);
00215     pixDestroy(&pixt);
00216     return pixa;
00217 }
00218 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines