1 |
|
|
/* |
2 |
|
|
Teem: Tools to process and visualize scientific data and images . |
3 |
|
|
Copyright (C) 2013, 2012, 2011, 2010, 2009 University of Chicago |
4 |
|
|
Copyright (C) 2008, 2007, 2006, 2005 Gordon Kindlmann |
5 |
|
|
Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998 University of Utah |
6 |
|
|
|
7 |
|
|
This library is free software; you can redistribute it and/or |
8 |
|
|
modify it under the terms of the GNU Lesser General Public License |
9 |
|
|
(LGPL) as published by the Free Software Foundation; either |
10 |
|
|
version 2.1 of the License, or (at your option) any later version. |
11 |
|
|
The terms of redistributing and/or modifying this software also |
12 |
|
|
include exceptions to the LGPL that facilitate static linking. |
13 |
|
|
|
14 |
|
|
This library is distributed in the hope that it will be useful, |
15 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 |
|
|
Lesser General Public License for more details. |
18 |
|
|
|
19 |
|
|
You should have received a copy of the GNU Lesser General Public License |
20 |
|
|
along with this library; if not, write to Free Software Foundation, Inc., |
21 |
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#include "unrrdu.h" |
25 |
|
|
#include "privateUnrrdu.h" |
26 |
|
|
|
27 |
|
|
#define INFO "Perform histogram equalization" |
28 |
|
|
static const char *_unrrdu_heqInfoL = |
29 |
|
|
(INFO |
30 |
|
|
". If this seems to be doing nothing, try increasing the " |
31 |
|
|
"number of histograms bins by an order of magnitude or " |
32 |
|
|
"two (or more). Or, use \"unu gamma\" to warp the values " |
33 |
|
|
"in the direction you know they need to go. Either of " |
34 |
|
|
"these might work because extremely tall and narrow peaks " |
35 |
|
|
"in the equalization histogram will produce poor results.\n " |
36 |
|
|
"* Uses nrrdHistoEq"); |
37 |
|
|
|
38 |
|
|
int |
39 |
|
|
unrrdu_heqMain(int argc, const char **argv, const char *me, |
40 |
|
|
hestParm *hparm) { |
41 |
|
2 |
hestOpt *opt = NULL; |
42 |
|
1 |
char *out, *err, *mapS; |
43 |
|
1 |
Nrrd *nin, *nout, *nmap; |
44 |
|
1 |
int smart, pret; |
45 |
|
1 |
unsigned int bins; |
46 |
|
|
airArray *mop; |
47 |
|
1 |
float amount; |
48 |
|
|
|
49 |
|
|
/* we want to facilitate saving out the mapping as a text file, |
50 |
|
|
but with the domain included */ |
51 |
|
|
/* this is commented out with the 8 Aug 2003 advent of nrrdDefGetenv |
52 |
|
|
nrrdDefWriteBareTable = AIR_FALSE; |
53 |
|
|
*/ |
54 |
|
|
|
55 |
|
1 |
hestOptAdd(&opt, "b,bin", "bins", airTypeInt, 1, 1, &bins, NULL, |
56 |
|
|
"# bins to use in histogram that is created in order to " |
57 |
|
|
"calculate the mapping that achieves the equalization."); |
58 |
|
1 |
hestOptAdd(&opt, "s,smart", "bins", airTypeInt, 0, 1, &smart, "0", |
59 |
|
|
"# bins in value histogram to ignore in calculating the mapping. " |
60 |
|
|
"Bins are ignored when they get more hits than other bins, and " |
61 |
|
|
"when the values that fall in them are constant. This is an " |
62 |
|
|
"effective way to prevent large regions of background value " |
63 |
|
|
"from distorting the equalization mapping."); |
64 |
|
1 |
hestOptAdd(&opt, "a,amount", "amount", airTypeFloat, 1, 1, &amount, "1.0", |
65 |
|
|
"extent to which the histogram equalizing mapping should be " |
66 |
|
|
"applied; 0.0: no change, 1.0: full equalization"); |
67 |
|
1 |
hestOptAdd(&opt, "m,map", "filename", airTypeString, 1, 1, &mapS, "", |
68 |
|
|
"The value mapping used to achieve histogram equalization is " |
69 |
|
|
"represented by a univariate regular map. By giving a filename " |
70 |
|
|
"here, that map can be saved out and applied to other nrrds " |
71 |
|
|
"with \"unu rmap\""); |
72 |
|
1 |
OPT_ADD_NIN(nin, "input nrrd"); |
73 |
|
1 |
OPT_ADD_NOUT(out, "output nrrd"); |
74 |
|
|
|
75 |
|
1 |
mop = airMopNew(); |
76 |
|
1 |
airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways); |
77 |
|
|
|
78 |
✓✗ |
2 |
USAGE(_unrrdu_heqInfoL); |
79 |
|
|
PARSE(); |
80 |
|
|
airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways); |
81 |
|
|
|
82 |
|
|
nout = nrrdNew(); |
83 |
|
|
airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways); |
84 |
|
|
|
85 |
|
|
if (nrrdHistoEq(nout, nin, airStrlen(mapS) ? &nmap : NULL, |
86 |
|
|
bins, smart, amount)) { |
87 |
|
|
airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); |
88 |
|
|
fprintf(stderr, "%s: trouble histogram equalizing:\n%s", me, err); |
89 |
|
|
airMopError(mop); |
90 |
|
|
return 1; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
if (airStrlen(mapS)) { |
94 |
|
|
SAVE(mapS, nmap, NULL); |
95 |
|
|
} |
96 |
|
|
SAVE(out, nout, NULL); |
97 |
|
|
|
98 |
|
|
airMopOkay(mop); |
99 |
|
|
return 0; |
100 |
|
1 |
} |
101 |
|
|
|
102 |
|
|
UNRRDU_CMD(heq, INFO); |