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&);
00228
00235 virtual void EstimateModels();
00236
00237 void EstimateKmeansModelParameters();
00238
00239 typedef typename TInputImage::SizeType ImageSizeType;
00240
00242 typedef typename TInputImage::PixelType::VectorType InputPixelVectorType;
00243
00244 void Reallocate(int oldSize, int newSize);
00245
00246
00247 int WithCodebookUseGLA();
00248 int WithoutCodebookUseLBG();
00249
00250 void NearestNeighborSearchBasic(double *distortion);
00251
00252 void SplitCodewords(int currentSize,
00253 int numDesired,
00254 int scale);
00255
00256 void Perturb(double *oldCodeword,
00257 int scale,
00258 double *newCodeword);
00259
00260 CodebookMatrixOfDoubleType m_Codebook;
00261
00262
00263 CodebookMatrixOfDoubleType m_Centroid;
00264
00265 double m_Threshold;
00266 double m_OffsetAdd;
00267 double m_OffsetMultiply;
00268 int m_MaxSplitAttempts;
00269
00270
00271 bool m_ValidInCodebook;
00272 double m_DoubleMaximum;
00273 double m_OutputDistortion;
00274 int m_OutputNumberOfEmptyCells;
00275
00276 unsigned long m_VectorDimension;
00277 unsigned long m_NumberOfCodewords;
00278 unsigned long m_CurrentNumberOfCodewords;
00279
00280 CodebookMatrixOfIntegerType m_CodewordHistogram;
00281 CodebookMatrixOfDoubleType m_CodewordDistortion;
00282
00283 };
00284
00285
00286 }
00287
00288 #ifndef ITK_MANUAL_INSTANTIATION
00289 #include "itkImageKmeansModelEstimator.txx"
00290 #endif
00291
00292
00293
00294 #endif
00295