ITK  5.0.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:
135  ITK_DISALLOW_COPY_AND_ASSIGN(ImageKmeansModelEstimator);
136 
139  using Superclass =
141 
144 
146  itkNewMacro(Self);
147 
150 
152  using InputImageType = TInputImage;
153  using InputImagePointer = typename TInputImage::Pointer;
154  using InputImageConstPointer = typename TInputImage::ConstPointer;
155 
159 
161  using InputImagePixelType = typename TInputImage::PixelType;
162 
165 
167 
169  using MembershipFunctionPointer = typename TMembershipFunction::Pointer;
170 
172  using CodebookMatrixOfDoubleType = vnl_matrix< double >;
173 
175  using CodebookMatrixOfIntegerType = vnl_matrix< int >;
176 
178  void SetCodebook(CodebookMatrixOfDoubleType InCodebook);
179 
181  itkGetConstMacro(Codebook, CodebookMatrixOfDoubleType);
182 
185 
187  itkSetMacro(Threshold, double);
188 
190  itkGetConstMacro(Threshold, double);
191 
193  itkSetMacro(OffsetAdd, double);
194 
196  itkGetConstMacro(OffsetAdd, double);
197 
199  itkSetMacro(OffsetMultiply, double);
200 
202  itkGetConstMacro(OffsetMultiply, double);
203 
205  itkSetMacro(MaxSplitAttempts, int);
206 
208  itkGetConstMacro(MaxSplitAttempts, int);
209 
212 
213 protected:
215  ~ImageKmeansModelEstimator() override = default;
216  void PrintSelf(std::ostream & os, Indent indent) const override;
217 
219  void GenerateData() override;
220 
222  void Allocate();
223 
225  void PrintKmeansAlgorithmResults();
226 
227 private:
234  void EstimateModels() override;
235 
236  void EstimateKmeansModelParameters();
237 
239 
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;
269 
274 
278 
281 }; // class ImageKmeansModelEstimator
282 
283 } // namespace itk
284 
285 #ifndef ITK_MANUAL_INSTANTIATION
286 #include "itkImageKmeansModelEstimator.hxx"
287 #endif
288 
289 #endif
typename TInputImage::ConstPointer InputImageConstPointer
Base class for ImageKmeansModelEstimator object.
Light weight base class for most itk classes.
typename TInputImage::SizeType ImageSizeType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
typename TInputImage::PixelType::VectorType InputImageVectorType
CodebookMatrixOfDoubleType GetOutCodebook()
CodebookMatrixOfIntegerType m_CodewordHistogram
CodebookMatrixOfDoubleType GetKmeansResults()
typename TInputImage::PixelType::VectorType InputPixelVectorType
Base class for model estimation from images used for classification.
typename TInputImage::PixelType InputImagePixelType
typename TInputImage::Pointer InputImagePointer
A multi-dimensional iterator templated over image type that walks a region of pixels.
CodebookMatrixOfDoubleType m_CodewordDistortion
typename TMembershipFunction::Pointer MembershipFunctionPointer
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Base class for most ITK classes.
Definition: itkObject.h:60
A multi-dimensional iterator templated over image type that walks a region of pixels.