[go: up one dir, main page]

Menu

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

Download this file

343 lines (254 with data), 9.6 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
/* -*- 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: mask.cc 938 2006-07-11 11:57:01Z write1 $
/*! \brief mona-mask -- mask out a region of interest from an image
\par Program description
The programm allows to masked out a region of interest from an image,
vector field, mesh, etc. Whatever is supported by a plugin.
\par Usage
<code>mona-mask --usage</code> (or <code> mona-mask --help</code>)
\param --in-imagee image to be masked
\param --mask-image a region of interest given as a binary image
\param --out-image the result
\param --not return anything which is NOT in mask
\param --verbose some verbose output (min/max values, etc)
\par Example
-# Mask a region of interest from a VISTA image
\code
user> mona-mask -i image.v -m binary-mask.v -o out-image.v
\endcode
Note, the image format can be anything covered by the io plugins included in \a libmona.
\todo
yet only applies for images; to be extended for meshes, vector fields, ...
\par Known bugs
Still none known.
\sa getmtr.cc
\file mask.cc
\author M. Tittgemeyer, tittge@cbs.mpg.de, 2004
*/
// $Id: mask.cc 938 2006-07-11 11:57:01Z write1 $
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <string>
#include <dlfcn.h>
#include <mona.hh>
using namespace std;
using namespace mona;
/* Revision string */
static const char *revision = "$Revision: 938 $";
template <class T>
struct invert_binary_mask {
T operator () (const T& value, bool mask)
{
return mask ? T() : value;
}
};
template <class T>
struct binary_mask {
T operator () (const T& value, bool mask)
{
return mask ? value : T();
}
};
template <class Data3D>
Data3D *do_mask(const C3DImageWrap& mask, const Data3D& data, const bool invert)
{
if ( data.get_size() != mask.get_size() ) {
cvwarn() << "Sizes of input and mask differ " << endl;
return NULL;
}
// get the actual image
const C3DBitImage* bit_mask = mask.getC3DBitImage();
// check whether mask is binary
if ( !bit_mask ){
cvwarn() << "Mask need to be bit type" << endl;
return NULL;
};
Data3D *result_image = new Data3D( data.get_size(), data.get_attribute_list() );
if (invert)
transform(data.begin(), data.end(), bit_mask->begin(), result_image->begin(), invert_binary_mask<typename Data3D::value_type>());
else
transform(data.begin(), data.end(), bit_mask->begin(), result_image->begin(), binary_mask<typename Data3D::value_type>());
return result_image;
}
/*! \brief Functor to mask images
The functor class is initialised by a mask image to assess values in a
specific region of interest.
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 Data3DWrap >
class CMaskData: public TUnaryImageFilter<Data3DWrap> {
C3DImageWrap _M_mask;
bool _M_not;
public:
//! \name Constructors
//@{
/*! Default constructor
\param mask a binary mask facilitating a regional analysis
*/
CMaskData(C3DImageWrap mask, bool not_in_mask):
_M_mask(mask), _M_not(not_in_mask)
{
}
//! \name Operator
//@{
/*! Brace operator
\param data any data struture that hold an (STL- style) iterator
\returns the masked image
*/
template <class Data3D>
typename CMaskData::result_type operator () (const Data3D& data) const
{
Data3D *result = do_mask(_M_mask, data, _M_not);
return Data3DWrap(result);
}
//@}
}; // CMaskData
int main( int argc, const char *argv[] )
{
string in_filename;
string mask_filename;
string out_filename;
bool not_masked = false;
try {
popt::COptions opts;
opts.push_back(popt::option( in_filename, "in-image", 'i', "input image to be masked", NULL));
opts.push_back(popt::option( mask_filename, "mask-image", 'm', "a region of interest given as binary image mask", NULL ));
opts.push_back(popt::option( out_filename, "out-image", 'o', "output image that have been masked", NULL));
opts.push_back(popt::option( not_masked, "not", 'n', "mask out anything which is in mask" ));
vector<string> non_options;
popt::parse_options(argc, argv, opts, non_options);
if ( non_options.size() > 0 )
throw invalid_argument("unknown options");
if ( in_filename.empty() )
throw mona_runtime_error("'--in-image' ('i') option required");
if ( out_filename.empty() )
throw mona_runtime_error("'--out-image' ('o') option required");
if ( mask_filename.empty() )
throw mona_runtime_error("'--mask-image' ('m') option required");
C3DImageIOPluginHandler imageio;
// read mask
auto_ptr<C3DImageList> mask_image_list(imageio.load(mask_filename));
if (!mask_image_list.get() || (mask_image_list->size() == 0) ) {
string not_found = ("No supported data found in ") + mask_filename;
throw mona_runtime_error(not_found);
};
// read image
auto_ptr<C3DImageList> in_image_list(imageio.load(in_filename));
if (in_image_list.get() && in_image_list->size()) {
CMaskData<C3DImageWrap> mask_filter( *mask_image_list->begin(), not_masked );
C3DImageList out_list;
out_list.push_back( image_filter( mask_filter, *in_image_list->begin() ) );
// write history
CHistory::instance().append(argv[0], revision, opts);
// save to file
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;
}else {
C3DVFieldIOPluginHandler fieldio;
auto_ptr<C3DVectorfieldPtr> field(fieldio.load(in_filename));
if (field.get()) {
CMaskData<C3DVectorfieldPtr> mask_filter( *mask_image_list->begin(), not_masked );
C3DVectorfieldPtr result = mask_filter(field->get_data());
if (!result)
throw mona_runtime_error("masking failed");
// write history
CHistory::instance().append(argv[0], revision, opts);
if ( !fieldio.save(in_image_list->get_sourceformat(), result, out_filename) ){
string not_save = ("unable to save result to ") + out_filename;
throw mona_runtime_error(not_save);
};
}else {
string not_found = ("unable to load vector field in ") + in_filename;
throw mona_runtime_error(not_found);
}
}
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.13 2005/06/29 13:32:02 wollny
move to libmona-version 0.7
Revision 1.4 2005/06/22 11:46:11 tittge
history attrib added to output
Revision 1.3 2005/06/02 13:33:23 gerddie
adapt code to new plugin handling
Revision 1.2 2005/04/07 17:15:59 gerddie
corrected some const specifier
Revision 1.1.1.1 2005/03/17 13:48:32 gerddie
initial checkin
Revision 1.12 2005/01/27 17:47:19 tittge
allow to apply mask inversion
Revision 1.11 2005/01/19 11:10:07 wollny
adapted to new verbose handling of libmona
Revision 1.10 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.9 2004/12/30 14:16:42 wollny
add support for masking vector fields
Revision 1.8 2004/11/15 14:27:45 tittge
passing of attributes enhanced
Revision 1.7 2004/11/01 16:24:54 wollny
move from old error-report to cverb
Revision 1.6 2004/10/22 15:03:04 tittge
remove some memory problems (by consitently using auto_ptr's)
Revision 1.5 2004/10/15 14:24:45 tittge
added a crisp (maximum likelyhood) segmentation
Revision 1.4 2004/09/22 09:06:38 tittge
added program for masking data
Revision 1.3 2004/09/20 13:30:25 tittge
remove writing bug from mask.cc
Revision 1.2 2004/08/25 12:08:11 tittge
determine some SIMD optimization
Revision 1.1 2004/08/24 12:43:36 tittge
optimization flags if no debug mode; program to mask images
Revision 1.10 2004/06/03 09:57:45 wollny
Changed (hopefully) all instancable class names to Cxxxxx
*/