ITK  5.3.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 
71  bool
72  operator==(const ThresholdLabeler & other) const
73  {
74  return m_Thresholds == other.m_Thresholds && m_LabelOffset == other.m_LabelOffset;
75  }
76 
77  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ThresholdLabeler);
78 
79  inline TOutput
80  operator()(const TInput & A) const
81  {
82  size_t size = m_Thresholds.size();
83 
84  if (size == 0)
85  {
86  return m_LabelOffset;
87  }
88  if (A <= m_Thresholds[0])
89  {
90  return m_LabelOffset;
91  }
92  for (size_t i = 0; i < size - 1; ++i)
93  {
94  /* Value is in this class if it equals the upper bound. */
95  if (m_Thresholds[i] < A && A <= m_Thresholds[i + 1])
96  {
97  return static_cast<TOutput>(i + 1) + m_LabelOffset;
98  }
99  }
100  return static_cast<TOutput>(size) + m_LabelOffset;
101  }
102 
103 private:
105  TOutput m_LabelOffset;
106 };
107 } // namespace Functor
108 
109 template <typename TInputImage, typename TOutputImage>
110 class ITK_TEMPLATE_EXPORT ThresholdLabelerImageFilter
111  : public UnaryFunctorImageFilter<
112  TInputImage,
113  TOutputImage,
114  Functor::ThresholdLabeler<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
115 {
116 public:
117  ITK_DISALLOW_COPY_AND_MOVE(ThresholdLabelerImageFilter);
118 
122  TInputImage,
123  TOutputImage,
125 
128 
130  itkNewMacro(Self);
131 
134 
136  using InputPixelType = typename TInputImage::PixelType;
137  using OutputPixelType = typename TOutputImage::PixelType;
138 
140  using ThresholdVector = std::vector<InputPixelType>;
142  using RealThresholdVector = std::vector<RealThresholdType>;
143 
145 #ifdef ITK_USE_CONCEPT_CHECKING
146  // Begin concept checking
148  itkConceptMacro(OutputPixelTypeComparable, (Concept::Comparable<OutputPixelType>));
149  itkConceptMacro(OutputPixelTypeOStreamWritable, (Concept::OStreamWritable<OutputPixelType>));
150  // End concept checking
151 #endif
152 
153 
155  void
156  SetThresholds(const ThresholdVector & thresholds)
157  {
158  m_Thresholds = thresholds;
159  m_RealThresholds.clear();
160  typename ThresholdVector::const_iterator itr = m_Thresholds.begin();
161  while (itr != m_Thresholds.end())
162  {
163  m_RealThresholds.push_back(static_cast<RealThresholdType>(*itr));
164  ++itr;
165  }
166  this->Modified();
167  }
169 
171  const ThresholdVector &
173  {
174  return m_Thresholds;
175  }
176 
178  void
180  {
181  m_RealThresholds = thresholds;
182  m_Thresholds.clear();
183  typename RealThresholdVector::const_iterator itr = m_RealThresholds.begin();
184  while (itr != m_RealThresholds.end())
185  {
186  m_Thresholds.push_back(static_cast<InputPixelType>(*itr));
187  ++itr;
188  }
189  this->Modified();
190  }
192 
194  const RealThresholdVector &
196  {
197  return m_RealThresholds;
198  }
199 
201  itkSetClampMacro(LabelOffset,
202  OutputPixelType,
205  itkGetConstMacro(LabelOffset, OutputPixelType);
207 
208 protected:
210  ~ThresholdLabelerImageFilter() override = default;
211  void
212  PrintSelf(std::ostream & os, Indent indent) const override;
213 
216  void
217  BeforeThreadedGenerateData() override;
218 
219 private:
223 };
224 } // end namespace itk
225 
226 #ifndef ITK_MANUAL_INSTANTIATION
227 # include "itkThresholdLabelerImageFilter.hxx"
228 #endif
229 
230 #endif
itk::Functor::ThresholdLabeler::m_Thresholds
RealThresholdVector m_Thresholds
Definition: itkThresholdLabelerImageFilter.h:104
itk::Functor::ThresholdLabeler
Definition: itkThresholdLabelerImageFilter.h:46
itk::Functor::ThresholdLabeler::operator==
bool operator==(const ThresholdLabeler &other) const
Definition: itkThresholdLabelerImageFilter.h:72
itkUnaryFunctorImageFilter.h
itk::Concept::OStreamWritable
Definition: itkConceptChecking.h:636
itk::ThresholdLabelerImageFilter::OutputPixelType
typename TOutputImage::PixelType OutputPixelType
Definition: itkThresholdLabelerImageFilter.h:137
itk::Functor::ThresholdLabeler::operator()
TOutput operator()(const TInput &A) const
Definition: itkThresholdLabelerImageFilter.h:80
itk::UnaryFunctorImageFilter
Implements pixel-wise generic operation on one image.
Definition: itkUnaryFunctorImageFilter.h:50
itk::ThresholdLabelerImageFilter::GetThresholds
const ThresholdVector & GetThresholds() const
Definition: itkThresholdLabelerImageFilter.h:172
itk::ThresholdLabelerImageFilter::m_RealThresholds
RealThresholdVector m_RealThresholds
Definition: itkThresholdLabelerImageFilter.h:221
itkConceptChecking.h
itk::ThresholdLabelerImageFilter::SetThresholds
void SetThresholds(const ThresholdVector &thresholds)
Definition: itkThresholdLabelerImageFilter.h:156
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ThresholdLabelerImageFilter::m_Thresholds
ThresholdVector m_Thresholds
Definition: itkThresholdLabelerImageFilter.h:220
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:142
itk::Functor::ThresholdLabeler::m_LabelOffset
TOutput m_LabelOffset
Definition: itkThresholdLabelerImageFilter.h:105
itk::NumericTraits::OneValue
static T OneValue()
Definition: itkNumericTraits.h:156
itk::ThresholdLabelerImageFilter::SetRealThresholds
void SetRealThresholds(const RealThresholdVector &thresholds)
Definition: itkThresholdLabelerImageFilter.h:179
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:136
itk::ThresholdLabelerImageFilter::m_LabelOffset
OutputPixelType m_LabelOffset
Definition: itkThresholdLabelerImageFilter.h:222
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::ThresholdLabelerImageFilter::RealThresholdType
typename NumericTraits< InputPixelType >::RealType RealThresholdType
Definition: itkThresholdLabelerImageFilter.h:141
itk::ThresholdLabelerImageFilter::GetRealThresholds
const RealThresholdVector & GetRealThresholds() const
Definition: itkThresholdLabelerImageFilter.h:195
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:140
itk::ThresholdLabelerImageFilter
Label an input image according to a set of thresholds.
Definition: itkThresholdLabelerImageFilter.h:110