[go: up one dir, main page]

Menu

[r994]: / libmona / src / gethisto.cc  Maximize  Restore  History

Download this file

374 lines (282 with data), 12.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* -*- mona-c++ -*-
* Copyright (c) Leipzig, Madrid 2004 - 2008
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// $Id: gethisto.cc 938 2006-07-11 11:57:01Z write1 $
/*! \brief mona-gethisto -- calculates and prints the histogram of an image
\par Description
mona-gethisto calculates the histogram of a given image using the parameters \c --step-width, \c --minimum, and \c --maximum.
If either \c --minimum or \c --maximum is undefined the image mininum or maximum is used; the parameter
\c --step-width is required.
In addition to producing the absolute histogram, the output may be normalized that the area under curve equals one.
For anti-aliasing the histogram meight be smoothed with a median filter (the parameter \c --fltSize then defines
the actual size of the smoothing kernel.
As verbose output (\c --verbose) the first, second, and third order statistical moments are written to stdout.
With the parameter \c --nxy the abzissa values (left center containers) are returned as well.
\par Usage
<code>mona-gethisto --usage</code> (or <code>mona-gethisto --help</code>)
\param --in-file input image holding the data
\param --out-file acii output of the Histogram
\param --step-width set intervall of histogram bins
\param --minimum set the minimum bin below which the histogram is ignored
\param --maximum set the maximum bin upon which the histogram is ignored
\param --factor multiply data by factor before computing histogram
\param --fltSize filter size for histogram smoothing
\param --normalized whether the histogram should be normalized or not (false)
\param --nxy print x (containers) and y (histogram amplitudes)
\param --verbose give some verbose information (e.g., histogram statistics)
\par Examples
-# Output the histogram of a VISTA image to a file
\code
user> mona-gethisto -i image.v -d 0.1 -o image_histogram.dat
\endcode
Note, the image format can be anything covered by the io plugins included in \a libmona.
-# Output the histogram of another image between the values 0 and 200, and with a resolution of 0.1 pu and upscaled by 1E3
\code
user> mona-gethisto -i image.v -1 0 -2 200 -d 0.1 -f 1000 -o image_histogram.dat
\endcode
-# Read data from stdin, plot the normalized histogram within a region of interest and print to standard out
\code
user> mona-gethisto -i - -d 0.1 -m image_mask.v -n -o -
\endcode
Beware of setting the verbode mode here that is also writing to stdout.
-# Read data from stdin, plot the filtered normalized histogram within a region of interest and print to standard out
\code
user> mona-gethisto -i - -d 0.1 -f 3 -m image_mask.v -n -o -
\endcode
By default the size of the filter is set to 0 which means no (!) filtering, like with any size <3.
\par Known bugs
If the resolution of the histogram (number of bins) is a non-integral multiple of the intensity resolution of the data
(number of possible intensity values), then aliasing can occur.
The effect is to cause periodic zero dropouts (for an oversampled histogram) or excess-valued bins (for a slightly undersampled histogram).
Artefacts may be removed by carefully applying the filter.
If the verbosity option is set statistical information about the histogram is written to stdout, this prevents piping the output.
\sa getmtr.cc
\todo move factor to separate, generic image multiplication
\file gethisto.cc
\author M. Tittgemeyer, tittge@cbs.mpg.de, 2004
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <fstream>
#include <cstdlib>
#include <string>
#include <dlfcn.h>
// MONA
#include <mona.hh>
using namespace std;
using namespace mona;
namespace options {
// required
string in_filename;
float stepWidth = 1.0f;
// optional
string out_filename = "-";
float min = 0.0f;
float max = -1;
int fltSize = 0;
bool normalized = false;
bool nxy = false;
}
namespace {
popt::COptions opts;
void get_options( int argc, const char *argv[] )
{
opts.push_back(popt::option( options::in_filename, "in-file", 'i',
"input image of the data to calculate", NULL ));
opts.push_back(popt::option( options::out_filename, "out-file", 'o',
"ascii output of the calculated histogram values", "-" ));
opts.push_back(popt::option( options::stepWidth, "step-width", 'd',
"interval of histogram bins", "1.0"));
opts.push_back(popt::option( options::min, "minimum", '1',
"threshold below which the histogram is ignored", "0"));
opts.push_back(popt::option( options::max, "maximum", '2',
"threshold above which the histogram is ignored", "-1"));
opts.push_back(popt::option( options::fltSize, "fltSize", 'f',
"filter size for histogram smoothing", "0"));
opts.push_back(popt::option( options::normalized, "normalize", 'n', "compute normalized histogram" ));
opts.push_back(popt::option( options::nxy, "nxy", 'p', "print xy containers" ));
vector<string> non_options;
popt::parse_options(argc, argv, opts, non_options);
// what to do with unrecognized options
if ( non_options.size() > 0 )
throw invalid_argument("unknown options");
// required options (anything that has no default value)
if ( options::in_filename.empty() )
throw mona_runtime_error("'--in-file' ('i') option required");
if ( options::stepWidth == 0 )
throw mona_runtime_error("'--step-width' ('d') option required");
}
} // anonymous namespace
/*! \brief Functor to handle histogram computation
The functor class is initialised by the minimum and maximum histogram bounds,
as well as by the number od bins. If any of this parameters is set to -1 it
is calculated.
The class is derived from the image filterbase, that solely defines a result type.
Whenever using in an image filter, the result_type is specified by
<code>typename Filter::result_type</code> that needs to be resolved here.
*/
template <class TOut=double>
class CGetHistogram : public TUnaryImageFilter<CHistogram<TOut> > {
float _M_min;
float _M_max;
float _M_stepWidth;
public:
/*! Default constructor
\param min the minimum histogram container
\param max the maximum histogram container
\param stepWidth the width between histogram containers
*/
CGetHistogram(const float& min, const float& max, const float& stepWidth ):
_M_min(min), _M_max(max), _M_stepWidth(stepWidth)
{
}
/*! Functor operator
\param data any data struture that hold an (STL- style) iterator
\returns the computed histogram
*/
template <class Data3D>
typename CGetHistogram<TOut>::result_type operator () (Data3D& data)
{
// Creating histogram using data value_type and _M_bins containers
// Output type is set to double, to allow for maximum bandwidth
typename CGetHistogram<TOut>::result_type histo =
computeHistogram<TOut>(data.begin(), data.end(), _M_min, _M_max, _M_stepWidth);
// get result
return histo;
}
};
int main( int argc, const char *argv[] )
{
using namespace std;
using namespace mona;
try {
get_options(argc, argv);
// initialize the plugin handler
list<string> path;
#ifdef ENABLE_DEBUG
cverb.set_verbosity( vstream::ml_debug );
#endif
C3DImageIOPluginHandler imageio;
// read data
auto_ptr<C3DImageList> image_list(imageio.load( options::in_filename) );
if (! image_list.get() || !image_list->size()) {
string not_found = ("No supported data found in ") + options::in_filename;
throw mona_runtime_error(not_found);
}
// initialize the histogram functor
CGetHistogram<double> histogramFlt(options::min, options::max, options::stepWidth );
// compute histogram
CHistogram<double> histogram = image_filter<CGetHistogram<double> >(histogramFlt, *image_list->begin());
if ( options::fltSize < 2 ) {
cout << "filter histogram ..." << endl;
median_filter( histogram.begin(), histogram.end(), options::fltSize );
}
// get some statistical information
evaluateHistogram<double>( histogram, options::normalized, cverb );
// Write histogram
if ( options::out_filename == "-" ){
printHistogram( histogram, cout, options::normalized, options::nxy);
}else {
ofstream fout( options::out_filename.c_str() );
printHistogram( histogram, fout, options::normalized, options::nxy);
};
return EXIT_SUCCESS;
}
catch (const mona_runtime_error& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const mona_fatal_error& e){
cerr << argv[0] << " fatal: " << e.what() << endl;
}
catch (const mona_exception& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const invalid_argument &e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const exception& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (...){
cerr << argv[0] << " unknown exception" << endl;
}
return EXIT_FAILURE;
}
/*
$Log$
Revision 1.24 2005/06/29 13:32:02 wollny
move to libmona-version 0.7
Revision 1.2 2005/06/02 13:33:23 gerddie
adapt code to new plugin handling
Revision 1.1.1.1 2005/03/17 13:48:32 gerddie
initial checkin
Revision 1.23 2005/01/19 11:10:07 wollny
adapted to new verbose handling of libmona
Revision 1.22 2005/01/18 12:54:22 wollny
Changes in option handling made it neccessary to update all programs. The
default vale of an option is now given as the last !string! vale of that
option.
Revision 1.21 2005/01/18 12:28:21 tittge
adjusted documetation
Revision 1.20 2005/01/04 16:56:12 tittge
orthographic bugs removed from dokumentation
Revision 1.19 2004/12/23 14:00:52 wollny
move to inplace median filter
Revision 1.18 2004/12/22 14:56:42 tittge
facilitate median filtering to histograms
Revision 1.17 2004/11/22 10:23:06 tittge
adapt functors
Revision 1.16 2004/11/01 16:24:54 wollny
move from old error-report to cverb
Revision 1.15 2004/10/22 15:03:04 tittge
remove some memory problems (by consitently using auto_ptr's)
Revision 1.14 2004/08/23 14:59:57 tittge
histogram evaluation changesd
Revision 1.13 2004/08/11 07:26:45 wollny
added support for multiple images
Revision 1.12 2004/07/19 07:00:56 tittge
some more documentation
Revision 1.11 2004/06/03 09:57:45 wollny
Changed (hopefully) all instancable class names to Cxxxxx
Revision 1.10 2004/06/03 09:40:37 tittge
class definition changed
Revision 1.9 2004/05/13 14:29:26 wollny
adapt to new message report
Revision 1.8 2004/04/05 08:44:04 tittge
make dot tool compatible
Revision 1.7 2004/03/23 13:16:46 tittge
functors re-defined
Revision 1.6 2004/03/22 13:16:14 tittge
statistical evaluation added
Revision 1.5 2004/03/22 08:44:28 tittge
extensive re-programmimng on histogram
Revision 1.4 2004/03/19 14:08:44 tittge
changes in coding style
Revision 1.3 2004/03/17 14:35:01 tittge
program redesiged
Revision 1.2 2004/03/15 08:03:50 tittge
reading files via plugins
Revision 1.1 2004/03/12 09:31:54 tittge
modified program structure
Revision 1.2 2004/02/23 15:45:04 tittge
update to libmona-0.1 functionality
*/