ITK  5.3.0
Insight Toolkit
itkImageKmeansModelEstimator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 {
130 template <typename TInputImage, typename TMembershipFunction>
131 class ITK_TEMPLATE_EXPORT ImageKmeansModelEstimator : public ImageModelEstimatorBase<TInputImage, TMembershipFunction>
132 {
133 public:
134  ITK_DISALLOW_COPY_AND_MOVE(ImageKmeansModelEstimator);
136 
140 
143 
145  itkNewMacro(Self);
146 
149 
151  using InputImageType = TInputImage;
152  using InputImagePointer = typename TInputImage::Pointer;
153  using InputImageConstPointer = typename TInputImage::ConstPointer;
154 
158 
160  using InputImagePixelType = typename TInputImage::PixelType;
161 
164 
166 
168  using MembershipFunctionPointer = typename TMembershipFunction::Pointer;
169 
171  using CodebookMatrixOfDoubleType = vnl_matrix<double>;
172 
174  using CodebookMatrixOfIntegerType = vnl_matrix<int>;
175 
177  void
178  SetCodebook(CodebookMatrixOfDoubleType inCodebook);
179 
181  itkGetConstMacro(Codebook, CodebookMatrixOfDoubleType);
182 
186  {
187  return m_Codebook;
188  }
189 
191  itkSetMacro(Threshold, double);
192  itkGetConstMacro(Threshold, double);
194 
196  itkSetMacro(OffsetAdd, double);
197  itkGetConstMacro(OffsetAdd, double);
199 
201  itkSetMacro(OffsetMultiply, double);
202  itkGetConstMacro(OffsetMultiply, double);
204 
206  itkSetMacro(MaxSplitAttempts, int);
207  itkGetConstMacro(MaxSplitAttempts, int);
209 
211  CodebookMatrixOfDoubleType
213  {
214  return m_Centroid;
215  }
216 
217 protected:
219  ~ImageKmeansModelEstimator() override = default;
220  void
221  PrintSelf(std::ostream & os, Indent indent) const override;
222 
224  void
225  GenerateData() override;
226 
228  void
229  Allocate();
230 
232  void
233  PrintKmeansAlgorithmResults();
234 
235 private:
244  void
245  EstimateModels() override;
246 
248  void
249  EstimateKmeansModelParameters();
250 
252 
255 
257  void
258  Reallocate(int oldSize, int newSize);
259 
260  int
261  WithCodebookUseGLA();
262 
263  int
264  WithoutCodebookUseLBG();
265 
266  void
267  NearestNeighborSearchBasic(double * distortion);
268 
269  void
270  SplitCodewords(int currentSize, int numDesired, int scale);
271 
272  void
273  Perturb(double * oldCodeword, int scale, double * newCodeword);
274 
276 
277  // Buffer for K-means calculations
279 
280  double m_Threshold;
281  double m_OffsetAdd;
284 
289 
293 
296 }; // class ImageKmeansModelEstimator
297 
298 } // end namespace itk
299 
300 #ifndef ITK_MANUAL_INSTANTIATION
301 # include "itkImageKmeansModelEstimator.hxx"
302 #endif
303 
304 #endif
itk::ImageKmeansModelEstimator::GetOutCodebook
CodebookMatrixOfDoubleType GetOutCodebook()
Definition: itkImageKmeansModelEstimator.h:185
itk::ImageKmeansModelEstimator::CodebookMatrixOfIntegerType
vnl_matrix< int > CodebookMatrixOfIntegerType
Definition: itkImageKmeansModelEstimator.h:174
itk::ImageKmeansModelEstimator
Base class for ImageKmeansModelEstimator object.
Definition: itkImageKmeansModelEstimator.h:131
itk::ImageKmeansModelEstimator::m_OutputDistortion
double m_OutputDistortion
Definition: itkImageKmeansModelEstimator.h:287
itk::ImageKmeansModelEstimator::m_NumberOfCodewords
SizeValueType m_NumberOfCodewords
Definition: itkImageKmeansModelEstimator.h:291
itk::GTest::TypedefsAndConstructors::Dimension2::VectorType
ImageBaseType::SpacingType VectorType
Definition: itkGTestTypedefsAndConstructors.h:53
itk::ImageKmeansModelEstimator::m_CodewordDistortion
CodebookMatrixOfDoubleType m_CodewordDistortion
Definition: itkImageKmeansModelEstimator.h:295
itk::ImageKmeansModelEstimator::m_CodewordHistogram
CodebookMatrixOfIntegerType m_CodewordHistogram
Definition: itkImageKmeansModelEstimator.h:294
itkImageModelEstimatorBase.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::ImageKmeansModelEstimator::InputImagePointer
typename TInputImage::Pointer InputImagePointer
Definition: itkImageKmeansModelEstimator.h:152
itk::SmartPointer< Self >
itkImageRegionIterator.h
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ImageKmeansModelEstimator::ImageSizeType
typename TInputImage::SizeType ImageSizeType
Definition: itkImageKmeansModelEstimator.h:251
itk::ImageKmeansModelEstimator::InputImagePixelType
typename TInputImage::PixelType InputImagePixelType
Definition: itkImageKmeansModelEstimator.h:160
itk::ImageKmeansModelEstimator::CodebookMatrixOfDoubleType
vnl_matrix< double > CodebookMatrixOfDoubleType
Definition: itkImageKmeansModelEstimator.h:171
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:80
itk::ImageKmeansModelEstimator::m_OffsetMultiply
double m_OffsetMultiply
Definition: itkImageKmeansModelEstimator.h:282
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::ImageKmeansModelEstimator::m_Threshold
double m_Threshold
Definition: itkImageKmeansModelEstimator.h:280
itkMacro.h
itk::ImageKmeansModelEstimator::m_CurrentNumberOfCodewords
SizeValueType m_CurrentNumberOfCodewords
Definition: itkImageKmeansModelEstimator.h:292
itk::ImageModelEstimatorBase
Base class for model estimation from images used for classification.
Definition: itkImageModelEstimatorBase.h:64
itk::ImageKmeansModelEstimator::m_OffsetAdd
double m_OffsetAdd
Definition: itkImageKmeansModelEstimator.h:281
itk::ImageKmeansModelEstimator::InputImageConstPointer
typename TInputImage::ConstPointer InputImageConstPointer
Definition: itkImageKmeansModelEstimator.h:153
itk::ImageKmeansModelEstimator::m_Centroid
CodebookMatrixOfDoubleType m_Centroid
Definition: itkImageKmeansModelEstimator.h:278
itk::ImageKmeansModelEstimator::InputImageVectorType
typename TInputImage::PixelType::VectorType InputImageVectorType
Definition: itkImageKmeansModelEstimator.h:157
itk::ImageKmeansModelEstimator::m_Codebook
CodebookMatrixOfDoubleType m_Codebook
Definition: itkImageKmeansModelEstimator.h:275
itk::ImageKmeansModelEstimator::m_DoubleMaximum
double m_DoubleMaximum
Definition: itkImageKmeansModelEstimator.h:286
itk::ImageKmeansModelEstimator::m_VectorDimension
SizeValueType m_VectorDimension
Definition: itkImageKmeansModelEstimator.h:290
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ImageKmeansModelEstimator::m_OutputNumberOfEmptyCells
int m_OutputNumberOfEmptyCells
Definition: itkImageKmeansModelEstimator.h:288
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::ImageRegionConstIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionConstIterator.h:109
itk::ImageKmeansModelEstimator::GetKmeansResults
CodebookMatrixOfDoubleType GetKmeansResults()
Definition: itkImageKmeansModelEstimator.h:212
itk::ImageKmeansModelEstimator::MembershipFunctionPointer
typename TMembershipFunction::Pointer MembershipFunctionPointer
Definition: itkImageKmeansModelEstimator.h:168
itk::ImageKmeansModelEstimator::m_ValidInCodebook
bool m_ValidInCodebook
Definition: itkImageKmeansModelEstimator.h:285
itk::ImageKmeansModelEstimator::InputPixelVectorType
typename TInputImage::PixelType::VectorType InputPixelVectorType
Definition: itkImageKmeansModelEstimator.h:254
itkMath.h
itk::ImageKmeansModelEstimator::m_MaxSplitAttempts
int m_MaxSplitAttempts
Definition: itkImageKmeansModelEstimator.h:283
itk::ImageKmeansModelEstimator::InputImageType
TInputImage InputImageType
Definition: itkImageKmeansModelEstimator.h:151
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83