ITK  5.2.0
Insight Toolkit
itkRescaleIntensityImageFilter.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 itkRescaleIntensityImageFilter_h
19 #define itkRescaleIntensityImageFilter_h
20 
22 #include "itkMath.h"
23 
24 namespace itk
25 {
26 // This functor class applies a linear transformation A.x + B
27 // to input values.
28 namespace Functor
29 {
30 template <typename TInput, typename TOutput>
31 class ITK_TEMPLATE_EXPORT IntensityLinearTransform
32 {
33 public:
36  {
37  m_Factor = 1.0;
38  m_Offset = 0.0;
40  m_Maximum = NumericTraits<TOutput>::max();
41 #if defined(__GNUC__) && (__GNUC__ == 5) && (__GNUC_MINOR__ == 2) && defined(NDEBUG) && defined(__i386__)
42  m_EpsilonCompensation = static_cast<RealType>(std::numeric_limits<TOutput>::epsilon());
43  if (m_EpsilonCompensation == 0)
44  {
45  m_EpsilonCompensation = std::numeric_limits<RealType>::epsilon();
46  }
47 #endif
48  }
49 
50  ~IntensityLinearTransform() = default;
51  void
53  {
54  m_Factor = a;
55  }
56  void
58  {
59  m_Offset = b;
60  }
61  void
62  SetMinimum(TOutput min)
63  {
64  m_Minimum = min;
65  }
66  void
67  SetMaximum(TOutput max)
68  {
69  m_Maximum = max;
70  }
71  bool
72  operator!=(const IntensityLinearTransform & other) const
73  {
74  if (Math::NotExactlyEquals(m_Factor, other.m_Factor) || Math::NotExactlyEquals(m_Offset, other.m_Offset) ||
75  Math::NotExactlyEquals(m_Maximum, other.m_Maximum) || Math::NotExactlyEquals(m_Minimum, other.m_Minimum))
76  {
77  return true;
78  }
79  return false;
80  }
81 
82  bool
83  operator==(const IntensityLinearTransform & other) const
84  {
85  return !(*this != other);
86  }
87 
88  inline TOutput
89  operator()(const TInput & x) const
90  {
91 #if defined(__GNUC__) && (__GNUC__ == 5) && (__GNUC_MINOR__ == 2) && defined(NDEBUG) && defined(__i386__)
92  RealType value = static_cast<RealType>(x) * m_Factor + m_Offset + m_EpsilonCompensation;
93  TOutput result = static_cast<TOutput>(value) - static_cast<TOutput>(m_EpsilonCompensation);
94 #else
95  RealType value = static_cast<RealType>(x) * m_Factor + m_Offset;
96  auto result = static_cast<TOutput>(value);
97 #endif
98  result = (result > m_Maximum) ? m_Maximum : result;
99  result = (result < m_Minimum) ? m_Minimum : result;
100  return result;
101  }
102 
103 private:
106  TOutput m_Maximum;
107  TOutput m_Minimum;
108 #if defined(__GNUC__) && (__GNUC__ == 5) && (__GNUC_MINOR__ == 2) && defined(NDEBUG) && defined(__i386__)
109  RealType m_EpsilonCompensation;
110 #endif
111 };
112 } // end namespace Functor
113 
153 template <typename TInputImage, typename TOutputImage = TInputImage>
154 class ITK_TEMPLATE_EXPORT RescaleIntensityImageFilter
155  : public UnaryFunctorImageFilter<
156  TInputImage,
157  TOutputImage,
158  Functor::IntensityLinearTransform<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
159 {
160 public:
161  ITK_DISALLOW_COPY_AND_MOVE(RescaleIntensityImageFilter);
163 
167  TInputImage,
168  TOutputImage,
170 
173 
174  using OutputPixelType = typename TOutputImage::PixelType;
175  using InputPixelType = typename TInputImage::PixelType;
177 
179  itkNewMacro(Self);
180 
183 
184  itkSetMacro(OutputMinimum, OutputPixelType);
185  itkSetMacro(OutputMaximum, OutputPixelType);
186  itkGetConstReferenceMacro(OutputMinimum, OutputPixelType);
187  itkGetConstReferenceMacro(OutputMaximum, OutputPixelType);
188 
192  itkGetConstReferenceMacro(Scale, RealType);
193  itkGetConstReferenceMacro(Shift, RealType);
195 
198  itkGetConstReferenceMacro(InputMinimum, InputPixelType);
199  itkGetConstReferenceMacro(InputMaximum, InputPixelType);
201 
203  void
204  BeforeThreadedGenerateData() override;
205 
207  void
208  PrintSelf(std::ostream & os, Indent indent) const override;
209 
210 #ifdef ITK_USE_CONCEPT_CHECKING
211  // Begin concept checking
212  itkConceptMacro(InputHasNumericTraitsCheck, (Concept::HasNumericTraits<InputPixelType>));
213  itkConceptMacro(OutputHasNumericTraitsCheck, (Concept::HasNumericTraits<OutputPixelType>));
214  itkConceptMacro(RealTypeMultiplyOperatorCheck, (Concept::MultiplyOperator<RealType>));
215  itkConceptMacro(RealTypeAdditiveOperatorsCheck, (Concept::AdditiveOperators<RealType>));
216  // End concept checking
217 #endif
218 
219 protected:
221  ~RescaleIntensityImageFilter() override = default;
222 
223 private:
226 
229 
232 };
233 } // end namespace itk
234 
235 #ifndef ITK_MANUAL_INSTANTIATION
236 # include "itkRescaleIntensityImageFilter.hxx"
237 #endif
238 
239 #endif
itk::RescaleIntensityImageFilter::m_Scale
RealType m_Scale
Definition: itkRescaleIntensityImageFilter.h:224
itk::Functor::IntensityLinearTransform::m_Factor
RealType m_Factor
Definition: itkRescaleIntensityImageFilter.h:104
itk::RescaleIntensityImageFilter::m_OutputMaximum
OutputPixelType m_OutputMaximum
Definition: itkRescaleIntensityImageFilter.h:231
itkUnaryFunctorImageFilter.h
itk::Concept::HasNumericTraits
Definition: itkConceptChecking.h:712
itk::RescaleIntensityImageFilter::RealType
typename NumericTraits< InputPixelType >::RealType RealType
Definition: itkRescaleIntensityImageFilter.h:176
itk::Functor::IntensityLinearTransform::operator==
bool operator==(const IntensityLinearTransform &other) const
Definition: itkRescaleIntensityImageFilter.h:83
itk::UnaryFunctorImageFilter
Implements pixel-wise generic operation on one image.
Definition: itkUnaryFunctorImageFilter.h:50
itk::NumericTraits::NonpositiveMin
static constexpr T NonpositiveMin()
Definition: itkNumericTraits.h:97
itk::SmartPointer< Self >
itk::Functor::IntensityLinearTransform::SetMinimum
void SetMinimum(TOutput min)
Definition: itkRescaleIntensityImageFilter.h:62
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Math::NotExactlyEquals
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:733
itk::Functor::IntensityLinearTransform::m_Offset
RealType m_Offset
Definition: itkRescaleIntensityImageFilter.h:105
itk::RescaleIntensityImageFilter::m_InputMaximum
InputPixelType m_InputMaximum
Definition: itkRescaleIntensityImageFilter.h:228
itk::ImageSource
Base class for all process objects that output image data.
Definition: itkImageSource.h:67
itk::RescaleIntensityImageFilter::m_Shift
RealType m_Shift
Definition: itkRescaleIntensityImageFilter.h:225
itk::Functor::IntensityLinearTransform::SetFactor
void SetFactor(RealType a)
Definition: itkRescaleIntensityImageFilter.h:52
itk::RescaleIntensityImageFilter::m_InputMinimum
InputPixelType m_InputMinimum
Definition: itkRescaleIntensityImageFilter.h:227
itk::RescaleIntensityImageFilter::OutputPixelType
typename TOutputImage::PixelType OutputPixelType
Definition: itkRescaleIntensityImageFilter.h:174
itk::Functor::IntensityLinearTransform::IntensityLinearTransform
IntensityLinearTransform()
Definition: itkRescaleIntensityImageFilter.h:35
itk::Functor::IntensityLinearTransform::SetOffset
void SetOffset(RealType b)
Definition: itkRescaleIntensityImageFilter.h:57
itk::Functor::IntensityLinearTransform::operator!=
bool operator!=(const IntensityLinearTransform &other) const
Definition: itkRescaleIntensityImageFilter.h:72
itk::Functor::IntensityLinearTransform::operator()
TOutput operator()(const TInput &x) const
Definition: itkRescaleIntensityImageFilter.h:89
itk::Concept::MultiplyOperator
Definition: itkConceptChecking.h:417
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:167
itk::Concept::AdditiveOperators
Definition: itkConceptChecking.h:356
itk::Functor::IntensityLinearTransform
Definition: itkRescaleIntensityImageFilter.h:31
itkConceptMacro
#define itkConceptMacro(name, concept)
Definition: itkConceptChecking.h:64
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:154
itk::Functor::IntensityLinearTransform::SetMaximum
void SetMaximum(TOutput max)
Definition: itkRescaleIntensityImageFilter.h:67
itk::Functor::IntensityLinearTransform::m_Maximum
TOutput m_Maximum
Definition: itkRescaleIntensityImageFilter.h:106
itk::Functor::IntensityLinearTransform< TInputImage::PixelType, TOutputImage::PixelType >::RealType
typename NumericTraits< TInputImage::PixelType >::RealType RealType
Definition: itkRescaleIntensityImageFilter.h:34
itk::RescaleIntensityImageFilter::m_OutputMinimum
OutputPixelType m_OutputMinimum
Definition: itkRescaleIntensityImageFilter.h:230
itk::RescaleIntensityImageFilter::InputPixelType
typename TInputImage::PixelType InputPixelType
Definition: itkRescaleIntensityImageFilter.h:175
itk::Functor::IntensityLinearTransform::m_Minimum
TOutput m_Minimum
Definition: itkRescaleIntensityImageFilter.h:107
itkMath.h