A.I.M.S algorithms


filteringfunction_element_d.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 
35 #ifndef AIMS_SIGNALFILTER_FILTERINGFUNCTION_NONLINEAR_D_H
36 #define AIMS_SIGNALFILTER_FILTERINGFUNCTION_NONLINEAR_D_H
37 
38 //--- aims -------------------------------------------------------------------
40 #include <aims/connectivity/structuring_element.h> // aims::StructuringElement
41 #include <aims/math/mathelem.h> // absdiff + all iterator methods
42 //--- cartodata --------------------------------------------------------------
44 #include <cartodata/volume/volume.h> // carto::VolumeRef
45 //--- cartobase --------------------------------------------------------------
46 #include <cartobase/config/verbose.h> // carto::verbose
47 #include <cartobase/smart/rcptr.h> // carto::rc_ptr
48 //--- std --------------------------------------------------------------------
49 #include <cmath>
50 #include <map>
51 #include <set>
52 #include <string>
53 #include <algorithm> // std::max_element
54 #include <limits> // std::numeric_limits
55 //----------------------------------------------------------------------------
56 
57 namespace aims {
58 
59 //============================================================================
60 // ELEMENT FILTERING FUNCTIONS: FACTORY
61 //============================================================================
62 
63  template <typename T>
65  {
66  static bool initialized = false;
67  if( !initialized )
68  {
69  initialized = true;
70  registerFunction( "min", MinFilterFunc<T>() );
71  registerFunction( "max", MaxFilterFunc<T>() );
72  registerFunction( "med", MedianFilterFunc<T>() );
73  registerFunction( "median", MedianFilterFunc<T>() );
74  registerFunction( "notnullmedian", NotNullMedianFilterFunc<T>() );
75  registerFunction( "mea", MeanFilterFunc<T>() );
76  registerFunction( "mean", MeanFilterFunc<T>() );
77  registerFunction( "notnullmean", NotNullMeanFilterFunc<T>() );
78  registerFunction( "maj", MajorityFilterFunc<T>() );
79  registerFunction( "majority", MajorityFilterFunc<T>() );
80  registerFunction( "dif", ExtremaDifferenceFilterFunc<T>() );
81  registerFunction( "difference", ExtremaDifferenceFilterFunc<T>() );
82  registerFunction( "sum", SumFilterFunc<T>() );
83  registerFunction( "var", VarFilterFunc<T>() );
84  registerFunction( "variance", VarFilterFunc<T>() );
85  registerFunction( "sd", StDevFilterFunc<T>() );
86  }
87  }
88 
89  template <typename T>
90  std::map<std::string,carto::rc_ptr<ElementFilteringFunction<T> > > &
92  {
93  static std::map<std::string,carto::rc_ptr<ElementFilteringFunction<T> > > m;
94  return m;
95  }
96 
97  template <typename T>
99  const std::string & name,
100  const ElementFilteringFunction<T> & func
101  )
102  {
103  init();
104  _map()[ name ] = carto::rc_ptr<ElementFilteringFunction<T> >( func.clone() );
105  }
106 
107  template <typename T>
109  {
110  init();
111  std::set<std::string> s;
112  typename std::map<std::string,carto::rc_ptr<ElementFilteringFunction<T> > >::const_iterator i, e = _map().end();
113  for( i=_map().begin(); i!=e; ++i )
114  s.insert( i->first );
115  return( s );
116  }
117 
118  template <typename T>
120  const std::string & name,
121  carto::Object options
122  )
123  {
124  init();
125  typename std::map<std::string,carto::rc_ptr<ElementFilteringFunction<T> > >::const_iterator i;
126  i = _map().find( name );
127  if( i == _map().end() )
128  return( 0 );
129  ElementFilteringFunction<T> * new_func = i->second->clone();
130  new_func->setOptions( options );
131  return new_func;
132  }
133 
134 //============================================================================
135 // ELEMENT FILTERING FUNCTIONS: DERIVED CLASSES
136 //============================================================================
137  //--------------------------------------------------------------------------
138  // MaxFilterFunc
139  //--------------------------------------------------------------------------
140  template <class T> inline
141  T MaxFilterFunc<T>::execute( const carto::VolumeRef<T> & volume ) const
142  {
143  return carto::VolumeUtil<T>::max(volume);
144  }
145 
146  template <class T> inline
147  T MaxFilterFunc<T>::execute(
148  const carto::VolumeRef<T> & volume,
150  ) const
151  {
152  StructuredConstVolume<T> volse( *volume, *se );
153  return MathUtil<T>::max( volse.begin(), volse.end() );
154  }
155 
156  //--------------------------------------------------------------------------
157  // MinFilterFunc
158  //--------------------------------------------------------------------------
159  template <class T> inline
160  T MinFilterFunc<T>::execute( const carto::VolumeRef<T>& volume ) const
161  {
162  return carto::VolumeUtil<T>::min(volume);
163  }
164 
165  template <class T> inline
166  T MinFilterFunc<T>::execute(
167  const carto::VolumeRef<T> & volume,
169  ) const
170  {
171  StructuredConstVolume<T> volse( *volume, *se );
172  return MathUtil<T>::min( volse.begin(), volse.end() );
173  }
174 
175  //--------------------------------------------------------------------------
176  // MedianFilterFunc
177  //--------------------------------------------------------------------------
178  template <class T> inline
179  T MedianFilterFunc<T>::execute( const carto::VolumeRef<T> & volume ) const
180  {
181  return MathUtil<T>::median( volume.begin(), volume.end() );
182  }
183 
184  template <class T> inline
185  T MedianFilterFunc<T>::execute(
186  const carto::VolumeRef<T> & volume,
188  ) const
189  {
190  StructuredConstVolume<T> volse( *volume, *se );
191  return MathUtil<T>::median( volse.begin(), volse.end() );
192  }
193 
194  //--------------------------------------------------------------------------
195  // NotNullMedianFilterFunc
196  //--------------------------------------------------------------------------
197  template <class T> inline
198  T NotNullMedianFilterFunc<T>::execute( const carto::VolumeRef<T>& volume ) const
199  {
200  return MathUtil<T>::notnull_median( volume.begin(), volume.end() );
201  }
202 
203  template <class T> inline
204  T NotNullMedianFilterFunc<T>::execute(
205  const carto::VolumeRef<T> & volume,
207  ) const
208  {
209  StructuredConstVolume<T> volse( *volume, *se );
210  return MathUtil<T>::notnull_median( volse.begin(), volse.end() );
211  }
212 
213  //--------------------------------------------------------------------------
214  // MeanFilterFunc
215  //--------------------------------------------------------------------------
216  template <class T> inline
217  T MeanFilterFunc<T>::execute( const carto::VolumeRef<T>& volume ) const
218  {
219  return MathUtil<T>::mean( volume.begin(), volume.end() );
220  }
221 
222  template <class T> inline
223  T MeanFilterFunc<T>::execute(
224  const carto::VolumeRef<T> & volume,
226  ) const
227  {
228  StructuredConstVolume<T> volse( *volume, *se );
229  return MathUtil<T>::mean( volse.begin(), volse.end() );
230  }
231 
232  //--------------------------------------------------------------------------
233  // NotNullMeanFilterFunc
234  //--------------------------------------------------------------------------
235  template <class T> inline
236  T NotNullMeanFilterFunc<T>::execute( const carto::VolumeRef<T>& volume ) const
237  {
238  return MathUtil<T>::notnull_mean( volume.begin(), volume.end() );
239  }
240 
241  template <class T> inline
242  T NotNullMeanFilterFunc<T>::execute(
243  const carto::VolumeRef<T> & volume,
245  ) const
246  {
247  StructuredConstVolume<T> volse( *volume, *se );
248  return MathUtil<T>::notnull_mean( volse.begin(), volse.end() );
249  }
250 
251  //--------------------------------------------------------------------------
252  // MajorityFilterFunc
253  //--------------------------------------------------------------------------
254  template <class T> inline
255  T MajorityFilterFunc<T>::execute( const carto::VolumeRef<T>& volume ) const
256  {
257  return MathUtil<T>::majority( volume.begin(), volume.end() );
258  }
259 
260  template <class T> inline
261  T MajorityFilterFunc<T>::execute(
262  const carto::VolumeRef<T> & volume,
264  ) const
265  {
266  StructuredConstVolume<T> volse( *volume, *se );
267  return MathUtil<T>::majority( volse.begin(), volse.end() );
268  }
269 
270  //--------------------------------------------------------------------------
271  // ExtremaDifferenceFilterFunc
272  //--------------------------------------------------------------------------
273  template <class T> inline
274  T ExtremaDifferenceFilterFunc<T>::execute( const carto::VolumeRef<T>& volume ) const
275  {
276  return MathUtil<T>::extrema_difference( volume.begin(), volume.end() );
277  }
278 
279  template <class T> inline
280  T ExtremaDifferenceFilterFunc<T>::execute(
281  const carto::VolumeRef<T> & volume,
283  ) const
284  {
285  StructuredConstVolume<T> volse( *volume, *se );
286  return MathUtil<T>::extrema_difference( volse.begin(), volse.end() );
287  }
288 
289  //--------------------------------------------------------------------------
290  // SumFilterFunc
291  //--------------------------------------------------------------------------
292  template <class T> inline
293  T SumFilterFunc<T>::execute( const carto::VolumeRef<T>& volume ) const
294  {
295  return MathUtil<T>::sum( volume.begin(), volume.end() );
296  }
297 
298  template <class T> inline
299  T SumFilterFunc<T>::execute(
300  const carto::VolumeRef<T> & volume,
302  ) const
303  {
304  StructuredConstVolume<T> volse( *volume, *se );
305  return MathUtil<T>::sum( volse.begin(), volse.end() );
306  }
307 
308  //--------------------------------------------------------------------------
309  // VarFilterFunc
310  //--------------------------------------------------------------------------
311  template <class T> inline
312  T VarFilterFunc<T>::execute( const carto::VolumeRef<T>& volume ) const
313  {
314  return MathUtil<T>::variance( volume.begin(), volume.end() );
315  }
316 
317  template <class T> inline
318  T VarFilterFunc<T>::execute(
319  const carto::VolumeRef<T> & volume,
321  ) const
322  {
323  StructuredConstVolume<T> volse( *volume, *se );
324  return MathUtil<T>::variance( volse.begin(), volse.end() );
325  }
326 
327  //--------------------------------------------------------------------------
328  // StDevFilterFunc
329  //--------------------------------------------------------------------------
330  template <class T> inline
331  T StDevFilterFunc<T>::execute( const carto::VolumeRef<T>& volume ) const
332  {
333  return MathUtil<T>::stdev( volume.begin(), volume.end() );
334  }
335 
336  template <class T> inline
337  T StDevFilterFunc<T>::execute(
338  const carto::VolumeRef<T> & volume,
340  ) const
341  {
342  StructuredConstVolume<T> volse( *volume, *se );
343  return MathUtil<T>::stdev( volse.begin(), volse.end() );
344  }
345 
346 } // namespace aims
347 
348 #endif
float min(float x, float y)
Definition: thickness.h:105
float max(float x, float y)
Definition: thickness.h:97
virtual void setOptions(const carto::Object &options)
Set the parameters of the filters If a parameter value is not set in the options object, a default value must be assigned.
static T majority(Iterator b, Iterator e, T default_value=(T) 0)
static T notnull_median(Iterator b, Iterator e, T default_value=(T) 0)
static T min(Iterator b, Iterator e, T default_value=std::numeric_limits< T >::max())
static std::map< std::string, carto::rc_ptr< ElementFilteringFunction< T > > > & _map()
static T extrema_difference(Iterator b, Iterator e)
Base class for filtering functions applied in a structuring element.
static T mean(Iterator b, Iterator e)
static std::set< std::string > names()
List all function ids registered in the factory.
static T max(Iterator b, Iterator e, T default_value=std::numeric_limits< T >::min())
static T sum(Iterator b, Iterator e)
iterator end()
static T stdev(Iterator b, Iterator e)
static T notnull_mean(Iterator b, Iterator e)
iterator begin()
static T variance(Iterator b, Iterator e)
static ElementFilteringFunction< T > * create(const std::string &name, carto::Object options=carto::none())
Build and return the ElementFilteringFunction associated with the id name.
static T median(Iterator b, Iterator e, T default_value=(T) 0)
static void registerFunction(const std::string &name, const ElementFilteringFunction< T > &func)
Register a ElementFilterigFunction into the factory.
virtual ElementFilteringFunction< T > * clone() const =0
clone