ITK  5.2.0
Insight Toolkit
itkThresholdLabelerImageFilter.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 itkThresholdLabelerImageFilter_h
19 #define itkThresholdLabelerImageFilter_h
20 
22 #include "itkConceptChecking.h"
23 
24 namespace itk
25 {
43 namespace Functor
44 {
45 template <typename TInput, typename TOutput>
46 class ITK_TEMPLATE_EXPORT ThresholdLabeler
47 {
48 public:
50  ~ThresholdLabeler() = default;
52 
54  using RealThresholdVector = std::vector<RealThresholdType>;
55 
57  void
58  SetThresholds(const RealThresholdVector & thresholds)
59  {
60  m_Thresholds = thresholds;
61  }
62 
64  void
65  SetLabelOffset(const TOutput & labelOffset)
66  {
67  m_LabelOffset = labelOffset;
68  }
69 
70  bool
71  operator!=(const ThresholdLabeler & other) const
72  {
73  if (m_Thresholds != other.m_Thresholds || m_LabelOffset != other.m_LabelOffset)
74  {
75  return true;
76  }
77  return false;
78  }
79 
80  bool
81  operator==(const ThresholdLabeler & other) const
82  {
83  return !(*this != other);
84  }
85 
86  inline TOutput
87  operator()(const TInput & A) const
88  {
89  size_t size = m_Thresholds.size();
90 
91  if (size == 0)
92  {
93  return m_LabelOffset;
94  }
95  if (A <= m_Thresholds[0])
96  {
97  return m_LabelOffset;
98  }
99  for (size_t i = 0; i < size - 1; i++)
100  {
101  /* Value is in this class if it equals the upper bound. */
102  if (m_Thresholds[i] < A && A <= m_Thresholds[i + 1])
103  {
104  return static_cast<TOutput>(i + 1) + m_LabelOffset;
105  }
106  }
107  return static_cast<TOutput>(size) + m_LabelOffset;
108  }
109 
110 private:
112  TOutput m_LabelOffset;
113 };
114 } // namespace Functor
115 
116 template <typename TInputImage, typename TOutputImage>
117 class ITK_TEMPLATE_EXPORT ThresholdLabelerImageFilter
118  : public UnaryFunctorImageFilter<
119  TInputImage,
120  TOutputImage,
121  Functor::ThresholdLabeler<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
122 {
123 public:
124  ITK_DISALLOW_COPY_AND_MOVE(ThresholdLabelerImageFilter);
125 
129  TInputImage,
130  TOutputImage,
132 
135 
137  itkNewMacro(Self);
138 
141 
143  using InputPixelType = typename TInputImage::PixelType;
144  using OutputPixelType = typename TOutputImage::PixelType;
145 
147  using ThresholdVector = std::vector<InputPixelType>;
149  using RealThresholdVector = std::vector<RealThresholdType>;
150 
152 #ifdef ITK_USE_CONCEPT_CHECKING
153  // Begin concept checking
155  itkConceptMacro(OutputPixelTypeComparable, (Concept::Comparable<OutputPixelType>));
156  itkConceptMacro(OutputPixelTypeOStreamWritable, (Concept::OStreamWritable<OutputPixelType>));
157  // End concept checking
158 #endif
159 
160 
162  void
163  SetThresholds(const ThresholdVector & thresholds)
164  {
165  m_Thresholds = thresholds;
166  m_RealThresholds.clear();
167  typename ThresholdVector::const_iterator itr = m_Thresholds.begin();
168  while (itr != m_Thresholds.end())
169  {
170  m_RealThresholds.push_back(static_cast<RealThresholdType>(*itr));
171  ++itr;
172  }
173  this->Modified();
174  }
176 
178  const ThresholdVector &
180  {
181  return m_Thresholds;
182  }
183 
185  void
187  {
188  m_RealThresholds = thresholds;
189  m_Thresholds.clear();
190  typename RealThresholdVector::const_iterator itr = m_RealThresholds.begin();
191  while (itr != m_RealThresholds.end())
192  {
193  m_Thresholds.push_back(static_cast<InputPixelType>(*itr));
194  ++itr;
195  }
196  this->Modified();
197  }
199 
201  const RealThresholdVector &
203  {
204  return m_RealThresholds;
205  }
206 
208  itkSetClampMacro(LabelOffset,
209  OutputPixelType,
212  itkGetConstMacro(LabelOffset, OutputPixelType);
214 
215 protected:
217  ~ThresholdLabelerImageFilter() override = default;
218  void
219  PrintSelf(std::ostream & os, Indent indent) const override;
220 
223  void
224  BeforeThreadedGenerateData() override;
225 
226 private:
230 };
231 } // end namespace itk
232 
233 #ifndef ITK_MANUAL_INSTANTIATION
234 # include "itkThresholdLabelerImageFilter.hxx"
235 #endif
236 
237 #endif
itk::Functor::ThresholdLabeler::m_Thresholds
RealThresholdVector m_Thresholds
Definition: itkThresholdLabelerImageFilter.h:111
itk::Functor::ThresholdLabeler
Definition: itkThresholdLabelerImageFilter.h:46
itk::Functor::ThresholdLabeler::operator==
bool operator==(const ThresholdLabeler &other) const
Definition: itkThresholdLabelerImageFilter.h:81
itkUnaryFunctorImageFilter.h
itk::Concept::OStreamWritable
Definition: itkConceptChecking.h:636
itk::ThresholdLabelerImageFilter::OutputPixelType
typename TOutputImage::PixelType OutputPixelType
Definition: itkThresholdLabelerImageFilter.h:144
itk::Functor::ThresholdLabeler::operator()
TOutput operator()(const TInput &A) const
Definition: itkThresholdLabelerImageFilter.h:87
itk::UnaryFunctorImageFilter
Implements pixel-wise generic operation on one image.
Definition: itkUnaryFunctorImageFilter.h:50
itk::ThresholdLabelerImageFilter::GetThresholds
const ThresholdVector & GetThresholds() const
Definition: itkThresholdLabelerImageFilter.h:179
itk::ThresholdLabelerImageFilter::m_RealThresholds
RealThresholdVector m_RealThresholds
Definition: itkThresholdLabelerImageFilter.h:228
itkConceptChecking.h
itk::ThresholdLabelerImageFilter::SetThresholds
void SetThresholds(const ThresholdVector &thresholds)
Definition: itkThresholdLabelerImageFilter.h:163
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ThresholdLabelerImageFilter::m_Thresholds
ThresholdVector m_Thresholds
Definition: itkThresholdLabelerImageFilter.h:227
itk::ImageSource
Base class for all process objects that output image data.
Definition: itkImageSource.h:67
itk::ThresholdLabelerImageFilter::RealThresholdVector
std::vector< RealThresholdType > RealThresholdVector
Definition: itkThresholdLabelerImageFilter.h:149
itk::Functor::ThresholdLabeler::m_LabelOffset
TOutput m_LabelOffset
Definition: itkThresholdLabelerImageFilter.h:112
itk::NumericTraits::OneValue
static T OneValue()
Definition: itkNumericTraits.h:156
itk::ThresholdLabelerImageFilter::SetRealThresholds
void SetRealThresholds(const RealThresholdVector &thresholds)
Definition: itkThresholdLabelerImageFilter.h:186
itk::Functor::ThresholdLabeler::SetThresholds
void SetThresholds(const RealThresholdVector &thresholds)
Definition: itkThresholdLabelerImageFilter.h:58
itk::Functor::ThresholdLabeler< TInputImage::PixelType, TOutputImage::PixelType >::RealThresholdType
typename NumericTraits< TInputImage::PixelType >::RealType RealThresholdType
Definition: itkThresholdLabelerImageFilter.h:53
itk::ThresholdLabelerImageFilter::InputPixelType
typename TInputImage::PixelType InputPixelType
Definition: itkThresholdLabelerImageFilter.h:143
itk::ThresholdLabelerImageFilter::m_LabelOffset
OutputPixelType m_LabelOffset
Definition: itkThresholdLabelerImageFilter.h:229
itk::Functor::ThresholdLabeler< TInputImage::PixelType, TOutputImage::PixelType >::RealThresholdVector
std::vector< RealThresholdType > RealThresholdVector
Definition: itkThresholdLabelerImageFilter.h:54
itk::Concept::Comparable
Definition: itkConceptChecking.h:330
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
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::ProcessObject
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Definition: itkProcessObject.h:138
itk::Functor::ThresholdLabeler::operator!=
bool operator!=(const ThresholdLabeler &other) const
Definition: itkThresholdLabelerImageFilter.h:71
itk::ThresholdLabelerImageFilter::RealThresholdType
typename NumericTraits< InputPixelType >::RealType RealThresholdType
Definition: itkThresholdLabelerImageFilter.h:148
itk::ThresholdLabelerImageFilter::GetRealThresholds
const RealThresholdVector & GetRealThresholds() const
Definition: itkThresholdLabelerImageFilter.h:202
itk::Functor::ThresholdLabeler::ThresholdLabeler
ThresholdLabeler()
Definition: itkThresholdLabelerImageFilter.h:49
itk::Functor::ThresholdLabeler::SetLabelOffset
void SetLabelOffset(const TOutput &labelOffset)
Definition: itkThresholdLabelerImageFilter.h:65
itk::ThresholdLabelerImageFilter::ThresholdVector
std::vector< InputPixelType > ThresholdVector
Definition: itkThresholdLabelerImageFilter.h:147
itk::ThresholdLabelerImageFilter
Label an input image according to a set of thresholds.
Definition: itkThresholdLabelerImageFilter.h:117