ITK  4.2.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< class TKdTree >
77 class ITK_EXPORT KdTreeBasedKmeansEstimator:
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 
128  const MembershipFunctionVectorObjectType * GetOutput() const;
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 protected:
169 
170  void PrintSelf(std::ostream & os, Indent indent) const;
171 
172  void FillClusterLabels(KdTreeNodeType *node, int closestIndex);
173 
179  {
180 public:
182 
183  struct Candidate {
186  int Size;
187  }; // end of struct
188 
189  virtual ~CandidateVector() {}
190 
192  int Size() const
193  {
194  return static_cast< int >( m_Candidates.size() );
195  }
196 
199  void SetCentroids(InternalParametersType & centroids)
200  {
201  this->m_MeasurementVectorSize = NumericTraits<ParameterType>::GetLength(centroids[0]);
202  m_Candidates.resize( centroids.size() );
203  for ( unsigned int i = 0; i < centroids.size(); i++ )
204  {
205  Candidate candidate;
206  candidate.Centroid = centroids[i];
208  m_MeasurementVectorSize);
209  candidate.WeightedCentroid.Fill(0.0);
210  candidate.Size = 0;
211  m_Candidates[i] = candidate;
212  }
213  }
215 
217  void GetCentroids(InternalParametersType & centroids)
218  {
219  unsigned int i;
220 
221  centroids.resize( this->Size() );
222  for ( i = 0; i < (unsigned int)this->Size(); i++ )
223  {
224  centroids[i] = m_Candidates[i].Centroid;
225  }
226  }
227 
230  void UpdateCentroids()
231  {
232  unsigned int i, j;
233 
234  for ( i = 0; i < (unsigned int)this->Size(); i++ )
235  {
236  if ( m_Candidates[i].Size > 0 )
237  {
238  for ( j = 0; j < m_MeasurementVectorSize; j++ )
239  {
240  m_Candidates[i].Centroid[j] =
241  m_Candidates[i].WeightedCentroid[j]
242  / double(m_Candidates[i].Size);
243  }
244  }
245  }
246  }
247 
249  Candidate & operator[](int index)
250  {
251  return m_Candidates[index];
252  }
253 
254 private:
256  std::vector< Candidate > m_Candidates;
257 
260  }; // end of class
261 
267  double GetSumOfSquaredPositionChanges(InternalParametersType & previous,
268  InternalParametersType & current);
269 
272  int GetClosestCandidate(ParameterType & measurements,
273  std::vector< int > & validIndexes);
274 
276  bool IsFarther(ParameterType & pointA,
277  ParameterType & pointB,
278  MeasurementVectorType & lowerBound,
279  MeasurementVectorType & upperBound);
280 
283  void Filter(KdTreeNodeType *node,
284  std::vector< int > validIndexes,
285  MeasurementVectorType & lowerBound,
286  MeasurementVectorType & upperBound);
287 
289  void CopyParameters(InternalParametersType & source, InternalParametersType & target);
290 
292  void CopyParameters(ParametersType & source, InternalParametersType & target);
293 
295  void CopyParameters(InternalParametersType & source, ParametersType & target);
296 
298  void GetPoint(ParameterType & point, MeasurementVectorType measurements);
299 
300  void PrintPoint(ParameterType & point);
301 
302 private:
305 
308 
311 
315 
317  typename TKdTree::Pointer m_KdTree;
318 
321 
324 
326 
328 
334 }; // end of class
335 } // end of namespace Statistics
336 } // end of namespace itk
337 
338 #ifndef ITK_MANUAL_INSTANTIATION
339 #include "itkKdTreeBasedKmeansEstimator.hxx"
340 #endif
341 
342 #endif
343