00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _itkImageKmeansModelEstimator_h
00018 #define _itkImageKmeansModelEstimator_h
00019
00020 #include <time.h>
00021 #include <math.h>
00022 #include <float.h>
00023
00024 #include "vnl/vnl_vector.h"
00025 #include "vnl/vnl_matrix.h"
00026 #include "vnl/vnl_math.h"
00027 #include "vnl/algo/vnl_matrix_inverse.h"
00028
00029 #include "itkImageRegionIterator.h"
00030 #include "itkImageRegionConstIterator.h"
00031 #include "itkExceptionObject.h"
00032
00033 #include "itkImageModelEstimatorBase.h"
00034
00035 #define ONEBAND 1
00036 #define GLA_CONVERGED 1
00037 #define GLA_NOT_CONVERGED 2
00038 #define LBG_COMPLETED 3
00039
00040 namespace itk
00041 {
00042
00125 template <class TInputImage,
00126 class TMembershipFunction>
00127 class ITK_EXPORT ImageKmeansModelEstimator:
00128 public ImageModelEstimatorBase<TInputImage, TMembershipFunction>
00129 {
00130 public:
00132 typedef ImageKmeansModelEstimator Self;
00133 typedef ImageModelEstimatorBase<TInputImage, TMembershipFunction> Superclass;
00134
00135 typedef SmartPointer<Self> Pointer;
00136 typedef SmartPointer<const Self> ConstPointer;
00137
00139 itkNewMacro(Self);
00140
00142 itkTypeMacro(ImageKmeansModelEstimator, ImageModelEstimatorBase);
00143
00145 typedef TInputImage InputImageType;
00146 typedef typename TInputImage::Pointer InputImagePointer;
00147 typedef typename TInputImage::ConstPointer InputImageConstPointer;
00148
00151 typedef typename TInputImage::PixelType::VectorType
00152 InputImageVectorType;
00153
00155 typedef typename TInputImage::PixelType InputImagePixelType;
00156
00158 typedef
00159 ImageRegionIterator<TInputImage> InputImageIterator;
00160
00161 typedef
00162 ImageRegionConstIterator<TInputImage> InputImageConstIterator;
00163
00165 typedef typename TMembershipFunction::Pointer MembershipFunctionPointer ;
00166
00168 typedef vnl_matrix<double> CodebookMatrixOfDoubleType;
00169
00171 typedef vnl_matrix<int> CodebookMatrixOfIntegerType;
00172
00174 void SetCodebook(CodebookMatrixOfDoubleType InCodebook);
00175
00177 itkGetMacro(Codebook,CodebookMatrixOfDoubleType);
00178
00180 CodebookMatrixOfDoubleType GetOutCodebook()
00181 { return m_Codebook; }
00182
00184 itkSetMacro(Threshold,double);
00185
00187 itkGetMacro(Threshold,double);
00188
00190 itkSetMacro(OffsetAdd,double);
00191
00193 itkGetMacro(OffsetAdd,double);
00194
00196 itkSetMacro(OffsetMultiply,double);
00197
00199 itkGetMacro(OffsetMultiply,double);
00200
00202 itkSetMacro(MaxSplitAttempts,int);
00203
00205 itkGetMacro(MaxSplitAttempts,int);
00206
00208 CodebookMatrixOfDoubleType GetKmeansResults(void)
00209 { return m_Centroid; }
00210
00211 protected:
00212 ImageKmeansModelEstimator();
00213 ~ImageKmeansModelEstimator();
00214 virtual void PrintSelf(std::ostream& os, Indent indent) const;
00215
00217 void GenerateData() ;
00218
00220 void Allocate();
00221
00223 void PrintKmeansAlgorithmResults();
00224 private:
00225 ImageKmeansModelEstimator(const Self&);
00226 void operator=(const Self&);
00227
00234 virtual void EstimateModels();
00235
00236 void EstimateKmeansModelParameters();
00237
00238 typedef typename TInputImage::SizeType ImageSizeType;
00239
00241 typedef typename TInputImage::PixelType::VectorType InputPixelVectorType;
00242
00243 void Reallocate(int oldSize, int newSize);
00244
00245
00246 int WithCodebookUseGLA();
00247 int WithoutCodebookUseLBG();
00248
00249 void NearestNeighborSearchBasic(double *distortion);
00250
00251 void SplitCodewords(int currentSize,
00252 int numDesired,
00253 int scale);
00254
00255 void Perturb(double *oldCodeword,
00256 int scale,
00257 double *newCodeword);
00258
00259 CodebookMatrixOfDoubleType m_Codebook;
00260
00261
00262 CodebookMatrixOfDoubleType m_Centroid;
00263
00264 double m_Threshold;
00265 double m_OffsetAdd;
00266 double m_OffsetMultiply;
00267 int m_MaxSplitAttempts;
00268
00269
00270 bool m_ValidInCodebook;
00271 double m_DoubleMaximum;
00272 double m_OutputDistortion;
00273 int m_OutputNumberOfEmptyCells;
00274
00275 unsigned long m_VectorDimension;
00276 unsigned long m_NumberOfCodewords;
00277 unsigned long m_CurrentNumberOfCodewords;
00278
00279 CodebookMatrixOfIntegerType m_CodewordHistogram;
00280 CodebookMatrixOfDoubleType m_CodewordDistortion;
00281
00282 };
00283
00284
00285 }
00286
00287 #ifndef ITK_MANUAL_INSTANTIATION
00288 #include "itkImageKmeansModelEstimator.txx"
00289 #endif
00290
00291
00292
00293 #endif