ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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;
52
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
149 itkConceptMacro(OutputPixelTypeComparable, (Concept::Comparable<OutputPixelType>));
150 itkConceptMacro(OutputPixelTypeOStreamWritable, (Concept::OStreamWritable<OutputPixelType>));
152
154 void
155 SetThresholds(const ThresholdVector & thresholds)
156 {
157 m_Thresholds = thresholds;
158 m_RealThresholds.clear();
159 auto itr = m_Thresholds.begin();
160 while (itr != m_Thresholds.end())
161 {
162 m_RealThresholds.push_back(static_cast<RealThresholdType>(*itr));
163 ++itr;
164 }
165 this->Modified();
166 }
167
168
170 const ThresholdVector &
172 {
173 return m_Thresholds;
174 }
175
177 void
179 {
180 m_RealThresholds = thresholds;
181 m_Thresholds.clear();
182 auto itr = m_RealThresholds.begin();
183 while (itr != m_RealThresholds.end())
184 {
185 m_Thresholds.push_back(static_cast<InputPixelType>(*itr));
186 ++itr;
187 }
188 this->Modified();
189 }
190
191
193 const RealThresholdVector &
195 {
196 return m_RealThresholds;
197 }
198
201 itkGetConstMacro(LabelOffset, OutputPixelType);
203
204protected:
206 ~ThresholdLabelerImageFilter() override = default;
207 void
208 PrintSelf(std::ostream & os, Indent indent) const override;
209
212 void
214
215private:
219};
220} // end namespace itk
221
222#ifndef ITK_MANUAL_INSTANTIATION
223# include "itkThresholdLabelerImageFilter.hxx"
224#endif
225
226#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
Control indentation during Print() invocation.
Definition itkIndent.h:50
static constexpr T max(const T &)
virtual void Modified() const
Implements transparent reference counting.
void BeforeThreadedGenerateData() override
typename NumericTraits< InputPixelType >::RealType RealThresholdType
UnaryFunctorImageFilter< TInputImage, TOutputImage, Functor::ThresholdLabeler< typename TInputImage::PixelType, typename TOutputImage::PixelType > > Superclass
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
#define itkConceptMacro(name, concept)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....