ITK  5.2.0
Insight Toolkit
itkSigmoidImageFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkSigmoidImageFilter_h
19 #define itkSigmoidImageFilter_h
20 
22 #include "itkMath.h"
23 
24 namespace itk
25 {
49 
50 namespace Functor
51 {
52 template <typename TInput, typename TOutput>
53 class Sigmoid
54 {
55 public:
57  {
58  m_Alpha = 1.0;
59  m_Beta = 0.0;
62  }
63 
64  ~Sigmoid() = default;
65  bool
66  operator!=(const Sigmoid & other) const
67  {
71  {
72  return true;
73  }
74  return false;
75  }
76 
77  bool
78  operator==(const Sigmoid & other) const
79  {
80  return !(*this != other);
81  }
82 
83  inline TOutput
84  operator()(const TInput & A) const
85  {
86  const double x = (static_cast<double>(A) - m_Beta) / m_Alpha;
87  const double e = 1.0 / (1.0 + std::exp(-x));
88  const double v = (m_OutputMaximum - m_OutputMinimum) * e + m_OutputMinimum;
89 
90  return static_cast<TOutput>(v);
91  }
92 
93  void
94  SetAlpha(double alpha)
95  {
96  m_Alpha = alpha;
97  }
98 
99  void
100  SetBeta(double beta)
101  {
102  m_Beta = beta;
103  }
104 
105  double
106  GetAlpha() const
107  {
108  return m_Alpha;
109  }
110 
111  double
112  GetBeta() const
113  {
114  return m_Beta;
115  }
116 
117  void
118  SetOutputMinimum(TOutput min)
119  {
120  m_OutputMinimum = min;
121  }
122 
123  void
124  SetOutputMaximum(TOutput max)
125  {
126  m_OutputMaximum = max;
127  }
128 
129  TOutput
131  {
132  return m_OutputMinimum;
133  }
134 
135  TOutput
137  {
138  return m_OutputMaximum;
139  }
140 
141 private:
142  double m_Alpha;
143  double m_Beta;
146 };
147 } // namespace Functor
148 
149 template <typename TInputImage, typename TOutputImage>
151  : public UnaryFunctorImageFilter<TInputImage,
152  TOutputImage,
153  Functor::Sigmoid<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
154 {
155 public:
156  ITK_DISALLOW_COPY_AND_MOVE(SigmoidImageFilter);
157 
160  using Superclass =
161  UnaryFunctorImageFilter<TInputImage,
162  TOutputImage,
164 
167 
168  using OutputPixelType = typename TOutputImage::PixelType;
169 
171  itkNewMacro(Self);
172 
175 
176  void
177  SetAlpha(double alpha)
178  {
179  if (Math::ExactlyEquals(alpha, this->GetFunctor().GetAlpha()))
180  {
181  return;
182  }
183  this->GetFunctor().SetAlpha(alpha);
184  this->Modified();
185  }
186 
187  double
188  GetAlpha() const
189  {
190  return this->GetFunctor().GetAlpha();
191  }
192 
193  void
194  SetBeta(double beta)
195  {
196  if (Math::ExactlyEquals(beta, this->GetFunctor().GetBeta()))
197  {
198  return;
199  }
200  this->GetFunctor().SetBeta(beta);
201  this->Modified();
202  }
203 
204  double
205  GetBeta() const
206  {
207  return this->GetFunctor().GetBeta();
208  }
209 
210  void
212  {
213  if (Math::ExactlyEquals(min, this->GetFunctor().GetOutputMinimum()))
214  {
215  return;
216  }
217  this->GetFunctor().SetOutputMinimum(min);
218  this->Modified();
219  }
220 
223  {
224  return this->GetFunctor().GetOutputMinimum();
225  }
226 
227  void
229  {
230  if (Math::ExactlyEquals(max, this->GetFunctor().GetOutputMaximum()))
231  {
232  return;
233  }
234  this->GetFunctor().SetOutputMaximum(max);
235  this->Modified();
236  }
237 
240  {
241  return this->GetFunctor().GetOutputMaximum();
242  }
243 
244 #ifdef ITK_USE_CONCEPT_CHECKING
245  // Begin concept checking
247  itkConceptMacro(OutputAdditiveOperatorsCheck, (Concept::AdditiveOperators<OutputPixelType>));
248  itkConceptMacro(DoubleConvertibleToOutputCheck, (Concept::Convertible<double, OutputPixelType>));
250  itkConceptMacro(OutputDoubleAdditiveOperatorsCheck,
252  // End concept checking
253 #endif
254 
255 protected:
256  SigmoidImageFilter() = default;
257  ~SigmoidImageFilter() override = default;
258 };
259 } // end namespace itk
260 
261 #endif
itk::SigmoidImageFilter::GetOutputMaximum
OutputPixelType GetOutputMaximum() const
Definition: itkSigmoidImageFilter.h:239
itkUnaryFunctorImageFilter.h
itk::SigmoidImageFilter::OutputPixelType
typename TOutputImage::PixelType OutputPixelType
Definition: itkSigmoidImageFilter.h:168
itk::Functor::Sigmoid::operator!=
bool operator!=(const Sigmoid &other) const
Definition: itkSigmoidImageFilter.h:66
itk::UnaryFunctorImageFilter
Implements pixel-wise generic operation on one image.
Definition: itkUnaryFunctorImageFilter.h:50
itk::Functor::Sigmoid::Sigmoid
Sigmoid()
Definition: itkSigmoidImageFilter.h:56
itk::SigmoidImageFilter::SetOutputMinimum
void SetOutputMinimum(OutputPixelType min)
Definition: itkSigmoidImageFilter.h:211
itk::SigmoidImageFilter::SetBeta
void SetBeta(double beta)
Definition: itkSigmoidImageFilter.h:194
itk::Math::ExactlyEquals
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potentially different types.
Definition: itkMath.h:723
itk::NumericTraits::min
static constexpr T min(const T &)
Definition: itkNumericTraits.h:172
itk::SmartPointer< Self >
itk::Math::NotExactlyEquals
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
itk::SigmoidImageFilter
Computes the sigmoid function pixel-wise.
Definition: itkSigmoidImageFilter.h:150
itk::SigmoidImageFilter::SetOutputMaximum
void SetOutputMaximum(OutputPixelType max)
Definition: itkSigmoidImageFilter.h:228
itk::Functor::Sigmoid::GetOutputMinimum
TOutput GetOutputMinimum() const
Definition: itkSigmoidImageFilter.h:130
itk::Functor::Sigmoid::GetOutputMaximum
TOutput GetOutputMaximum() const
Definition: itkSigmoidImageFilter.h:136
itk::ImageSource
Base class for all process objects that output image data.
Definition: itkImageSource.h:67
itk::SigmoidImageFilter::GetOutputMinimum
OutputPixelType GetOutputMinimum() const
Definition: itkSigmoidImageFilter.h:222
itk::SigmoidImageFilter::SigmoidImageFilter
SigmoidImageFilter()=default
itk::Functor::Sigmoid::m_OutputMinimum
TOutput m_OutputMinimum
Definition: itkSigmoidImageFilter.h:144
itk::Functor::Sigmoid::m_OutputMaximum
TOutput m_OutputMaximum
Definition: itkSigmoidImageFilter.h:145
itk::Functor::Sigmoid::operator==
bool operator==(const Sigmoid &other) const
Definition: itkSigmoidImageFilter.h:78
itk::Functor::Sigmoid::GetBeta
double GetBeta() const
Definition: itkSigmoidImageFilter.h:112
itk::SigmoidImageFilter::GetAlpha
double GetAlpha() const
Definition: itkSigmoidImageFilter.h:188
itk::Functor::Sigmoid
Definition: itkSigmoidImageFilter.h:53
itk::Functor::Sigmoid::SetOutputMaximum
void SetOutputMaximum(TOutput max)
Definition: itkSigmoidImageFilter.h:124
itk::Concept::MultiplyOperator
Definition: itkConceptChecking.h:419
itk::SigmoidImageFilter::SetAlpha
void SetAlpha(double alpha)
Definition: itkSigmoidImageFilter.h:177
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:167
itk::SigmoidImageFilter::~SigmoidImageFilter
~SigmoidImageFilter() override=default
itk::Concept::AdditiveOperators
Definition: itkConceptChecking.h:358
itkConceptMacro
#define itkConceptMacro(name, concept)
Definition: itkConceptChecking.h:65
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Functor::Sigmoid::SetOutputMinimum
void SetOutputMinimum(TOutput min)
Definition: itkSigmoidImageFilter.h:118
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::Concept::Convertible
Definition: itkConceptChecking.h:216
itk::Functor::Sigmoid::GetAlpha
double GetAlpha() const
Definition: itkSigmoidImageFilter.h:106
itk::Functor::Sigmoid::SetAlpha
void SetAlpha(double alpha)
Definition: itkSigmoidImageFilter.h:94
itk::UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::Sigmoid< TInputImage::PixelType, TOutputImage::PixelType > >::GetFunctor
FunctorType & GetFunctor()
Definition: itkUnaryFunctorImageFilter.h:85
itk::Math::e
static constexpr double e
Definition: itkMath.h:54
itk::Functor::Sigmoid::m_Alpha
double m_Alpha
Definition: itkSigmoidImageFilter.h:142
itk::Functor::Sigmoid::SetBeta
void SetBeta(double beta)
Definition: itkSigmoidImageFilter.h:100
itk::Functor::Sigmoid::m_Beta
double m_Beta
Definition: itkSigmoidImageFilter.h:143
itk::SigmoidImageFilter::GetBeta
double GetBeta() const
Definition: itkSigmoidImageFilter.h:205
itk::Object::Modified
virtual void Modified() const
itkMath.h
itk::Functor::Sigmoid::operator()
TOutput operator()(const TInput &A) const
Definition: itkSigmoidImageFilter.h:84
itk::Functor::Sigmoid::~Sigmoid
~Sigmoid()=default