ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkImageKmeansModelEstimator.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 __itkImageKmeansModelEstimator_h
19 #define __itkImageKmeansModelEstimator_h
20 
21 #include <ctime>
22 #include <cmath>
23 #include <cfloat>
24 
25 #include "vnl/vnl_vector.h"
26 #include "vnl/vnl_matrix.h"
27 #include "vnl/vnl_math.h"
28 #include "vnl/algo/vnl_matrix_inverse.h"
29 
30 #include "itkImageRegionIterator.h"
31 #include "itkMacro.h"
32 
34 
35 #define ONEBAND 1
36 #define GLA_CONVERGED 1
37 #define GLA_NOT_CONVERGED 2
38 #define LBG_COMPLETED 3
39 
40 namespace itk
41 {
129 template< class TInputImage,
130  class TMembershipFunction >
131 class ITK_EXPORT ImageKmeansModelEstimator:
132  public ImageModelEstimatorBase< TInputImage, TMembershipFunction >
133 {
134 public:
139 
142 
144  itkNewMacro(Self);
145 
148 
150  typedef TInputImage InputImageType;
151  typedef typename TInputImage::Pointer InputImagePointer;
152  typedef typename TInputImage::ConstPointer InputImageConstPointer;
153 
156  typedef typename TInputImage::PixelType::VectorType InputImageVectorType;
157 
159  typedef typename TInputImage::PixelType InputImagePixelType;
160 
163 
165 
167  typedef typename TMembershipFunction::Pointer MembershipFunctionPointer;
168 
170  typedef vnl_matrix< double > CodebookMatrixOfDoubleType;
171 
173  typedef vnl_matrix< int > CodebookMatrixOfIntegerType;
174 
176  void SetCodebook(CodebookMatrixOfDoubleType InCodebook);
177 
179  itkGetConstMacro(Codebook, CodebookMatrixOfDoubleType);
180 
183 
185  itkSetMacro(Threshold, double);
186 
188  itkGetConstMacro(Threshold, double);
189 
191  itkSetMacro(OffsetAdd, double);
192 
194  itkGetConstMacro(OffsetAdd, double);
195 
197  itkSetMacro(OffsetMultiply, double);
198 
200  itkGetConstMacro(OffsetMultiply, double);
201 
203  itkSetMacro(MaxSplitAttempts, int);
204 
206  itkGetConstMacro(MaxSplitAttempts, int);
207 
209  CodebookMatrixOfDoubleType GetKmeansResults(void) { return m_Centroid; }
210 
211 protected:
214  virtual void PrintSelf(std::ostream & os, Indent indent) const;
215 
217  void GenerateData();
218 
220  void Allocate();
221 
223  void PrintKmeansAlgorithmResults();
224 
225 private:
226  ImageKmeansModelEstimator(const Self &); //purposely not implemented
227  void operator=(const Self &); //purposely not implemented
228 
235  virtual void EstimateModels();
236 
237  void EstimateKmeansModelParameters();
238 
239  typedef typename TInputImage::SizeType ImageSizeType;
240 
242  typedef typename TInputImage::PixelType::VectorType InputPixelVectorType;
243 
244  void Reallocate(int oldSize, int newSize);
245 
246  //Local functions
247  int WithCodebookUseGLA(); // GLA stands for the Generalized Lloyd Algorithm
248 
249  int WithoutCodebookUseLBG(); //LBG stands for the Lindo Buzo Gray Algorithm
250 
251  void NearestNeighborSearchBasic(double *distortion);
252 
253  void SplitCodewords(int currentSize,
254  int numDesired,
255  int scale);
256 
257  void Perturb(double *oldCodeword,
258  int scale,
259  double *newCodeword);
260 
262 
263  // Buffer for K-means calcualtions
265 
266  double m_Threshold;
267  double m_OffsetAdd;
270 
271  //SizeValueType m_NumberOfModels;
276 
280 
283 }; // class ImageKmeansModelEstimator
284 } // namespace itk
285 
286 #ifndef ITK_MANUAL_INSTANTIATION
287 #include "itkImageKmeansModelEstimator.hxx"
288 #endif
289 
290 #endif
291