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 : m_LabelOffset(NumericTraits<TOutput>::OneValue())
51 {}
52 ~ThresholdLabeler() = default;
53
55 using RealThresholdVector = std::vector<RealThresholdType>;
56
58 void
60 {
61 m_Thresholds = thresholds;
62 }
63
65 void
66 SetLabelOffset(const TOutput & labelOffset)
67 {
68 m_LabelOffset = labelOffset;
69 }
70
71
72 bool
73 operator==(const ThresholdLabeler & other) const
74 {
75 return m_Thresholds == other.m_Thresholds && m_LabelOffset == other.m_LabelOffset;
76 }
77
79
80 inline TOutput
81 operator()(const TInput & A) const
82 {
83 // When there are N thresholds, they divide values into N+1 buckets, which we number
84 // 0, ..., N. Each bucket represents a half-open interval of values (A, B]. The
85 // variables low, mid, and high refer to buckets. The inclusive range [low, high]
86 // are the buckets that are not yet ruled out. We repeatedly bisect this range
87 // using the variable `mid`. In the case of ties, this method returns the lowest
88 // bucket index for which `A` is less than or equal to the bucket's upper limit.
89 size_t low = 0;
90 size_t high = m_Thresholds.size();
91 while (low < high)
92 {
93 const size_t mid = (low + high) / 2;
94 if (A <= m_Thresholds[mid])
95 {
96 high = mid;
97 }
98 else
99 {
100 low = mid + 1;
101 }
102 }
103 // The computed bucket index is relative to m_LabelOffset.
104 return static_cast<TOutput>(low) + m_LabelOffset;
105 }
106
107private:
110};
111} // namespace Functor
112
113template <typename TInputImage, typename TOutputImage>
114class ITK_TEMPLATE_EXPORT ThresholdLabelerImageFilter
116 TInputImage,
117 TOutputImage,
118 Functor::ThresholdLabeler<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
119{
120public:
121 ITK_DISALLOW_COPY_AND_MOVE(ThresholdLabelerImageFilter);
122
126 TInputImage,
127 TOutputImage,
129
132
134 itkNewMacro(Self);
135
137 itkOverrideGetNameOfClassMacro(ThresholdLabelerImageFilter);
138
140 using InputPixelType = typename TInputImage::PixelType;
141 using OutputPixelType = typename TOutputImage::PixelType;
142
144 using ThresholdVector = std::vector<InputPixelType>;
146 using RealThresholdVector = std::vector<RealThresholdType>;
147
151 itkConceptMacro(OutputPixelTypeComparable, (Concept::Comparable<OutputPixelType>));
152 itkConceptMacro(OutputPixelTypeOStreamWritable, (Concept::OStreamWritable<OutputPixelType>));
155 void
156 SetThresholds(const ThresholdVector & thresholds)
157 {
158 m_Thresholds = thresholds;
159 m_RealThresholds.clear();
160 auto 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 }
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
192 const RealThresholdVector &
194 {
195 return m_RealThresholds;
196 }
197
201 itkGetConstMacro(LabelOffset, OutputPixelType);
203protected:
205 ~ThresholdLabelerImageFilter() override = default;
206 void
207 PrintSelf(std::ostream & os, Indent indent) const override;
208
211 void
213
214private:
218};
219} // end namespace itk
220
221#ifndef ITK_MANUAL_INSTANTIATION
222# include "itkThresholdLabelerImageFilter.hxx"
223#endif
224
225#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
Define additional traits for native types such as int or float.
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....