ITK  4.13.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 "itkMath.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< typename TInputImage,
130  typename TMembershipFunction >
131 class ITK_TEMPLATE_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 
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:
213  ~ImageKmeansModelEstimator() ITK_OVERRIDE;
214  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
215 
217  void GenerateData() ITK_OVERRIDE;
218 
220  void Allocate();
221 
223  void PrintKmeansAlgorithmResults();
224 
225 private:
226  ITK_DISALLOW_COPY_AND_ASSIGN(ImageKmeansModelEstimator);
227 
234  virtual void EstimateModels() ITK_OVERRIDE;
235 
236  void EstimateKmeansModelParameters();
237 
238  typedef typename TInputImage::SizeType ImageSizeType;
239 
241  typedef typename TInputImage::PixelType::VectorType InputPixelVectorType;
242 
243  void Reallocate(int oldSize, int newSize);
244 
245  //Local functions
246  int WithCodebookUseGLA(); // GLA stands for the Generalized Lloyd Algorithm
247 
248  int WithoutCodebookUseLBG(); //LBG stands for the Lindo Buzo Gray Algorithm
249 
250  void NearestNeighborSearchBasic(double *distortion);
251 
252  void SplitCodewords(int currentSize,
253  int numDesired,
254  int scale);
255 
256  void Perturb(double *oldCodeword,
257  int scale,
258  double *newCodeword);
259 
261 
262  // Buffer for K-means calcualtions
264 
265  double m_Threshold;
266  double m_OffsetAdd;
267  double m_OffsetMultiply;
268  int m_MaxSplitAttempts;
269 
270  bool m_ValidInCodebook;
271  double m_DoubleMaximum;
272  double m_OutputDistortion;
273  int m_OutputNumberOfEmptyCells;
274 
275  SizeValueType m_VectorDimension;
276  SizeValueType m_NumberOfCodewords;
277  SizeValueType m_CurrentNumberOfCodewords;
278 
279  CodebookMatrixOfIntegerType m_CodewordHistogram;
280  CodebookMatrixOfDoubleType m_CodewordDistortion;
281 }; // class ImageKmeansModelEstimator
282 
283 } // namespace itk
284 
285 #ifndef ITK_MANUAL_INSTANTIATION
286 #include "itkImageKmeansModelEstimator.hxx"
287 #endif
288 
289 #endif
Base class for ImageKmeansModelEstimator object.
Light weight base class for most itk classes.
ImageRegionIterator< TInputImage > InputImageIterator
CodebookMatrixOfDoubleType GetOutCodebook()
TMembershipFunction::Pointer MembershipFunctionPointer
TInputImage::PixelType::VectorType InputPixelVectorType
Base class for model estimation from images used for classification.
unsigned long SizeValueType
Definition: itkIntTypes.h:143
ImageModelEstimatorBase< TInputImage, TMembershipFunction > Superclass
ImageRegionConstIterator< TInputImage > InputImageConstIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
CodebookMatrixOfDoubleType GetKmeansResults(void)
Control indentation during Print() invocation.
Definition: itkIndent.h:49
TInputImage::PixelType::VectorType InputImageVectorType
TInputImage::ConstPointer InputImageConstPointer
A multi-dimensional iterator templated over image type that walks a region of pixels.