[go: up one dir, main page]

Menu

[r999]: / libmona / src / autoadjust.cc  Maximize  Restore  History

Download this file

436 lines (337 with data), 13.8 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* -*- 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: autoadjust.cc 953 2006-07-13 12:40:39Z write1 $
/*! \brief mona-autoadjust -- adjust the brightness and contrast relative to a given reference
\par Description
mona-autoadjust
This program is used to adjust the brightness of and contrast of an input image
to match the intensity distribution of a reference image.
Three different distance measures are supported to rate the similarity of the image intensitiy distribution -
a statistical measure that compares the average and the deviation of the voxel intensities,
a measure that compares the histograms of both images, and a measure that compares the
intensities per voxel.
Three search strategies are supported to find the best values for brightness \b and contrast \c used to adjust
the images: A brute force grid search, where a search area (b,c) \in A_\text{search} = \[b_\text{min}, b_\text{max}] \times \[c_\text{min}, c_\text{max}]
is given, and a grid constant \g (gbf), and for every grid point the distance between the adjusted image and the reference
image is evaluated. Finally, the pair (b,c)_\text{best} providing the smallest distance is picked.
Based on this strategy, a refinement stategy is also implemented that uses a large grid constant \g (grefine). By iteratively
finding the best adjustment values (b,c)_\text{fit} and reducing the search area A_\text{search} a mest match (b,c)_\text{best} is found.
The third stategy uses a circular search around the origin. In \sectors search directions, pairs (a,b) located at a circle around the origin
are picked, and the distance of the adjusted image to the reference image is evaluated.
Then, the best match is picked and used as new search center. If the best match on the circle is worse that the match at the center
the search radius is reduced. When the search radius drops below a certain threshold (epsilon), the current search center is picked as
(b,c)_\text{best}.
Finally, (b,c)_\text{best} is used the adjust the input image providing the output image.
\par Usage
<code>mona-autoadjust --usage</code> (or <code>mona-autoadjust --help</code>)
\param --in-file input image holding the data
\param --out-file output image of the adjusted image
\param --ref-file reference image
\param --method method for adjustment (brute, refine, simple)
\param --min min threshold for masking out areas
\param --max max threshold for masking out areas
\param --sectors number of search directions
\param --cost cost function (stat, histo, intens)
\param --alpha search area parameter
\param --gbf grid parameter for brute force
\param --grefine grid parameter for refinement
\param --epsilon breaking condition
\par Examples
mona-autoadjust -i image.v -r reference.v --method refine --cost histo -o adjusted.v
\par Known bugs
only byte images supported so far
\sa
\todo add support for othet voxel representations
\file autoadjust.cc
\author Gert Wollny <wollnyatcbs.mpg.de>
*/
// $Id: autoadjust.cc 953 2006-07-13 12:40:39Z write1 $
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <time.h>
#include <unistd.h>
#include <vector>
#include "adjuster.hh"
#include <fstream>
#include <cstdlib>
#include <string>
#include <dlfcn.h>
// MONA
#include <mona.hh>
using namespace std;
using namespace mona;
/* Provide revision string */
static const char *revision = "$Revision: 953 $";
/* Initialize error handling */
#define error_report(level, text) cverb << level << (text) << endl
enum ECostFunctions { meth_intens, meth_histo, meth_stat };
popt::option::Dictionary CostOptions[] = {
{"intens", meth_intens},
{"histo", meth_histo},
{"stat", meth_stat},
{NULL, 0}
};
enum ESearchFunctions { meth_brute, meth_refine, meth_simple };
popt::option::Dictionary SearchOptions[] = {
{"brute", meth_brute},
{"refine", meth_refine},
{"simple", meth_simple},
{NULL, 0}
};
template < class Image3D >
struct adjuster {
static C3DImageWrap run(const Image3D & image,
const C3DImageWrap & reference,
TAdjuster * adjuster, int histo, long min_thresh, long max_thresh)
{
std::stringstream type_err;
type_err << "\n Image format: " << typeid(Image3D).name() << " not supported\n";
type_err << " Image format: " << typeid(C3DUBImage).name() << " expected.\n";
throw mona_runtime_error(type_err.str());
}
};
template<>
struct adjuster <const C3DUBImage > {
static C3DImageWrap run(const C3DUBImage & in_image,
const C3DImageWrap & ref, TAdjuster * adjuster, int histo,
long min_thresh, long max_thresh) {
THistTester *tester = NULL;
assert(ref.getC3DUBImage());
switch (histo) {
case meth_intens:
tester =
new TCrossAdjHistogramm(in_image, *ref.getC3DUBImage(),
min_thresh, max_thresh);
break;
case meth_histo:tester =
new TDualFullHistogramm(in_image, *ref.getC3DUBImage(),
min_thresh, max_thresh);
break;
case meth_stat:tester =
new TDualStatHistogramm(in_image, *ref.getC3DUBImage(),
min_thresh, max_thresh);
break;
default:mona_runtime_error("unknown statistics usage requested");
}
mona::cverb << vstream::ml_message << "initial fit = " << setprecision(5) << tester->get_fit() << std::endl;
TMapper map = adjuster->get_adjust_map(tester);
delete adjuster;
C3DUBImage *result = new C3DUBImage(in_image.get_size(), in_image.get_attribute_list());
C3DUBImage::const_iterator in_i = in_image.begin();
C3DUBImage::iterator res_i = result->begin();
C3DUBImage::iterator res_e = result->end();
while (res_i != res_e) {
*res_i++ = map(*in_i++);
}
delete tester;
#ifdef HAVE_CLASSES
CStringAttribute *classes = dynamic_cast <CStringAttribute * >((*result._M_attr)[string("classes")]);
if (classes) {
try {
CClasses c(classes->get_value());
c.map_values(map);
CStringAttribute *new_classes = new CStringAttribute(c.get_string());
delete classes;
result->set_attribute(string("classes"), new_classes);
}
catch(std::exception & e) {
std::cerr << e.what() << std::endl;
}
}
#endif
return C3DImageWrap(result);
}
};
/*! \brief Functor to handle
*/
class CAutoAdjust:public TUnaryImageFilter < C3DImageWrap > {
// float _M_min;
// float _M_max;
// float _M_stepWidth;
// C3DImageWrap *_M_out;
C3DImageWrap _M_refImage;
int _M_method;
int _M_histo;
long _M_min_thresh, _M_max_thresh;
int _M_sectors, _M_g_bf, _M_g_bfr;
float _M_alpha, _M_epsilon;
public:
CAutoAdjust(C3DImageWrap refImage, int method, int histo, long min_thresh,
long max_thresh, int sectors, int g_bf, int g_bfr, float alpha,
float epsilon) :
_M_refImage(refImage), _M_method(method), _M_histo(histo),
_M_min_thresh(min_thresh), _M_max_thresh(max_thresh),
_M_sectors(sectors), _M_g_bf(g_bf), _M_g_bfr(g_bfr), _M_alpha(alpha),
_M_epsilon(epsilon)
{
}
/*! Functor operator
\param data any data struture that hold an (STL- style) iterator
\returns the adjusted image
*/
template < class Data3D >
CAutoAdjust::result_type operator ()(Data3D & inImage) const
{
TAdjuster *adj = NULL;
switch (_M_method) {
case meth_brute: adj = new TBruteForceAdjuster(_M_min_thresh, _M_alpha, _M_g_bf);
break;
case meth_refine: adj = new TBruteForceRefinementAdjuster(_M_min_thresh, _M_alpha,_M_g_bfr, _M_epsilon);
break;
case meth_simple: adj = new TSimpleSearchAdjuster(_M_min_thresh, _M_sectors, _M_epsilon);
break;
default:
string unknown = ("Unknown method ") + _M_method;
throw mona_runtime_error(unknown);
}
return adjuster < Data3D >::run(inImage, _M_refImage, adj, _M_histo, _M_min_thresh, _M_max_thresh);
}
};
int main(int argc, const char *argv[])
{
#ifdef DEBUG
cverb.set_verbosity(vstream::ml_debug);
#endif
C3DImageIOPluginHandler imageio;
// required
string in_filename;
string ref_filename;
// optional
string out_filename = "-";
long min_thresh = 60;
long max_thresh = 220;
int sectors = 16;
int method;
int histo = meth_histo;
float alpha = 0.7;
int g_bf = 256;
int g_bfr = 32;
float epsilon = 0.0001;
try {
popt::COptions opts;
opts.push_back(popt::option(in_filename, "in-file", 'i',"input image of the data to calculate", NULL));
opts.push_back(popt::option(out_filename, "out-file", 'o',"output image of the adjusted image", NULL));
opts.push_back(popt::option(ref_filename, "ref-file", 'r',"reference image", NULL));
opts.push_back(popt::option(min_thresh, "min", 'l',"min - threshold for masking out areas", "60"));
opts.push_back(popt::option(max_thresh, "max", 'u',"max threshold for masking out areas", "220"));
opts.push_back(popt::option(sectors, "sectors", 's',"number of search directions", "16"));
opts.push_back(popt::option(method, SearchOptions, "method", 'm',"method for adjustment", "refine"));
opts.push_back(popt::option(histo, CostOptions, "cost", 'c',"cost function", "histo"));
opts.push_back(popt::option(alpha, "alpha", 'a',"search area parameter", "0.7"));
opts.push_back(popt::option(g_bf, "gbf", 'b',"grid parameter for brute force", "256"));
opts.push_back(popt::option(g_bfr, "grefine", 'f',"grid parameter for refinement", "32"));
opts.push_back(popt::option(epsilon, "epsilon", 'e',"breaking condition", "0.0001"));
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 (in_filename.empty())
throw mona_runtime_error("'--in-file' ('i') option required");
if (in_filename.empty())
throw mona_runtime_error("'--ref-file' ('r') option required");
cvdebug() << "found " << imageio.size() << "plugins: " << imageio.supported_formats() << "\n";
// read reference image
auto_ptr < C3DImageList > ref_image_list(imageio.load(ref_filename));
if (ref_image_list.get()) {
cvdebug() << "Got a ref image list pointer and " << ref_image_list->size() << " images \n";
}
if (!ref_image_list.get() || (ref_image_list->size() == 0)) {
string not_found = ("No supported data found in ") + ref_filename;
throw mona_runtime_error(not_found);
};
CAutoAdjust autoAdjust_filter(*ref_image_list->begin(), method,
histo, min_thresh, max_thresh,
sectors, g_bf, g_bfr, alpha,
epsilon);
// read image
auto_ptr < C3DImageList > in_image_list(imageio.load(in_filename));
if (!in_image_list.get() || !in_image_list->size()) {
string not_found = ("No supported data found in ") + in_filename;
throw mona_runtime_error(not_found);
}
// save to file
C3DImageList out_list;
out_list.push_back(image_filter(autoAdjust_filter, *in_image_list->begin()));
// write history
CHistory::instance().append(argv[0], revision, opts);
if (!imageio.save(in_image_list->get_sourceformat(), out_list, out_filename)) {
string not_save = ("unable to save result to ") + out_filename;
throw mona_runtime_error(not_save);
};
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.11 2005/06/29 13:32:02 wollny
move to libmona-version 0.7
Revision 1.5 2005/06/02 13:33:23 gerddie
adapt code to new plugin handling
Revision 1.4 2005/05/18 10:40:42 gerddie
specialisation needs const
Revision 1.3 2005/05/18 10:13:04 gerddie
more debugging
Revision 1.2 2005/05/18 10:00:56 gerddie
more debugging
Revision 1.1.1.1 2005/03/17 13:48:31 gerddie
initial checkin
Revision 1.10 2005/01/31 11:12:08 tittge
updated to doxygen 1.4.0 compatible documenation
Revision 1.9 2005/01/31 08:17:30 tittge
sourceformat output == input!
Revision 1.8 2005/01/20 18:16:48 wollny
some more comment
Revision 1.7 2005/01/20 18:13:30 wollny
Added some comment
Revision 1.6 2005/01/19 11:10:07 wollny
adapted to new verbose handling of libmona
Revision 1.5 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.4 2005/01/18 12:28:21 tittge
adjusted documetation
Revision 1.3 2005/01/11 13:58:33 tittge
set header attributes to all generated files
*/