ITK  5.4.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  * 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 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;
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  // When there are N thresholds, they divide values into N+1 buckets, which we number
83  // 0, ..., N. Each bucket represents a half-open interval of values (A, B]. The
84  // variables low, mid, and high refer to buckets. The inclusive range [low, high]
85  // are the buckets that are not yet ruled out. We repeatedly bisect this range
86  // using the variable `mid`. In the case of ties, this method returns the lowest
87  // bucket index for which `A` is less than or equal to the bucket's upper limit.
88  size_t low = 0;
89  size_t high = m_Thresholds.size();
90  while (low < high)
91  {
92  const size_t mid = (low + high) / 2;
93  if (A <= m_Thresholds[mid])
94  {
95  high = mid;
96  }
97  else
98  {
99  low = mid + 1;
100  }
101  }
102  // The computed bucket index is relative to m_LabelOffset.
103  return static_cast<TOutput>(low) + m_LabelOffset;
104  }
105 
106 private:
108  TOutput m_LabelOffset;
109 };
110 } // namespace Functor
111 
112 template <typename TInputImage, typename TOutputImage>
113 class ITK_TEMPLATE_EXPORT ThresholdLabelerImageFilter
114  : public UnaryFunctorImageFilter<
115  TInputImage,
116  TOutputImage,
117  Functor::ThresholdLabeler<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
118 {
119 public:
120  ITK_DISALLOW_COPY_AND_MOVE(ThresholdLabelerImageFilter);
121 
125  TInputImage,
126  TOutputImage,
128 
131 
133  itkNewMacro(Self);
134 
136  itkOverrideGetNameOfClassMacro(ThresholdLabelerImageFilter);
137 
139  using InputPixelType = typename TInputImage::PixelType;
140  using OutputPixelType = typename TOutputImage::PixelType;
141 
143  using ThresholdVector = std::vector<InputPixelType>;
145  using RealThresholdVector = std::vector<RealThresholdType>;
146 
148 #ifdef ITK_USE_CONCEPT_CHECKING
149  // Begin concept checking
151  itkConceptMacro(OutputPixelTypeComparable, (Concept::Comparable<OutputPixelType>));
152  itkConceptMacro(OutputPixelTypeOStreamWritable, (Concept::OStreamWritable<OutputPixelType>));
153  // End concept checking
154 #endif
155 
158  void
159  SetThresholds(const ThresholdVector & thresholds)
160  {
161  m_Thresholds = thresholds;
162  m_RealThresholds.clear();
163  typename ThresholdVector::const_iterator itr = m_Thresholds.begin();
164  while (itr != m_Thresholds.end())
165  {
166  m_RealThresholds.push_back(static_cast<RealThresholdType>(*itr));
167  ++itr;
168  }
169  this->Modified();
170  }
174  const ThresholdVector &
176  {
177  return m_Thresholds;
178  }
179 
181  void
183  {
184  m_RealThresholds = thresholds;
185  m_Thresholds.clear();
186  typename RealThresholdVector::const_iterator itr = m_RealThresholds.begin();
187  while (itr != m_RealThresholds.end())
188  {
189  m_Thresholds.push_back(static_cast<InputPixelType>(*itr));
190  ++itr;
191  }
192  this->Modified();
193  }
197  const RealThresholdVector &
199  {
200  return m_RealThresholds;
201  }
202 
204  itkSetClampMacro(LabelOffset, OutputPixelType, OutputPixelType{}, NumericTraits<OutputPixelType>::max());
205  itkGetConstMacro(LabelOffset, OutputPixelType);
208 protected:
209  ThresholdLabelerImageFilter();
210  ~ThresholdLabelerImageFilter() override = default;
211  void
212  PrintSelf(std::ostream & os, Indent indent) const override;
213 
216  void
217  BeforeThreadedGenerateData() override;
218 
219 private:
220  ThresholdVector m_Thresholds{};
221  RealThresholdVector m_RealThresholds{};
222  OutputPixelType m_LabelOffset{};
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:107
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:140
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:175
itkConceptChecking.h
itk::ThresholdLabelerImageFilter::SetThresholds
void SetThresholds(const ThresholdVector &thresholds)
Definition: itkThresholdLabelerImageFilter.h:159
itk::SmartPointer< Self >
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:145
itk::Functor::ThresholdLabeler::m_LabelOffset
TOutput m_LabelOffset
Definition: itkThresholdLabelerImageFilter.h:108
itk::NumericTraits::OneValue
static T OneValue()
Definition: itkNumericTraits.h:157
itk::ThresholdLabelerImageFilter::SetRealThresholds
void SetRealThresholds(const RealThresholdVector &thresholds)
Definition: itkThresholdLabelerImageFilter.h:182
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:139
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:59
itk::NumericTraits::max
static constexpr T max(const T &)
Definition: itkNumericTraits.h:168
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:139
itk::ThresholdLabelerImageFilter::RealThresholdType
typename NumericTraits< InputPixelType >::RealType RealThresholdType
Definition: itkThresholdLabelerImageFilter.h:144
itk::ThresholdLabelerImageFilter::GetRealThresholds
const RealThresholdVector & GetRealThresholds() const
Definition: itkThresholdLabelerImageFilter.h:198
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:143
itk::ThresholdLabelerImageFilter
Label an input image according to a set of thresholds.
Definition: itkThresholdLabelerImageFilter.h:113