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
24namespace itk
25{
43namespace Functor
44{
45template <typename TInput, typename TOutput>
46class ITK_TEMPLATE_EXPORT ThresholdLabeler
47{
48public:
50 ~ThresholdLabeler() = default;
54 using RealThresholdVector = std::vector<RealThresholdType>;
55
57 void
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
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
106private:
109};
110} // namespace Functor
111
112template <typename TInputImage, typename TOutputImage>
113class ITK_TEMPLATE_EXPORT ThresholdLabelerImageFilter
115 TInputImage,
116 TOutputImage,
117 Functor::ThresholdLabeler<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
118{
119public:
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
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
205 itkGetConstMacro(LabelOffset, OutputPixelType);
208protected:
210 ~ThresholdLabelerImageFilter() override = default;
211 void
212 PrintSelf(std::ostream & os, Indent indent) const override;
213
216 void
218
219private:
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_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ThresholdLabeler)
void SetThresholds(const RealThresholdVector &thresholds)
bool operator==(const ThresholdLabeler &other) const
std::vector< RealThresholdType > RealThresholdVector
void SetLabelOffset(const TOutput &labelOffset)
TOutput operator()(const TInput &A) const
typename NumericTraits< TInput >::RealType RealThresholdType
Base class for all process objects that output image data.
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Define additional traits for native types such as int or float.
static constexpr T max(const T &)
The base class for all process objects (source, filters, mappers) in the Insight data processing pipe...
Label an input image according to a set of thresholds.
std::vector< InputPixelType > ThresholdVector
void BeforeThreadedGenerateData() override
typename NumericTraits< InputPixelType >::RealType RealThresholdType
void PrintSelf(std::ostream &os, Indent indent) const override
void SetThresholds(const ThresholdVector &thresholds)
std::vector< RealThresholdType > RealThresholdVector
const RealThresholdVector & GetRealThresholds() const
const ThresholdVector & GetThresholds() const
void SetRealThresholds(const RealThresholdVector &thresholds)
~ThresholdLabelerImageFilter() override=default
typename TOutputImage::PixelType OutputPixelType
typename TInputImage::PixelType InputPixelType
Implements pixel-wise generic operation on one image.
#define itkConceptMacro(name, concept)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....