A.I.M.S algorithms


imagealgorithm.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMSALGO_ALGORITHM_IMAGEALGORITHM_H
35 #define AIMSALGO_ALGORITHM_IMAGEALGORITHM_H
36 
37 //--- aims -------------------------------------------------------------------
38 #include <aims/data/data.h>
39 #include <aims/utility/channel.h>
40 #include <aims/vector/vector.h>
41 //--- cartodata --------------------------------------------------------------
43 //--- cartobase --------------------------------------------------------------
46 #include <cartobase/type/types.h>
49 #include <cartobase/smart/rcptr.h>
50 //--- std --------------------------------------------------------------------
51 #include <iostream>
52 //----------------------------------------------------------------------------
53 
54 namespace aims
55 {
56  template <typename T> class ImageAlgorithmInterface;
57  template <typename T> class ImageAlgorithm;
58  template <typename T, bool M> class ImageAlgorithmSwitch;
59 
60  //==========================================================================
61  // IMAGE ALGORITHM INTERFACE
62  //==========================================================================
68  template <typename T>
70  public:
71  //----------------------------------------------------------------------
72  // types
73  //----------------------------------------------------------------------
74  typedef T VoxelType;
75 
76  //----------------------------------------------------------------------
77  // interface (needs implementation in derived classes)
78  //----------------------------------------------------------------------
85  virtual Point4dl getOutputImageDimensions( const Point4dl & dims ) const
86  {
87  return Point4dl( dims );
88  }
89 
96  virtual Point4df getOutputImageVoxelSize( const Point4df & voxelsize ) const
97  {
98  return Point4df( voxelsize );
99  }
100 
108  virtual void execute( const carto::VolumeRef<T> & in,
109  carto::VolumeRef<T> & out ) const = 0;
110 
111  virtual void setOptions( const carto::Object & options ) {};
112  virtual void updateOptions( const carto::Object & options ) {};
113 
116  virtual ImageAlgorithmInterface *clone() const = 0;
117 
118  //----------------------------------------------------------------------
119  // utility method
120  //----------------------------------------------------------------------
122  virtual Point4dl getOutputImageDimensions( const long & dx = 1,
123  const long & dy = 1,
124  const long & dz = 1,
125  const long & dt = 1 ) const
126  {
127  return getOutputImageDimensions( Point4dl( dx, dy, dz, dt ) );
128  }
129 
131  virtual Point4df getOutputImageVoxelSize( const float & vx = 1.0,
132  const float & vy = 1.0,
133  const float & vz = 1.0,
134  const float & vt = 1.0 ) const
135  {
136  return getOutputImageVoxelSize( Point4df( vx, vy, vz, vt ) );
137  }
138 
140  virtual carto::VolumeRef<T> execute( const carto::VolumeRef<T> & in ) const
141  {
142  Point4dl dims( in.getSizeX(), in.getSizeY(), in.getSizeZ(), in.getSizeT() );
143  std::vector<float> voxelsize(4, 1.);
144 
145  in.header().getProperty( "voxel_size", voxelsize );
146  Point4df voxel( voxelsize[0], voxelsize[1], voxelsize[2], voxelsize[3] );
147  dims = getOutputImageDimensions( dims );
148  voxel = getOutputImageVoxelSize( voxel );
149  voxelsize[0] = voxel[0];
150  voxelsize[1] = voxel[1];
151  voxelsize[2] = voxel[2];
152  voxelsize[3] = voxel[3];
153 
154  carto::VolumeRef<T> out( dims[0], dims[1], dims[2], dims[3] );
155  out->copyHeaderFrom( in.header() );
156  out.header().setProperty( "voxel_size", voxelsize );
157  execute( in, out );
158 
159  return out;
160  }
161 
163  virtual AimsData<T> execute( const AimsData<T> & in ) const
164  {
165  return AimsData<T>( execute( carto::VolumeRef<T>( in.volume() ) ) );
166  }
167 
168  //----------------------------------------------------------------------
169  // verbose
170  //----------------------------------------------------------------------
171  public:
172  virtual void setVerbose( int level ) { _verbose = level; }
173  virtual void setQuiet() { setVerbose(0); }
174  protected:
175  int _verbose;
176 
177  //----------------------------------------------------------------------
178  // polymorphism
179  //----------------------------------------------------------------------
180  public:
182  protected:
185  _verbose(other._verbose)
186  {}
188  {
189  _verbose = other._verbose;
190  }
191  };
192 
193 
194  //==========================================================================
195  // IMAGE ALGORITHM
196  //==========================================================================
206  template <typename T>
207  class ImageAlgorithm: public ImageAlgorithmInterface<T>
208  {
209  public:
210  //----------------------------------------------------------------------
211  // types
212  //----------------------------------------------------------------------
213  typedef T VoxelType;
215 
216  //----------------------------------------------------------------------
217  // interface implementation
218  //----------------------------------------------------------------------
219  virtual void execute( const carto::VolumeRef<T> & in,
220  carto::VolumeRef<T> & out ) const
221  {
223  S::execute( *_algo, in, out, this->_verbose );
224  }
225 
226  virtual Point4dl getOutputImageDimensions( const Point4dl & dims ) const {
227  return _algo->getOutputImageDimensions( dims );
228  }
229 
230  virtual Point4df getOutputImageVoxelSize( const Point4df & voxelsize ) const {
231  return _algo->getOutputImageVoxelSize( voxelsize );
232  }
233 
234  // I need this line to unhide all the "execute" method since the
235  // overloading of one hid all the others.
239 
240  virtual ImageAlgorithm<T> *clone() const {
241  return new ImageAlgorithm<T>( *this );
242  }
243 
244  virtual void setVerbose( int level )
245  {
247  _algo->setVerbose( level );
248  }
249 
250  //----------------------------------------------------------------------
251  // member method (backward compability)
252  //----------------------------------------------------------------------
257  virtual AimsData< T > doit( const AimsData<T>& in )
258  {
260  }
261 
262  //----------------------------------------------------------------------
263  // polymorphism
264  //----------------------------------------------------------------------
265  public:
266  virtual ~ImageAlgorithm() {}
267  protected:
271  _algo(algo.clone())
272  {}
275  _algo( (deepcopy ? algo->clone() : algo ) )
276  {}
277  ImageAlgorithm( const ImageAlgorithm & other ):
278  ImageAlgorithmInterface<T>(other),
279  _algo(other._algo->clone())
280  {}
282  {
283  ASSERT( typeid(other) == typeid(*this) );
284  if( &other != this ) {
286  _algo.reset( other._algo->clone() );
287  }
288  return *this;
289  }
290 
291  //----------------------------------------------------------------------
292  // member
293  //----------------------------------------------------------------------
294  protected:
296  };
297 
298  //==========================================================================
299  // IMAGE ALGORITHM SWITCH
300  //==========================================================================
311  template <typename T, bool M>
312  class ImageAlgorithmSwitch {
313  public:
314  typedef T VoxelType;
316  };
317 
318  //--------------------------------------------------------------------------
319  // specialization: single channel
320  //--------------------------------------------------------------------------
329  template <typename T>
330  class ImageAlgorithmSwitch<T,false> {
331  public:
333 
339  const carto::VolumeRef<T> & in,
340  carto::VolumeRef<T> & out,
341  int verbose = carto::verbose )
342  {
343  return algo.execute( in, out );
344  }
345  };
346 
347  //--------------------------------------------------------------------------
348  // specialization: multi channel
349  //--------------------------------------------------------------------------
358  template <typename T>
359  class ImageAlgorithmSwitch<T,true> {
360  public:
361  typedef typename T::ChannelType ChannelType;
363 
368  static void execute(
370  const carto::VolumeRef<T> & in,
371  carto::VolumeRef<T> & out,
372  int verbose )
373  {
374  ChannelSelector< carto::VolumeRef<T>,
376  > selector;
377 
378  for( uint8_t channel = 0;
379  channel < DataTypeInfo<T>::samples();
380  channel++ )
381  {
382  if( verbose )
383  std::cout << "Processing channel :"
384  << carto::toString(channel)
385  << std::endl;
386 
387  // Split the data and process filter on each component
388  const carto::VolumeRef<ChannelType> & inChannel = selector.select( in, channel );
389  const carto::VolumeRef<ChannelType> & outChannel = algo.execute( inChannel );
390  selector.set( out, channel, outChannel );
391  }
392  }
393  };
394 
395 }
396 
397 #endif
virtual ImageAlgorithmInterface * clone() const =0
ImageAlgorithmInterface Pure virtual method.
int getSizeY() const
virtual Point4df getOutputImageVoxelSize(const float &vx=1.0, const float &vy=1.0, const float &vz=1.0, const float &vt=1.0) const
virtual Point4dl getOutputImageDimensions(const Point4dl &dims) const
Returns the output dimensions of the processed image.
ImageAlgorithmInterface(const ImageAlgorithmInterface &other)
virtual ImageAlgorithm< T > * clone() const
ImageAlgorithmInterface Pure virtual method.
aims::ImageAlgorithmSwitch switches between multichannel and monochannel ImageAlgorithmInterface ...
int getSizeZ() const
virtual Point4df getOutputImageVoxelSize(const Point4df &voxelsize) const
Returns the output voxel size of the processed image.
virtual void setVerbose(int level)
carto::DataTypeTraits< T >::ChannelType ChannelType
static void execute(ImageAlgorithmInterface< T > &algo, const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out, int verbose=carto::verbose)
Execute ImageAlgorithmInterface on VolumeRef.
AlgorithmCaller algo
virtual Point4dl getOutputImageDimensions(const long &dx=1, const long &dy=1, const long &dz=1, const long &dt=1) const
static void execute(ImageAlgorithmInterface< ChannelType > &algo, const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out, int verbose)
Execute ImageAlgorithmInterface on VolumeRef.
int verbose
int getSizeT() const
virtual AimsData< T > execute(const AimsData< T > &in) const
ImageAlgorithm(const ImageAlgorithm &other)
virtual carto::VolumeRef< T > execute(const carto::VolumeRef< T > &in) const
virtual Point4df getOutputImageVoxelSize(const Point4df &voxelsize) const
Returns the output voxel size of the processed image.
virtual AimsData< T > doit(const AimsData< T > &in)
Convenience method that execute the algorithm on the input image.
aims::ImageAlgorithm class used to implement image algorithms
virtual void setOptions(const carto::Object &options)
ImageAlgorithm(const ImageAlgorithmInterface< ChannelType > &algo)
virtual void execute(const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out) const
ImageAlgorithmInterface Pure virtual method.
ImageAlgorithmInterface & operator=(const ImageAlgorithmInterface &other)
virtual void execute(const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out) const =0
ImageAlgorithmInterface Pure virtual method.
virtual Point4dl getOutputImageDimensions(const Point4dl &dims) const
Returns the output dimensions of the processed image.
virtual void setVerbose(int level)
carto::rc_ptr< carto::Volume< T > > volume()
aims::ImageAlgorithmInterface is the interface for an image processing algorithm. ...
ImageAlgorithm(ImageAlgorithmInterface< ChannelType > *algo, bool deepcopy=true)
int getSizeX() const
virtual void copyHeaderFrom(const PropertySet &other)
ImageAlgorithm & operator=(const ImageAlgorithm &other)
const PropertySet & header() const
std::string toString(const T &object)
#define ASSERT(EX)
virtual void updateOptions(const carto::Object &options)
carto::rc_ptr< ImageAlgorithmInterface< ChannelType > > _algo