ITK  6.0.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  * https://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 {
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 
66 
67  bool
68  operator==(const Sigmoid & other) const
69  {
73  }
74 
76 
77  inline TOutput
78  operator()(const TInput & A) const
79  {
80  const double x = (static_cast<double>(A) - m_Beta) / m_Alpha;
81  const double e = 1.0 / (1.0 + std::exp(-x));
82  const double v = (m_OutputMaximum - m_OutputMinimum) * e + m_OutputMinimum;
83 
84  return static_cast<TOutput>(v);
85  }
86 
87  void
88  SetAlpha(double alpha)
89  {
90  m_Alpha = alpha;
91  }
92 
93  void
94  SetBeta(double beta)
95  {
96  m_Beta = beta;
97  }
98 
99  double
100  GetAlpha() const
101  {
102  return m_Alpha;
103  }
104 
105  double
106  GetBeta() const
107  {
108  return m_Beta;
109  }
110 
111  void
112  SetOutputMinimum(TOutput min)
113  {
114  m_OutputMinimum = min;
115  }
116 
117  void
118  SetOutputMaximum(TOutput max)
119  {
120  m_OutputMaximum = max;
121  }
122 
123  TOutput
125  {
126  return m_OutputMinimum;
127  }
128 
129  TOutput
131  {
132  return m_OutputMaximum;
133  }
134 
135 private:
136  double m_Alpha;
137  double m_Beta;
140 };
141 } // namespace Functor
142 
143 template <typename TInputImage, typename TOutputImage>
145  : public UnaryFunctorImageFilter<TInputImage,
146  TOutputImage,
147  Functor::Sigmoid<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
148 {
149 public:
150  ITK_DISALLOW_COPY_AND_MOVE(SigmoidImageFilter);
151 
154  using Superclass =
155  UnaryFunctorImageFilter<TInputImage,
156  TOutputImage,
158 
161 
162  using OutputPixelType = typename TOutputImage::PixelType;
163 
165  itkNewMacro(Self);
166 
168  itkOverrideGetNameOfClassMacro(SigmoidImageFilter);
169 
170  void
171  SetAlpha(double alpha)
172  {
173  if (Math::ExactlyEquals(alpha, this->GetFunctor().GetAlpha()))
174  {
175  return;
176  }
177  this->GetFunctor().SetAlpha(alpha);
178  this->Modified();
179  }
180 
181  double
182  GetAlpha() const
183  {
184  return this->GetFunctor().GetAlpha();
185  }
186 
187  void
188  SetBeta(double beta)
189  {
190  if (Math::ExactlyEquals(beta, this->GetFunctor().GetBeta()))
191  {
192  return;
193  }
194  this->GetFunctor().SetBeta(beta);
195  this->Modified();
196  }
197 
198  double
199  GetBeta() const
200  {
201  return this->GetFunctor().GetBeta();
202  }
203 
204  void
206  {
207  if (Math::ExactlyEquals(min, this->GetFunctor().GetOutputMinimum()))
208  {
209  return;
210  }
211  this->GetFunctor().SetOutputMinimum(min);
212  this->Modified();
213  }
214 
217  {
218  return this->GetFunctor().GetOutputMinimum();
219  }
220 
221  void
223  {
224  if (Math::ExactlyEquals(max, this->GetFunctor().GetOutputMaximum()))
225  {
226  return;
227  }
228  this->GetFunctor().SetOutputMaximum(max);
229  this->Modified();
230  }
231 
234  {
235  return this->GetFunctor().GetOutputMaximum();
236  }
237 
238 #ifdef ITK_USE_CONCEPT_CHECKING
239  // Begin concept checking
241  itkConceptMacro(OutputAdditiveOperatorsCheck, (Concept::AdditiveOperators<OutputPixelType>));
242  itkConceptMacro(DoubleConvertibleToOutputCheck, (Concept::Convertible<double, OutputPixelType>));
244  itkConceptMacro(OutputDoubleAdditiveOperatorsCheck,
246  // End concept checking
247 #endif
248 
249 protected:
250  SigmoidImageFilter() = default;
251  ~SigmoidImageFilter() override = default;
252 };
253 } // end namespace itk
254 
255 #endif
itk::SigmoidImageFilter::GetOutputMaximum
OutputPixelType GetOutputMaximum() const
Definition: itkSigmoidImageFilter.h:233
itkUnaryFunctorImageFilter.h
itk::SigmoidImageFilter::OutputPixelType
typename TOutputImage::PixelType OutputPixelType
Definition: itkSigmoidImageFilter.h:162
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:205
itk::SigmoidImageFilter::SetBeta
void SetBeta(double beta)
Definition: itkSigmoidImageFilter.h:188
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:726
itk::NumericTraits::min
static constexpr T min(const T &)
Definition: itkNumericTraits.h:174
itk::Functor::Sigmoid::ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Sigmoid)
itk::SmartPointer< Self >
itk::SigmoidImageFilter
Computes the sigmoid function pixel-wise.
Definition: itkSigmoidImageFilter.h:144
itk::SigmoidImageFilter::SetOutputMaximum
void SetOutputMaximum(OutputPixelType max)
Definition: itkSigmoidImageFilter.h:222
itk::Functor::Sigmoid::GetOutputMinimum
TOutput GetOutputMinimum() const
Definition: itkSigmoidImageFilter.h:124
itk::Functor::Sigmoid::GetOutputMaximum
TOutput GetOutputMaximum() const
Definition: itkSigmoidImageFilter.h:130
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:216
itk::SigmoidImageFilter::SigmoidImageFilter
SigmoidImageFilter()=default
itk::Functor::Sigmoid::m_OutputMinimum
TOutput m_OutputMinimum
Definition: itkSigmoidImageFilter.h:138
itk::Functor::Sigmoid::m_OutputMaximum
TOutput m_OutputMaximum
Definition: itkSigmoidImageFilter.h:139
itk::Functor::Sigmoid::operator==
bool operator==(const Sigmoid &other) const
Definition: itkSigmoidImageFilter.h:68
itk::Functor::Sigmoid::GetBeta
double GetBeta() const
Definition: itkSigmoidImageFilter.h:106
itk::SigmoidImageFilter::GetAlpha
double GetAlpha() const
Definition: itkSigmoidImageFilter.h:182
itk::Functor::Sigmoid
Definition: itkSigmoidImageFilter.h:53
itk::Functor::Sigmoid::SetOutputMaximum
void SetOutputMaximum(TOutput max)
Definition: itkSigmoidImageFilter.h:118
itk::Concept::MultiplyOperator
Definition: itkConceptChecking.h:421
itk::SigmoidImageFilter::SetAlpha
void SetAlpha(double alpha)
Definition: itkSigmoidImageFilter.h:171
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:169
itk::SigmoidImageFilter::~SigmoidImageFilter
~SigmoidImageFilter() override=default
itk::Concept::AdditiveOperators
Definition: itkConceptChecking.h:360
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:112
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:139
itk::Concept::Convertible
Definition: itkConceptChecking.h:216
itk::Functor::Sigmoid::GetAlpha
double GetAlpha() const
Definition: itkSigmoidImageFilter.h:100
itk::Functor::Sigmoid::SetAlpha
void SetAlpha(double alpha)
Definition: itkSigmoidImageFilter.h:88
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:56
itk::Functor::Sigmoid::m_Alpha
double m_Alpha
Definition: itkSigmoidImageFilter.h:136
itk::Functor::Sigmoid::SetBeta
void SetBeta(double beta)
Definition: itkSigmoidImageFilter.h:94
itk::Functor::Sigmoid::m_Beta
double m_Beta
Definition: itkSigmoidImageFilter.h:137
itk::SigmoidImageFilter::GetBeta
double GetBeta() const
Definition: itkSigmoidImageFilter.h:199
itk::Object::Modified
virtual void Modified() const
itkMath.h
itk::Functor::Sigmoid::operator()
TOutput operator()(const TInput &A) const
Definition: itkSigmoidImageFilter.h:78
itk::Functor::Sigmoid::~Sigmoid
~Sigmoid()=default