/* -*- 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
*/