ITK  4.6.0
Insight Segmentation and Registration Toolkit
itkKdTreeBasedKmeansEstimator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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  * http://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 "itksys/hash_map.hxx"
23 
24 #include "itkObject.h"
29 
30 namespace itk
31 {
32 namespace Statistics
33 {
76 template< typename TKdTree >
78  public Object
79 {
80 public:
83  typedef Object Superclass;
86 
88  itkNewMacro(Self);
89 
92 
94  typedef typename TKdTree::KdTreeNodeType KdTreeNodeType;
95  typedef typename TKdTree::MeasurementType MeasurementType;
96  typedef typename TKdTree::MeasurementVectorType MeasurementVectorType;
97  typedef typename TKdTree::InstanceIdentifier InstanceIdentifier;
98  typedef typename TKdTree::SampleType SampleType;
99  typedef typename KdTreeNodeType::CentroidType CentroidType;
100 
102  typedef unsigned int MeasurementVectorSizeType;
103 
107  typedef std::vector< ParameterType > InternalParametersType;
109 
114 
117 
120  typedef std::vector< MembershipFunctionPointer > MembershipFunctionVectorType;
123  typedef typename
125 
129 
131  itkSetMacro(Parameters, ParametersType);
132  itkGetConstMacro(Parameters, ParametersType);
134 
136  itkSetMacro(MaximumIteration, int);
137  itkGetConstMacro(MaximumIteration, int);
139 
142  itkSetMacro(CentroidPositionChangesThreshold, double);
143  itkGetConstMacro(CentroidPositionChangesThreshold, double);
144 
146  void SetKdTree(TKdTree *tree);
147 
148  const TKdTree * GetKdTree() const;
149 
151  itkGetConstMacro(MeasurementVectorSize, MeasurementVectorSizeType);
152 
153  itkGetConstMacro(CurrentIteration, int);
154  itkGetConstMacro(CentroidPositionChanges, double);
155 
160  void StartOptimization();
161 
162  typedef itksys::hash_map< InstanceIdentifier, unsigned int > ClusterLabelsType;
163 
164  itkSetMacro(UseClusterLabels, bool);
165  itkGetConstMacro(UseClusterLabels, bool);
166 
167 protected:
170 
171  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
172 
173  void FillClusterLabels(KdTreeNodeType *node, int closestIndex);
174 
180  {
181 public:
183 
184  struct Candidate {
187  int Size;
188  }; // end of struct
189 
190  virtual ~CandidateVector() {}
191 
193  int Size() const
194  {
195  return static_cast< int >( m_Candidates.size() );
196  }
197 
201  {
203  m_Candidates.resize( centroids.size() );
204  for ( unsigned int i = 0; i < centroids.size(); i++ )
205  {
206  Candidate candidate;
207  candidate.Centroid = centroids[i];
210  candidate.WeightedCentroid.Fill(0.0);
211  candidate.Size = 0;
212  m_Candidates[i] = candidate;
213  }
214  }
216 
219  {
220  unsigned int i;
221 
222  centroids.resize( this->Size() );
223  for ( i = 0; i < (unsigned int)this->Size(); i++ )
224  {
225  centroids[i] = m_Candidates[i].Centroid;
226  }
227  }
228 
232  {
233  unsigned int i, j;
234 
235  for ( i = 0; i < (unsigned int)this->Size(); i++ )
236  {
237  if ( m_Candidates[i].Size > 0 )
238  {
239  for ( j = 0; j < m_MeasurementVectorSize; j++ )
240  {
241  m_Candidates[i].Centroid[j] =
242  m_Candidates[i].WeightedCentroid[j]
243  / double(m_Candidates[i].Size);
244  }
245  }
246  }
247  }
248 
250  Candidate & operator[](int index)
251  {
252  return m_Candidates[index];
253  }
254 
255 private:
257  std::vector< Candidate > m_Candidates;
258 
261  }; // end of class
262 
269  InternalParametersType & current);
270 
273  int GetClosestCandidate(ParameterType & measurements,
274  std::vector< int > & validIndexes);
275 
277  bool IsFarther(ParameterType & pointA,
278  ParameterType & pointB,
279  MeasurementVectorType & lowerBound,
280  MeasurementVectorType & upperBound);
281 
284  void Filter(KdTreeNodeType *node,
285  std::vector< int > validIndexes,
286  MeasurementVectorType & lowerBound,
287  MeasurementVectorType & upperBound);
288 
291 
293  void CopyParameters(ParametersType & source, InternalParametersType & target);
294 
296  void CopyParameters(InternalParametersType & source, ParametersType & target);
297 
299  void GetPoint(ParameterType & point, MeasurementVectorType measurements);
300 
301  void PrintPoint(ParameterType & point);
302 
303 private:
306 
309 
312 
316 
318  typename TKdTree::Pointer m_KdTree;
319 
322 
325 
327 
329 
335 }; // end of class
336 } // end of namespace Statistics
337 } // end of namespace itk
338 
339 #ifndef ITK_MANUAL_INSTANTIATION
340 #include "itkKdTreeBasedKmeansEstimator.hxx"
341 #endif
342 
343 #endif
virtual void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
EuclideanDistanceMetric< ParameterType >::Pointer m_DistanceMetric
Light weight base class for most itk classes.
MembershipFunctionBase< MeasurementVectorType > MembershipFunctionType
std::vector< MembershipFunctionPointer > MembershipFunctionVectorType
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
void GetPoint(ParameterType &point, MeasurementVectorType measurements)
double GetSumOfSquaredPositionChanges(InternalParametersType &previous, InternalParametersType &current)
fast k-means algorithm implementation using k-d tree structure
DistanceToCentroidMembershipFunctionType::Pointer DistanceToCentroidMembershipFunctionPointer
void PrintPoint(ParameterType &point)
MembershipFunctionVectorObjectPointer m_MembershipFunctionsObject
const MembershipFunctionVectorObjectType * GetOutput() const
Decorates any &quot;simple&quot; data type (data types without smart pointers) with a DataObject API...
static unsigned int GetLength()
MembershipFunctionVectorObjectType::Pointer MembershipFunctionVectorObjectPointer
MembershipFunctionBase defines common interfaces for membership functions.
static void SetLength(T &m, const unsigned int s)
SimpleDataObjectDecorator< MembershipFunctionVectorType > MembershipFunctionVectorObjectType
void FillClusterLabels(KdTreeNodeType *node, int closestIndex)
MembershipFunctionType::ConstPointer MembershipFunctionPointer
itksys::hash_map< InstanceIdentifier, unsigned int > ClusterLabelsType
int GetClosestCandidate(ParameterType &measurements, std::vector< int > &validIndexes)
DistanceToCentroidMembershipFunction models class membership using a distance metric.
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void Filter(KdTreeNodeType *node, std::vector< int > validIndexes, MeasurementVectorType &lowerBound, MeasurementVectorType &upperBound)
DistanceToCentroidMembershipFunction< MeasurementVectorType > DistanceToCentroidMembershipFunctionType
Base class for most ITK classes.
Definition: itkObject.h:57
bool IsFarther(ParameterType &pointA, ParameterType &pointB, MeasurementVectorType &lowerBound, MeasurementVectorType &upperBound)
void CopyParameters(InternalParametersType &source, InternalParametersType &target)