ITK  5.4.0
Insight Toolkit
itkKdTreeBasedKmeansEstimator.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 itkKdTreeBasedKmeansEstimator_h
19#define itkKdTreeBasedKmeansEstimator_h
20
21#include <vector>
22#include <unordered_map>
23
24#include "itkObject.h"
29
30namespace itk
31{
32namespace Statistics
33{
73template <typename TKdTree>
74class ITK_TEMPLATE_EXPORT KdTreeBasedKmeansEstimator : public Object
75{
76public:
82
84 itkNewMacro(Self);
85
87 itkOverrideGetNameOfClassMacro(KdTreeBasedKmeansEstimator);
88
90 using KdTreeNodeType = typename TKdTree::KdTreeNodeType;
91 using MeasurementType = typename TKdTree::MeasurementType;
92 using MeasurementVectorType = typename TKdTree::MeasurementVectorType;
93 using InstanceIdentifier = typename TKdTree::InstanceIdentifier;
94 using SampleType = typename TKdTree::SampleType;
95 using CentroidType = typename KdTreeNodeType::CentroidType;
96
98 using MeasurementVectorSizeType = unsigned int;
99
103 using InternalParametersType = std::vector<ParameterType>;
105
109
111
114 using MembershipFunctionVectorType = std::vector<MembershipFunctionPointer>;
117
121 GetOutput() const;
122
124 itkSetMacro(Parameters, ParametersType);
125 itkGetConstMacro(Parameters, ParametersType);
129 itkSetMacro(MaximumIteration, int);
130 itkGetConstMacro(MaximumIteration, int);
135 itkSetMacro(CentroidPositionChangesThreshold, double);
136 itkGetConstMacro(CentroidPositionChangesThreshold, double);
137
139 void
140 SetKdTree(TKdTree * tree);
141
142 const TKdTree *
143 GetKdTree() const;
144
146 itkGetConstMacro(MeasurementVectorSize, MeasurementVectorSizeType);
147
148 itkGetConstMacro(CurrentIteration, int);
149 itkGetConstMacro(CentroidPositionChanges, double);
150
155 void
157
158 using ClusterLabelsType = std::unordered_map<InstanceIdentifier, unsigned int>;
159
160 itkSetMacro(UseClusterLabels, bool);
161 itkGetConstMacro(UseClusterLabels, bool);
162 itkBooleanMacro(UseClusterLabels);
163
164protected:
166 ~KdTreeBasedKmeansEstimator() override = default;
167
168 void
169 PrintSelf(std::ostream & os, Indent indent) const override;
170
171 void
172 FillClusterLabels(KdTreeNodeType * node, int closestIndex);
173
180 {
181 public:
182 CandidateVector() = default;
183
185 {
188 int Size;
189 }; // end of struct
190
191 virtual ~CandidateVector() = default;
192
194 int
195 Size() const
196 {
197 return static_cast<int>(m_Candidates.size());
198 }
199
202 void
204 {
205 this->m_MeasurementVectorSize = NumericTraits<ParameterType>::GetLength(centroids[0]);
206 m_Candidates.resize(centroids.size());
207 for (unsigned int i = 0; i < centroids.size(); ++i)
208 {
209 Candidate candidate;
210 candidate.Centroid = centroids[i];
211 NumericTraits<CentroidType>::SetLength(candidate.WeightedCentroid, m_MeasurementVectorSize);
212 candidate.WeightedCentroid.Fill(0.0);
213 candidate.Size = 0;
214 m_Candidates[i] = candidate;
215 }
216 }
220 void
222 {
223 unsigned int i;
224
225 centroids.resize(this->Size());
226 for (i = 0; i < static_cast<unsigned int>(this->Size()); ++i)
227 {
228 centroids[i] = m_Candidates[i].Centroid;
229 }
230 }
231
234 void
236 {
237 unsigned int i, j;
238
239 for (i = 0; i < static_cast<unsigned int>(this->Size()); ++i)
240 {
241 if (m_Candidates[i].Size > 0)
242 {
243 for (j = 0; j < m_MeasurementVectorSize; ++j)
244 {
245 m_Candidates[i].Centroid[j] =
246 m_Candidates[i].WeightedCentroid[j] / static_cast<double>(m_Candidates[i].Size);
247 }
248 }
249 }
250 }
251
253 Candidate & operator[](int index) { return m_Candidates[index]; }
254
255 private:
257 std::vector<Candidate> m_Candidates;
258
260 MeasurementVectorSizeType m_MeasurementVectorSize{ 0 };
261 }; // end of class
262
268 double
270
273 int
274 GetClosestCandidate(ParameterType & measurements, std::vector<int> & validIndexes);
275
277 bool
279 ParameterType & pointB,
280 MeasurementVectorType & lowerBound,
281 MeasurementVectorType & upperBound);
282
285 void
287 std::vector<int> validIndexes,
288 MeasurementVectorType & lowerBound,
289 MeasurementVectorType & upperBound);
290
292 void
294
296 void
298
300 void
302
304 void
306
307 void
309
310private:
312 int m_CurrentIteration{ 0 };
313
315 int m_MaximumIteration{ 100 };
316
318 double m_CentroidPositionChanges{ 0.0 };
319
322 double m_CentroidPositionChangesThreshold{ 0.0 };
323
325 typename TKdTree::Pointer m_KdTree{};
326
329
331 ParametersType m_Parameters{};
332
333 CandidateVector m_CandidateVector{};
334
335 ParameterType m_TempVertex{};
336
337 bool m_UseClusterLabels{ false };
338 bool m_GenerateClusterLabels{ false };
339 ClusterLabelsType m_ClusterLabels{};
340 MeasurementVectorSizeType m_MeasurementVectorSize{ 0 };
341 MembershipFunctionVectorObjectPointer m_MembershipFunctionsObject{};
342}; // end of class
343} // end of namespace Statistics
344} // end of namespace itk
345
346#ifndef ITK_MANUAL_INSTANTIATION
347# include "itkKdTreeBasedKmeansEstimator.hxx"
348#endif
349
350#endif
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
static unsigned int GetLength()
static void SetLength(T &m, const unsigned int s)
Base class for most ITK classes.
Definition: itkObject.h:62
Decorates any "simple" data type (data types without smart pointers) with a DataObject API.
DistanceToCentroidMembershipFunction models class membership using a distance metric.
fast k-means algorithm implementation using k-d tree structure
typename TKdTree::InstanceIdentifier InstanceIdentifier
void CopyParameters(InternalParametersType &source, ParametersType &target)
typename MembershipFunctionType::ConstPointer MembershipFunctionPointer
int GetClosestCandidate(ParameterType &measurements, std::vector< int > &validIndexes)
void CopyParameters(InternalParametersType &source, InternalParametersType &target)
const MembershipFunctionVectorObjectType * GetOutput() const
std::vector< MembershipFunctionPointer > MembershipFunctionVectorType
void PrintPoint(ParameterType &point)
typename MembershipFunctionVectorObjectType::Pointer MembershipFunctionVectorObjectPointer
bool IsFarther(ParameterType &pointA, ParameterType &pointB, MeasurementVectorType &lowerBound, MeasurementVectorType &upperBound)
void FillClusterLabels(KdTreeNodeType *node, int closestIndex)
void CopyParameters(ParametersType &source, InternalParametersType &target)
void Filter(KdTreeNodeType *node, std::vector< int > validIndexes, MeasurementVectorType &lowerBound, MeasurementVectorType &upperBound)
std::unordered_map< InstanceIdentifier, unsigned int > ClusterLabelsType
typename DistanceToCentroidMembershipFunctionType::Pointer DistanceToCentroidMembershipFunctionPointer
void PrintSelf(std::ostream &os, Indent indent) const override
double GetSumOfSquaredPositionChanges(InternalParametersType &previous, InternalParametersType &current)
typename KdTreeNodeType::CentroidType CentroidType
typename TKdTree::MeasurementVectorType MeasurementVectorType
void GetPoint(ParameterType &point, MeasurementVectorType measurements)
MembershipFunctionBase defines common interfaces for membership functions.
SmartPointer< const Self > ConstPointer
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
*par Constraints *The filter image with at least two dimensions and a vector *length of at least The theory supports extension to scalar but *the implementation of the itk vector classes do not **The template parameter TRealType must be floating point(float or double) or *a user-defined "real" numerical type with arithmetic operations defined *sufficient to compute derivatives. **\par Performance *This filter will automatically multithread if run with *SetUsePrincipleComponents
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:72