[go: up one dir, main page]

Menu

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

Download this file

429 lines (331 with data), 12.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
/* -*- 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: oprojection.cc,v 1.2 2006-07-24 13:08:27 wollny Exp $
/*! \brief eva-2dimagefilter
\sa 3va-2dimagefilter.cc
\file mask.cc
\author G. Wollny, wollny eva.mpg.de, 2005
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <iomanip>
#include <string>
#include <stdexcept>
#include <dlfcn.h>
#include <boost/shared_ptr.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/lambda/lambda.hpp>
#include <libmona/filter_plugin2d.hh>
#include <libmona/2DImageWrap.hh>
#include <libmona/monaException.hh>
#include <libmona/monaHistory.hh>
#include <libmona/filetools.hh>
#include <libmona/fnameana.hh>
using namespace std;
using namespace mona;
using namespace boost::lambda;
class CMaxIntenseFilter: public TUnaryImageFilter<void> {
public:
CMaxIntenseFilter(size_t dz);
template <class T>
typename CMaxIntenseFilter::result_type operator () (const T2DImage<T>& data);
bool save(const string& filebase, const string& type, const C2DImageIOPluginHandler& imageio) const;
private:
bool save(const string& filebase, const char *view, const string& type, const C2DImageWrap& image, const C2DImageIOPluginHandler& imageio) const;
bool _M_initialized;
size_t _M_slice;
size_t _M_dz;
C2DImageWrap _M_axial;
C2DImageWrap _M_coronal;
C2DImageWrap _M_saggital;
};
class CAvgIntenseFilter: public TUnaryImageFilter<void> {
public:
CAvgIntenseFilter(size_t dz);
template <class T>
typename CAvgIntenseFilter::result_type operator () (const T2DImage<T>& data);
bool save(const string& filebase, const string& type, const C2DImageIOPluginHandler& imageio) const;
private:
template <class T>
typename CAvgIntenseFilter::result_type operator () (T2DImage<T>& data, const C2DFImage& input);
bool save(const string& filebase, const char *view, const string& type, const C2DFImage& input, const C2DImageIOPluginHandler& imageio) const;
bool _M_initialized;
size_t _M_slice;
size_t _M_dz;
boost::shared_ptr<C2DFImage> _M_axial_sum;
boost::shared_ptr<C2DFImage> _M_coronal_sum;
boost::shared_ptr<C2DFImage> _M_saggital_sum;
C2DImageWrap _M_prototype;
};
CMaxIntenseFilter::CMaxIntenseFilter(size_t dz):
_M_initialized(false),
_M_slice(0),
_M_dz(dz)
{
}
bool CMaxIntenseFilter::save(const string& filebase, const char *view, const string& type, const C2DImageWrap& image, const C2DImageIOPluginHandler& imageio) const
{
C2DImageList list;
list.push_back(image);
stringstream ss;
ss << filebase << view << "." << type;
return imageio.save(type, list, ss.str());
}
bool CMaxIntenseFilter::save(const string& filebase, const string& type, const C2DImageIOPluginHandler& imageio) const
{
return save(filebase, "axial", type, _M_axial, imageio) &&
save(filebase, "coronal", type, _M_coronal, imageio) &&
save(filebase, "sagggital", type, _M_saggital, imageio);
}
template <class T>
typename CMaxIntenseFilter::result_type CMaxIntenseFilter::operator () (const T2DImage<T>& data)
{
if (!_M_initialized) {
_M_axial = C2DImageWrap(new T2DImage<T>(data.get_size()));
_M_coronal = C2DImageWrap(new T2DImage<T>(C2DBounds(data.get_size().x, _M_dz)));
_M_saggital = C2DImageWrap(new T2DImage<T>(C2DBounds(data.get_size().y, _M_dz)));
_M_initialized = true;
cvdebug() << "3D size is " << data.get_size().x << 'x' << data.get_size().y << 'x' << _M_dz << '\n';
}
T2DImage<T> *axial = _M_axial.get<T>();
T2DImage<T> *coronal = _M_coronal.get<T>();
T2DImage<T> *saggital = _M_saggital.get<T>();
if ( !axial || !coronal || !saggital) {
throw runtime_error("input images differ in type");
}
if (axial->get_size() != data.get_size())
throw runtime_error("input images differ in size");
assert(_M_slice < _M_dz);
typename T2DImage<T>::const_iterator inp = data.begin();
// axial aximum intensity
for (typename T2DImage<T>::iterator ai = axial->begin(), ae = axial->end();
ai != ae; ++inp, ++ai) {
if (*ai < *inp)
*ai = *inp;
}
// coronal maximum intensity
typename T2DImage<T>::iterator ci = coronal->begin();
advance(ci, coronal->get_size().x * _M_slice);
inp = data.begin();
for (size_t y = 0; y < data.get_size().y; ++y) {
typename T2DImage<T>::iterator cii = ci;
for (size_t x = 0; x < data.get_size().x; ++x, ++inp, ++cii) {
if (*cii < *inp)
*cii = *inp;
}
}
// saggital maximum intensity
typename T2DImage<T>::iterator si = saggital->begin();
advance(si, saggital->get_size().x * _M_slice);
inp = data.begin();
for (size_t y = 0; y < data.get_size().y; ++y, ++si) {
typename T2DImage<T>::value_type val = 0;
for (size_t x = 0; x < data.get_size().x; ++x, ++inp) {
if (val < *inp)
val = *inp;
}
*si = val;
}
++_M_slice;
}
CAvgIntenseFilter::CAvgIntenseFilter(size_t dz):
_M_initialized(false),
_M_slice(0),
_M_dz(dz)
{
}
class CCopyConvert : public TUnaryImageFilter<C2DImageWrap> {
const C2DFImage& _M_src;
public:
CCopyConvert(const C2DFImage& src):
_M_src(src)
{
}
template <class T>
typename CCopyConvert::result_type operator () (const T2DImage<T>& prototype)const
{
T2DImage<T> *output = new T2DImage<T>(_M_src.get_size());
typename T2DImage<T>::iterator outp = output->begin();
for (C2DFImage::const_iterator inp = _M_src.begin(), inpe = _M_src.end();
inp != inpe; ++inp, ++outp)
*outp = boost::numeric_cast<T>(*inp);
return C2DImageWrap(output);
}
};
bool CAvgIntenseFilter::save(const string& filebase, const char *view, const string& type,
const C2DFImage& input, const C2DImageIOPluginHandler& imageio) const
{
CCopyConvert cc(input);
C2DImageWrap image = wrap_filter(cc, _M_prototype);
C2DImageList list;
list.push_back(image);
stringstream ss;
ss << filebase << view << "." << type;
return imageio.save(type, list, ss.str());
}
bool CAvgIntenseFilter::save(const string& filebase, const string& type, const C2DImageIOPluginHandler& imageio) const
{
transform(_M_axial_sum->begin(), _M_axial_sum->end(), _M_axial_sum->begin(), _1 / _M_dz);
transform(_M_coronal_sum->begin(), _M_coronal_sum->end(), _M_coronal_sum->begin(), _1 / _M_axial_sum->get_size().x);
transform(_M_saggital_sum->begin(), _M_saggital_sum->end(), _M_saggital_sum->begin(), _1 / _M_axial_sum->get_size().y);
return save(filebase, "axial", type, *_M_axial_sum, imageio) &&
save(filebase, "coronal", type, *_M_coronal_sum, imageio) &&
save(filebase, "sagggital", type, *_M_saggital_sum, imageio);
}
template <class T>
typename CAvgIntenseFilter::result_type CAvgIntenseFilter::operator () (const T2DImage<T>& data)
{
if (!_M_initialized) {
_M_axial_sum.reset(new C2DFImage(data.get_size()));
_M_coronal_sum.reset(new C2DFImage(C2DBounds(data.get_size().x, _M_dz)));
_M_saggital_sum.reset(new C2DFImage(C2DBounds(data.get_size().y, _M_dz)));
_M_prototype = C2DImageWrap(new T2DImage<T>(C2DBounds(1,1)));
_M_initialized = true;
cvdebug() << "3D size is " << data.get_size().x << 'x' << data.get_size().y << 'x' << _M_dz << '\n';
}
if (_M_axial_sum->get_size() != data.get_size())
throw runtime_error("input images differ in size");
assert(_M_slice < _M_dz);
typename T2DImage<T>::const_iterator inp = data.begin();
// axial aximum intensity
for (C2DFImage::iterator ai = _M_axial_sum->begin(), ae = _M_axial_sum->end();
ai != ae; ++inp, ++ai) {
*ai += *inp;
}
// coronal maximum intensity
C2DFImage::iterator ci = _M_coronal_sum->begin();
advance(ci, _M_coronal_sum->get_size().x * _M_slice);
inp = data.begin();
for (size_t y = 0; y < data.get_size().y; ++y) {
C2DFImage::iterator cii = ci;
for (size_t x = 0; x < data.get_size().x; ++x, ++inp, ++cii) {
*cii += *inp;
}
}
// saggital maximum intensity
C2DFImage::iterator si = _M_saggital_sum->begin();
advance(si, _M_saggital_sum->get_size().x * _M_slice);
inp = data.begin();
for (size_t y = 0; y < data.get_size().y; ++y, ++si) {
for (size_t x = 0; x < data.get_size().x; ++x, ++inp) {
*si += *inp;
}
}
++_M_slice;
}
template <class F>
int filter(F filter, const vector<string>& src_files, C2DImageIOPluginHandler& imageio,
bool use_src_format, const string& cout_type, const string& out_filebase)
{
char new_line = cverb.show_debug() ? '\n' : '\r';
string out_type = cout_type;
for (vector<string>::const_iterator fname = src_files.begin(),
fend = src_files.end(); fname != fend; ++fname) {
cvmsg() << "Filter: " << *fname << new_line;
auto_ptr<C2DImageList> in_image_list(imageio.load(*fname));
if (in_image_list.get() && in_image_list->size()) {
if (use_src_format) {
out_type = in_image_list->get_sourceformat();
use_src_format = false;
}
wrap_filter(filter, *in_image_list->begin());
}
}
// write the three images
return filter.save(out_filebase, out_type, imageio) ? EXIT_SUCCESS : EXIT_FAILURE;
}
/* Revision string */
const char revision[] = "not specified";
int main( int argc, const char *argv[] )
{
string in_filename;
string out_filebase;
string out_type;
vector<string> filter_chain;
bool help_plugins = false;
C2DImageFilterHandler filter_plugins;
C2DImageIOPluginHandler imageio;
enum EOps {op_avg, op_max};
const popt::option::Dictionary op_dict[] = {
{"average", op_avg},
{"max", op_max},
{NULL, 0},
};
int op;
popt::COptions options;
options.push_back(popt::option( in_filename, "in-file", 'i', "input image(s) to be filtered", NULL));
options.push_back(popt::option( out_filebase, "out-base", 'o', "projection output file name base", "maximum"));
options.push_back(popt::option( out_type, imageio.get_set(), "type", 't', "output file type", NULL));
options.push_back(popt::option( op, op_dict, "op", 'p', "operation to apply", "max"));
try {
popt::parse_options(argc, argv, options, filter_chain);
cvdebug() << "IO supported types: " << imageio.plugin_names() << "\n";
if (help_plugins) {
filter_plugins.print_help();
return EXIT_SUCCESS;
}
if ( in_filename.empty() )
throw mona_runtime_error("'--in-file' ('i') option required");
if ( out_filebase.empty() )
throw mona_runtime_error("'--out-base' ('o') option required");
bool use_src_format = out_type.empty();
if (!use_src_format && !imageio.find_plugin_by_name(out_type)) {
cvwarn() << "Output file format not supported, revert to input file format\n";
use_src_format = true;
}
CHistory::instance().append(argv[0], revision, options);
vector<string> src_files = get_consecutive_numbered_files(in_filename);
if (src_files.empty())
throw invalid_argument(string("no files match pattern ") + in_filename);
cvdebug() << "filter " << src_files.size() << "images\n";
switch (op) {
case op_avg: return filter(CAvgIntenseFilter(src_files.size()), src_files, imageio, use_src_format, out_type, out_filebase);
case op_max: return filter(CMaxIntenseFilter(src_files.size()), src_files, imageio, use_src_format, out_type, out_filebase);
default: assert(!"unknown filter option returned - not possible at this point");
}
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 runtime_error &e){
cerr << argv[0] << " runtime: " << 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;
}