ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSigmoidImageFilter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkSigmoidImageFilter_h
00019 #define __itkSigmoidImageFilter_h
00020 
00021 #include "itkUnaryFunctorImageFilter.h"
00022 
00023 namespace itk
00024 {
00047 namespace Functor
00048 {
00049 template< class TInput, class TOutput >
00050 class Sigmoid
00051 {
00052 public:
00053   Sigmoid()
00054   {
00055     m_Alpha = 1.0;
00056     m_Beta =  0.0;
00057     m_OutputMinimum = NumericTraits< TOutput >::min();
00058     m_OutputMaximum = NumericTraits< TOutput >::max();
00059   }
00060 
00061   ~Sigmoid() {}
00062   bool operator!=(const Sigmoid & other) const
00063   {
00064     if ( m_Alpha != other.m_Alpha
00065          || m_Beta != other.m_Beta
00066          || m_OutputMaximum != other.m_OutputMaximum
00067          || m_OutputMinimum != other.m_OutputMinimum  )
00068       {
00069       return true;
00070       }
00071     return false;
00072   }
00073 
00074   bool operator==(const Sigmoid & other) const
00075   {
00076     return !( *this != other );
00077   }
00078 
00079   inline TOutput operator()(const TInput & A) const
00080   {
00081     const double x = ( static_cast< double >( A ) - m_Beta ) / m_Alpha;
00082     const double e = 1.0 / ( 1.0 + vcl_exp(-x) );
00083     const double v =
00084       ( m_OutputMaximum - m_OutputMinimum ) * e + m_OutputMinimum;
00085 
00086     return static_cast< TOutput >( v );
00087   }
00088 
00089   void SetAlpha(double alpha)
00090   {
00091     m_Alpha = alpha;
00092   }
00093 
00094   void SetBeta(double beta)
00095   {
00096     m_Beta = beta;
00097   }
00098 
00099   double GetAlpha() const
00100   {
00101     return m_Alpha;
00102   }
00103 
00104   double GetBeta() const
00105   {
00106     return m_Beta;
00107   }
00108 
00109   void SetOutputMinimum(TOutput min)
00110   {
00111     m_OutputMinimum = min;
00112   }
00113 
00114   void SetOutputMaximum(TOutput max)
00115   {
00116     m_OutputMaximum = max;
00117   }
00118 
00119   TOutput GetOutputMinimum() const
00120   {
00121     return m_OutputMinimum;
00122   }
00123 
00124   TOutput GetOutputMaximum() const
00125   {
00126     return m_OutputMaximum;
00127   }
00128 
00129 private:
00130   double  m_Alpha;
00131   double  m_Beta;
00132   TOutput m_OutputMinimum;
00133   TOutput m_OutputMaximum;
00134 };
00135 }
00136 
00137 template< class TInputImage, class TOutputImage >
00138 class ITK_EXPORT SigmoidImageFilter:
00139   public
00140   UnaryFunctorImageFilter< TInputImage, TOutputImage,
00141                            Functor::Sigmoid<
00142                              typename TInputImage::PixelType,
00143                              typename TOutputImage::PixelType >   >
00144 {
00145 public:
00147   typedef SigmoidImageFilter Self;
00148   typedef UnaryFunctorImageFilter<
00149     TInputImage, TOutputImage,
00150     Functor::Sigmoid< typename TInputImage::PixelType,
00151                        typename TOutputImage::PixelType > >  Superclass;
00152 
00153   typedef SmartPointer< Self >       Pointer;
00154   typedef SmartPointer< const Self > ConstPointer;
00155 
00156   typedef typename TOutputImage::PixelType OutputPixelType;
00157 
00159   itkNewMacro(Self);
00160 
00162   itkTypeMacro(SigmoidImageFilter, UnaryFunctorImageFilter);
00163 
00164   void SetAlpha(double alpha)
00165   {
00166     if ( alpha == this->GetFunctor().GetAlpha() )
00167       {
00168       return;
00169       }
00170     this->GetFunctor().SetAlpha(alpha);
00171     this->Modified();
00172   }
00173 
00174   void SetBeta(double beta)
00175   {
00176     if ( beta == this->GetFunctor().GetBeta() )
00177       {
00178       return;
00179       }
00180     this->GetFunctor().SetBeta(beta);
00181     this->Modified();
00182   }
00183 
00184   void SetOutputMinimum(OutputPixelType min)
00185   {
00186     if ( min == this->GetFunctor().GetOutputMinimum() )
00187       {
00188       return;
00189       }
00190     this->GetFunctor().SetOutputMinimum(min);
00191     this->Modified();
00192   }
00193 
00194   void SetOutputMaximum(OutputPixelType max)
00195   {
00196     if ( max == this->GetFunctor().GetOutputMaximum() )
00197       {
00198       return;
00199       }
00200     this->GetFunctor().SetOutputMaximum(max);
00201     this->Modified();
00202   }
00203 
00204 #ifdef ITK_USE_CONCEPT_CHECKING
00205 
00206   itkConceptMacro( InputConvertibleToDoubleCheck,
00207                    ( Concept::Convertible< typename TInputImage::PixelType, double > ) );
00208   itkConceptMacro( OutputAdditiveOperatorsCheck,
00209                    ( Concept::AdditiveOperators< OutputPixelType > ) );
00210   itkConceptMacro( DoubleConvertibleToOutputCheck,
00211                    ( Concept::Convertible< double, OutputPixelType > ) );
00212   itkConceptMacro( OutputTimesDoubleCheck,
00213                    ( Concept::MultiplyOperator< OutputPixelType, double > ) );
00214   itkConceptMacro( OutputDoubleAdditiveOperatorsCheck,
00215                    ( Concept::AdditiveOperators< OutputPixelType, OutputPixelType, double > ) );
00216 
00218 #endif
00219 protected:
00220   SigmoidImageFilter() {}
00221   virtual ~SigmoidImageFilter() {}
00222 private:
00223   SigmoidImageFilter(const Self &); //purposely not implemented
00224   void operator=(const Self &);     //purposely not implemented
00225 };
00226 } // end namespace itk
00228 
00229 #endif
00230