aimsalgo  5.1.2
Neuroimaging image processing
filteringimagealgorithm.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_SIGNALFILTER_FILTERINGIMAGEALGORITHM_H
35 #define AIMSALGO_SIGNALFILTER_FILTERINGIMAGEALGORITHM_H
36 
37 //--- aims -------------------------------------------------------------------
39 #include <aims/utility/progress.h>
41 //--- cartodata --------------------------------------------------------------
43 //--- cartobase --------------------------------------------------------------
45 #include <cartobase/type/types.h>
49 //--- std --------------------------------------------------------------------
50 #include <iostream>
51 #include <assert.h>
52 //----------------------------------------------------------------------------
53 
54 namespace aims {
55  template <typename T> class FilteringImageAlgorithmInterface;
56  template <typename T> class LinearFilteringImageAlgorithm;
57  template <typename T> class LinearFilteringFunction;
58  template <typename T> class ElementFilteringImageAlgorithm;
59  template <typename T> class ElementFilteringFunction;
60  namespace singlechannel {
61  template <typename T> class LinearFilteringImageAlgorithm;
62  template <typename T> class ElementFilteringImageAlgorithm;
63  }
64 
65  //==========================================================================
66  // LINEAR FILTERING IMAGE ALGORITHM: MULTI CHANNELS
67  //==========================================================================
68  template <typename T>
70  {
71  public:
72  //----------------------------------------------------------------------
73  // typedef
74  //----------------------------------------------------------------------
75  typedef T VoxelType;
78 
79  //----------------------------------------------------------------------
80  // image algorithm interface: implementation
81  //----------------------------------------------------------------------
82  virtual void setOptions( const carto::Object & options )
83  {
84  _lalgo->setOptions( options );
85  }
86 
87  virtual void updateOptions( const carto::Object & options )
88  {
89  _lalgo->updateOptions( options );
90  }
91 
93  {
94  return new LinearFilteringImageAlgorithm<T>(*this);
95  }
96 
97  //----------------------------------------------------------------------
98  // constructor (default+copy) & destructor
99  //----------------------------------------------------------------------
100  protected:
103  ImageAlgorithm<T>(),
105  {
107  }
109  ImageAlgorithm<T>(),
110  _lalgo( other._lalgo->clone() )
111  {
113  }
115  {
116  _lalgo = other._lalgo->clone();
118  return *this;
119  }
120  public:
122 
123  //----------------------------------------------------------------------
124  // member
125  //----------------------------------------------------------------------
126  protected:
128 
129  };
130 
131  //==========================================================================
132  // ELEMENT FILTERING IMAGE ALGORITHM: MULTI CHANNELS
133  //==========================================================================
134  template <typename T>
136  {
137  public:
138  //----------------------------------------------------------------------
139  // typedef
140  //----------------------------------------------------------------------
141  typedef T VoxelType;
144 
145  //----------------------------------------------------------------------
146  // image algorithm interface: implementation
147  //----------------------------------------------------------------------
148  virtual void setOptions( const carto::Object & options )
149  {
150  _ealgo->setOptions( options );
151  }
152 
153  virtual void updateOptions( const carto::Object & options )
154  {
155  _ealgo->updateOptions( options );
156  }
157 
159  return new ElementFilteringImageAlgorithm<T>(*this);
160  }
161 
162  //----------------------------------------------------------------------
163  // member method
164  //----------------------------------------------------------------------
165  virtual void setStructuringElement( const StructuringElement & se )
166  {
168  }
169 
170  //----------------------------------------------------------------------
171  // constructor (default+copy) & destructor
172  //----------------------------------------------------------------------
173  protected:
176  const StructuringElement & se = strel::Cube(1.)
177  ):
178  ImageAlgorithm<T>(),
180  {
182  }
184  ImageAlgorithm<T>(),
185  _ealgo(other._ealgo->clone())
186  {
188  }
190  {
191  _ealgo = other._ealgo->clone();
193  return *this;
194  }
195  public:
197 
198  //----------------------------------------------------------------------
199  // member
200  //----------------------------------------------------------------------
201  protected:
203  };
204 
205  namespace singlechannel {
206 
207  //========================================================================
208  // LINEAR FILTERING IMAGE ALGORITHM: SINGLE CHANNEL
209  //========================================================================
210  template <typename T>
212  {
213  public:
214  //--------------------------------------------------------------------
215  // image algorithm interface: implementation
216  //--------------------------------------------------------------------
217  virtual void execute( const carto::VolumeRef<T> & in,
218  carto::VolumeRef<T> & out ) const;
219 
220  virtual void setOptions( const carto::Object & options )
221  {
222  _func->setOptions( options );
223  }
224 
225  virtual void updateOptions( const carto::Object & options )
226  {
227  _func->updateOptions( options );
228  }
229 
230  virtual ::aims::singlechannel::LinearFilteringImageAlgorithm<T> * clone() const {
231  return new ::aims::singlechannel::LinearFilteringImageAlgorithm<T>(*this);
232  }
233 
234  //--------------------------------------------------------------------
235  // constructor (default+copy) & destructor
236  //--------------------------------------------------------------------
237  protected:
239 
240  public:
243  _func(f.clone())
244  {}
245 
246  LinearFilteringImageAlgorithm( const ::aims::singlechannel::LinearFilteringImageAlgorithm<T> & other ):
248  _func(other._func->clone())
249  {}
250 
252  const LinearFilteringImageAlgorithm & other )
253  {
254  assert( typeid(other) == typeid(*this) );
255  if( this != &other )
256  _func.reset( other._func->clone() );
257  return *this;
258  }
259 
261 
262  //--------------------------------------------------------------------
263  // member
264  //--------------------------------------------------------------------
265  protected:
267  };
268 
269  template <typename T>
270  inline
272  const carto::VolumeRef<T> & in,
273  carto::VolumeRef<T> & out ) const
274  {
275  int sx, sy, sz, ex, ey, ez, x, y, z, t;
276  typename carto::Volume<T>::Position4Di pos(0,0,0,0), size(1,1,1,1);
277 
278  // set voxel size
280  std::vector<float> vs(4, 1.);
281  in.header().getProperty( "voxel_size", vs );
282  options->setProperty( "voxel_size", vs );
283  _func->updateOptions( options );
284 
285  // Manage borders / structuring element size
286  const std::vector<int> & border = in->getBorders();
287  const std::vector<int> & amplitude = _func->getAmplitude();
288 
289  // When volume has borders, it is possible to use it to process filtering
290  sz = (amplitude[4] < border[4]) ? 0 : amplitude[4] - border[4];
291  sy = (amplitude[2] < border[2]) ? 0 : amplitude[2] - border[2];
292  sx = (amplitude[0] < border[0]) ? 0 : amplitude[0] - border[0];
293  ez = (amplitude[5] < border[5]) ? in->getSizeZ() : in->getSizeZ() - amplitude[5] + border[5];
294  ey = (amplitude[3] < border[3]) ? in->getSizeY() : in->getSizeY() - amplitude[3] + border[3];
295  ex = (amplitude[1] < border[1]) ? in->getSizeX() : in->getSizeX() - amplitude[1] + border[1];
296 
298  std::cout << "Filter amplitude (voxels): [ ";
299  for(int i = 0; i < 6; ++i)
300  std::cout << carto::toString(amplitude[i]) << ( i==5 ? " " : ", ");
301  std::cout << "]" << std::endl;
302  std::cout << "Processing with borders (voxels): [ ";
303  for(int i = 0; i < 8; ++i)
304  std::cout << carto::toString(border[i]) << ( i==7 ? " " : ", ");
305  std::cout << "]" << std::endl;
306  std::cout << "Start: [" << carto::toString(sx) << ", "
307  << carto::toString(sy) << ", "
308  << carto::toString(sz) << "]" << std::endl;
309  std::cout << "End: [" << carto::toString(ex) << ", "
310  << carto::toString(ey) << ", "
311  << carto::toString(ez) << "]" << std::endl;
312  }
313 
314  size[0] = amplitude[0] + amplitude[1] + 1;
315  size[1] = amplitude[2] + amplitude[3] + 1;
316  size[2] = amplitude[4] + amplitude[5] + 1;
317  carto::VolumeRef<T> win( new carto::Volume<T>( in, pos, size ) );
318 
319  aims::Progression progress( static_cast<size_t>(out->getSizeT())
320  * (ez - sz) * (ey - sy) * (ex - sx) );
322  std::cout << "Filtering progress: ";
323 
324  for( t = 0; t < in->getSizeT(); ++t )
325  for( z = sz; z < ez; ++z )
326  for( y = sy; y < ey; ++y )
327  for( x = sx; x < ex; ++x )
328  {
329  if( this->_verbose > 0 )
330  (++progress).print();
331  // Set view position in the volume
332  pos[0] = x - amplitude[0];
333  pos[1] = y - amplitude[2];
334  pos[2] = z - amplitude[4];
335  pos[3] = t;
336  win->setPosInRefVolume( pos );
337  (*out)( x, y, z, t ) = _func->execute( win );
338  }
339 
341  std::cout << std::endl;
342  }
343 
344 
345  //========================================================================
346  // ELEMENT FILTERING IMAGE ALGORITHM: SINGLE CHANNEL
347  //========================================================================
348  template <typename T>
350  {
351  public:
352  //--------------------------------------------------------------------
353  // image algorithm interface: implementation
354  //--------------------------------------------------------------------
355  virtual void execute( const carto::VolumeRef<T> & in,
356  carto::VolumeRef<T> & out ) const;
357 
358  virtual void setOptions( const carto::Object & options )
359  {
360  _func->setOptions( options );
361  }
362 
363  virtual void updateOptions( const carto::Object & options )
364  {
365  _func->updateOptions( options );
366  }
367 
368  virtual ::aims::singlechannel::ElementFilteringImageAlgorithm<T> * clone() const {
369  return new ::aims::singlechannel::ElementFilteringImageAlgorithm<T>(*this);
370  }
371 
372  //--------------------------------------------------------------------
373  // member method
374  //--------------------------------------------------------------------
375  virtual void setStructuringElement( const StructuringElement & se )
376  {
377  _strel.reset( se.clone() );
378  }
379 
380  //--------------------------------------------------------------------
381  // constructor (default+copy) & destructor
382  //--------------------------------------------------------------------
383  protected:
385 
386  public:
388  const StructuringElement & se = strel::Cube(1.) ):
390  _func(f.clone()),
391  _strel(se.clone())
392  {}
393 
394  ElementFilteringImageAlgorithm( const ::aims::singlechannel::ElementFilteringImageAlgorithm<T> & other ):
396  _func(other._func->clone()),
397  _strel(other._strel->clone())
398  {}
399 
401  const ::aims::singlechannel::ElementFilteringImageAlgorithm<T> & other )
402  {
403  assert( typeid(other) == typeid(*this) );
404  if( *this != other )
405  {
406  _func.reset( other._func->clone() );
407  _strel.reset( other._strel->clone() );
408  }
409  return *this;
410  }
411 
413 
414  //--------------------------------------------------------------------
415  // members
416  //--------------------------------------------------------------------
417  protected:
420  };
421 
422  template <typename T>
423  inline
425  const carto::VolumeRef<T> & in,
426  carto::VolumeRef<T> & out
427  ) const
428  {
429  int sx, sy, sz, ex, ey, ez, x, y, z, t;
430  typename carto::Volume<T>::Position4Di pos(0,0,0), size(1,1,1);
431 
432  // Manage borders / structuring element size
433  const std::vector<int> & border = in->getBorders();
434  const std::vector<int> amplitude = _strel->getAmplitude();
435 
436  // When volume has borders, it is possible to use it to process filtering
437  sz = (amplitude[4] < border[4]) ? 0 : amplitude[4] - border[4];
438  sy = (amplitude[2] < border[2]) ? 0 : amplitude[2] - border[2];
439  sx = (amplitude[0] < border[0]) ? 0 : amplitude[0] - border[0];
440  ez = (amplitude[5] < border[5]) ? in->getSizeZ() : in->getSizeZ() - amplitude[5] + border[5];
441  ey = (amplitude[3] < border[3]) ? in->getSizeY() : in->getSizeY() - amplitude[3] + border[3];
442  ex = (amplitude[1] < border[1]) ? in->getSizeX() : in->getSizeX() - amplitude[1] + border[1];
443 
445  std::cout << "Filter amplitude (voxels): [ ";
446  for(int i = 0; i < 6; ++i)
447  std::cout << carto::toString(amplitude[i]) << ( i==8 ? " " : ", ");
448  std::cout << "]" << std::endl;
449  std::cout << "Processing with borders (voxels): [ ";
450  for(int i = 0; i < 8; ++i)
451  std::cout << carto::toString(border[i]) << ( i==8 ? " " : ", ");
452  std::cout << "]" << std::endl;
453  std::cout << "Start: [" << carto::toString(sx) << ", "
454  << carto::toString(sy) << ", "
455  << carto::toString(sz) << "]" << std::endl;
456  std::cout << "End: [" << carto::toString(ex) << ", "
457  << carto::toString(ey) << ", "
458  << carto::toString(ez) << "]" << std::endl;
459  }
460 
461  carto::VolumeRef<T> win( new carto::Volume<T>( in, pos, size ) );
462 
463  aims::Progression progress( static_cast<size_t>(out->getSizeT())
464  * (ez - sz) * (ey - sy) *(ex - sx) );
466  std::cout << "Filtering progress: ";
467 
468  for( t = 0; t < in->getSizeT(); ++t )
469  for( z = sz; z < ez; ++z )
470  for( y = sy; y < ey; ++y )
471  for( x = sx; x < ex; ++x )
472  {
473  if( this->_verbose > 0 )
474  (++progress).print();
475  // Set view position in the volume
476  pos[0] = x;
477  pos[1] = y;
478  pos[2] = z;
479  pos[3] = t;
480  win->setPosInRefVolume( pos );
481  (*out)( x, y, z, t ) = _func->execute( win, _strel );
482  }
483 
485  std::cout << std::endl;
486  }
487 
488  } // namespace singlechannel
489 
490 } // namespace aims
491 
492 #endif
Base class for filtering functions applied in a structuring element.
virtual ElementFilteringImageAlgorithm< T > * clone() const
ImageAlgorithmInterface<T> Pure virtual method.
ElementFilteringImageAlgorithm(const ElementFilteringFunction< ChannelType > &f, const StructuringElement &se=strel::Cube(1.))
carto::DataTypeTraits< T >::ChannelType ChannelType
virtual void setStructuringElement(const StructuringElement &se)
ElementFilteringImageAlgorithm & operator=(ElementFilteringImageAlgorithm &other)
ElementFilteringImageAlgorithm(const ElementFilteringImageAlgorithm< T > &other)
virtual void updateOptions(const carto::Object &options)
virtual void setOptions(const carto::Object &options)
SingleChannelImageAlgorithmType * _ealgo
singlechannel::ElementFilteringImageAlgorithm< ChannelType > SingleChannelImageAlgorithmType
aims::ImageAlgorithmInterface is the interface for an image processing algorithm.
aims::ImageAlgorithm class used to implement image algorithms
Base class for linear filtering functions.
singlechannel::LinearFilteringImageAlgorithm< ChannelType > SingleChannelImageAlgorithmType
LinearFilteringImageAlgorithm(const LinearFilteringImageAlgorithm< T > &other)
SingleChannelImageAlgorithmType * _lalgo
carto::DataTypeTraits< T >::ChannelType ChannelType
virtual void setOptions(const carto::Object &options)
LinearFilteringImageAlgorithm(const LinearFilteringFunction< ChannelType > &f)
virtual void updateOptions(const carto::Object &options)
LinearFilteringImageAlgorithm & operator=(LinearFilteringImageAlgorithm &other)
virtual LinearFilteringImageAlgorithm< T > * clone() const
ImageAlgorithmInterface<T> Pure virtual method.
virtual StructuringElement * clone() const
virtual void execute(const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out) const
ImageAlgorithmInterface<T> Pure virtual method.
::aims::singlechannel::ElementFilteringImageAlgorithm< T > & operator=(const ::aims::singlechannel::ElementFilteringImageAlgorithm< T > &other)
ElementFilteringImageAlgorithm(const ElementFilteringFunction< T > &f, const StructuringElement &se=strel::Cube(1.))
carto::rc_ptr< ElementFilteringFunction< T > > _func
virtual void setStructuringElement(const StructuringElement &se)
ElementFilteringImageAlgorithm(const ::aims::singlechannel::ElementFilteringImageAlgorithm< T > &other)
virtual void setOptions(const carto::Object &options)
virtual ::aims::singlechannel::ElementFilteringImageAlgorithm< T > * clone() const
ImageAlgorithmInterface<T> Pure virtual method.
virtual void updateOptions(const carto::Object &options)
virtual void execute(const carto::VolumeRef< T > &in, carto::VolumeRef< T > &out) const
ImageAlgorithmInterface<T> Pure virtual method.
virtual void updateOptions(const carto::Object &options)
LinearFilteringImageAlgorithm(const LinearFilteringFunction< T > &f)
carto::rc_ptr< LinearFilteringFunction< T > > _func
virtual void setOptions(const carto::Object &options)
virtual ::aims::singlechannel::LinearFilteringImageAlgorithm< T > * clone() const
ImageAlgorithmInterface<T> Pure virtual method.
LinearFilteringImageAlgorithm(const ::aims::singlechannel::LinearFilteringImageAlgorithm< T > &other)
LinearFilteringImageAlgorithm & operator=(const LinearFilteringImageAlgorithm &other)
static Object value()
bool getProperty(const std::string &, T &) const
int getSizeZ() const
std::vector< int > getBorders() const
int getSizeT() const
int getSizeY() const
const PropertySet & header() const
int getSizeX() const
void setPosInRefVolume(const Position4Di &pos)
std::string toString(const T &object)