1 /*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
27 /*
28 * rankhisto_reg.c
29 *
30 * Tests grayscale rank functions:
31 * (1) pixGetRankColorArray()
32 * (2) numaDiscretizeRankAndIntensity()
33 */
34
35 #include <math.h>
36 #include "allheaders.h"
37
38 static PIXA *PixSavePlots1(void);
39 static PIXA *PixSavePlots2(void);
40
main(int argc,char ** argv)41 int main(int argc,
42 char **argv)
43 {
44 char fname[256];
45 l_int32 i, w, h, nbins, factor;
46 l_int32 spike;
47 l_uint32 *array, *marray;
48 NUMA *na, *nan, *nai, *narbin;
49 PIX *pixs, *pixt, *pixd;
50 PIXA *pixa;
51 L_REGPARAMS *rp;
52
53 if (regTestSetup(argc, argv, &rp))
54 return 1;
55
56 /* Find the rank bin colors */
57 pixs = pixRead("map1.jpg");
58 pixGetDimensions(pixs, &w, &h, NULL);
59 factor = L_MAX(1, (l_int32)sqrt((l_float64)(w * h / 20000.0)));
60 nbins = 10;
61 pixGetRankColorArray(pixs, nbins, L_SELECT_MIN, factor, &array, 2, 6);
62 if (!array)
63 return ERROR_INT("\n\n\nFAILURE!\n\n\n", rp->testname, 1);
64 for (i = 0; i < nbins; i++)
65 fprintf(stderr, "%d: %x\n", i, array[i]);
66 pixd = pixDisplayColorArray(array, nbins, 200, 5, 6);
67 pixWrite("/tmp/lept/regout/rankhisto.0.png", pixd, IFF_PNG);
68 regTestCheckFile(rp, "/tmp/lept/regout/rankhisto.0.png"); /* 0 */
69 pixDisplayWithTitle(pixd, 100, 100, NULL, rp->display);
70 pixDestroy(&pixd);
71
72 /* Modify the rank bin colors by mapping them such
73 * that the lightest color is mapped to white */
74 marray = (l_uint32 *)lept_calloc(nbins, sizeof(l_uint32));
75 for (i = 0; i < nbins; i++)
76 pixelLinearMapToTargetColor(array[i], array[nbins - 1],
77 0xffffff00, &marray[i]);
78 pixd = pixDisplayColorArray(marray, nbins, 200, 5, 6);
79 pixWrite("/tmp/lept/regout/rankhisto.1.png", pixd, IFF_PNG);
80 regTestCheckFile(rp, "/tmp/lept/regout/rankhisto.1.png"); /* 1 */
81 pixDisplayWithTitle(pixd, 100, 600, NULL, rp->display);
82 pixDestroy(&pixd);
83 lept_free(marray);
84
85 /* Save the histogram plots */
86 pixa = PixSavePlots1();
87 pixd = pixaDisplay(pixa, 0, 0);
88 pixWrite("/tmp/lept/regout/rankhisto.2.png", pixd, IFF_PNG);
89 regTestCheckFile(rp, "/tmp/lept/regout/rankhisto.2.png"); /* 2 */
90 pixDisplayWithTitle(pixd, 100, 600, NULL, rp->display);
91 pixaDestroy(&pixa);
92 pixDestroy(&pixd);
93
94 /* Map to the lightest bin; then do TRC adjustment */
95 pixt = pixLinearMapToTargetColor(NULL, pixs, array[nbins - 1], 0xffffff00);
96 pixd = pixGammaTRC(NULL, pixt, 1.0, 0, 240);
97 pixWrite("/tmp/lept/regout/rankhisto.3.png", pixd, IFF_PNG);
98 regTestCheckFile(rp, "/tmp/lept/regout/rankhisto.3.png"); /* 3 */
99 pixDisplayWithTitle(pixd, 600, 100, NULL, rp->display);
100 pixDestroy(&pixt);
101 pixDestroy(&pixd);
102
103 /* Now test the edge cases for the histogram and rank LUT,
104 * where all the histo data is piled up at one place.
105 * We only require that the result be sensible. */
106 for (i = 0; i < 3; i++) {
107 if (i == 0)
108 spike = 0;
109 else if (i == 1)
110 spike = 50;
111 else
112 spike = 99;
113 na = numaMakeConstant(0, 100);
114 numaReplaceNumber(na, spike, 200.0);
115 nan = numaNormalizeHistogram(na, 1.0);
116 numaDiscretizeRankAndIntensity(nan, 10, &narbin, &nai, NULL, NULL);
117 snprintf(fname, sizeof(fname), "/tmp/lept/regout/rtnan%d", i + 1);
118 gplotSimple1(nan, GPLOT_PNG, fname, "Normalized Histogram");
119 snprintf(fname, sizeof(fname), "/tmp/lept/regout/rtnai%d", i + 1);
120 gplotSimple1(nai, GPLOT_PNG, fname, "Intensity vs. rank bin");
121 snprintf(fname, sizeof(fname), "/tmp/lept/regout/rtnarbin%d", i + 1);
122 gplotSimple1(narbin, GPLOT_PNG, fname, "LUT: rank bin vs. Intensity");
123 numaDestroy(&na);
124 numaDestroy(&nan);
125 numaDestroy(&narbin);
126 numaDestroy(&nai);
127 }
128
129 pixa = PixSavePlots2();
130 pixd = pixaDisplay(pixa, 0, 0);
131 pixWrite("/tmp/lept/regout/rankhisto.4.png", pixd, IFF_PNG);
132 regTestCheckFile(rp, "/tmp/lept/regout/rankhisto.4.png"); /* 4 */
133 pixDisplayWithTitle(pixd, 500, 600, NULL, rp->display);
134 pixaDestroy(&pixa);
135 pixDestroy(&pixd);
136
137 pixDestroy(&pixs);
138 lept_free(array);
139 return regTestCleanup(rp);
140 }
141
142
143 static PIXA *
PixSavePlots1(void)144 PixSavePlots1(void)
145 {
146 PIX *pixt;
147 PIXA *pixa;
148
149 pixa = pixaCreate(8);
150 pixt = pixRead("/tmp/lept/regout/rtnan.png");
151 pixSaveTiled(pixt, pixa, 1.0, 1, 20, 8);
152 pixDestroy(&pixt);
153 pixt = pixRead("/tmp/lept/regout/rtnar.png");
154 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
155 pixDestroy(&pixt);
156 pixt = pixRead("/tmp/lept/regout/rtnai.png");
157 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
158 pixDestroy(&pixt);
159 pixt = pixRead("/tmp/lept/regout/rtnarbin.png");
160 pixSaveTiled(pixt, pixa, 1.0, 1, 20, 8);
161 pixDestroy(&pixt);
162 pixt = pixRead("/tmp/lept/regout/rtnabb.png");
163 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
164 pixDestroy(&pixt);
165 pixt = pixRead("/tmp/lept/regout/rtnared.png");
166 pixSaveTiled(pixt, pixa, 1.0, 1, 20, 8);
167 pixDestroy(&pixt);
168 pixt = pixRead("/tmp/lept/regout/rtnagreen.png");
169 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
170 pixDestroy(&pixt);
171 pixt = pixRead("/tmp/lept/regout/rtnablue.png");
172 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
173 pixDestroy(&pixt);
174 return pixa;
175 }
176
177 static PIXA *
PixSavePlots2(void)178 PixSavePlots2(void)
179 {
180 PIX *pixt;
181 PIXA *pixa;
182
183 pixa = pixaCreate(9);
184 pixt = pixRead("/tmp/lept/regout/rtnan1.png");
185 pixSaveTiled(pixt, pixa, 1.0, 1, 20, 8);
186 pixDestroy(&pixt);
187 pixt = pixRead("/tmp/lept/regout/rtnai1.png");
188 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
189 pixDestroy(&pixt);
190 pixt = pixRead("/tmp/lept/regout/rtnarbin1.png");
191 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
192 pixDestroy(&pixt);
193 pixt = pixRead("/tmp/lept/regout/rtnan2.png");
194 pixSaveTiled(pixt, pixa, 1.0, 1, 20, 8);
195 pixDestroy(&pixt);
196 pixt = pixRead("/tmp/lept/regout/rtnai2.png");
197 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
198 pixDestroy(&pixt);
199 pixt = pixRead("/tmp/lept/regout/rtnarbin2.png");
200 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
201 pixDestroy(&pixt);
202 pixt = pixRead("/tmp/lept/regout/rtnan3.png");
203 pixSaveTiled(pixt, pixa, 1.0, 1, 20, 8);
204 pixDestroy(&pixt);
205 pixt = pixRead("/tmp/lept/regout/rtnai3.png");
206 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
207 pixDestroy(&pixt);
208 pixt = pixRead("/tmp/lept/regout/rtnarbin3.png");
209 pixSaveTiled(pixt, pixa, 1.0, 0, 20, 8);
210 pixDestroy(&pixt);
211 return pixa;
212 }
213