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
00118 template <class TInputImage,
00119 class TMembershipFunction>
00120 class ITK_EXPORT ImageKmeansModelEstimator:
00121 public ImageModelEstimatorBase<TInputImage, TMembershipFunction>
00122 {
00123 public:
00125 typedef ImageKmeansModelEstimator Self;
00126 typedef ImageModelEstimatorBase<TInputImage, TMembershipFunction> Superclass;
00127
00128 typedef SmartPointer<Self> Pointer;
00129 typedef SmartPointer<const Self> ConstPointer;
00130
00132 itkNewMacro(Self);
00133
00135 itkTypeMacro(ImageKmeansModelEstimator, ImageModelEstimatorBase);
00136
00138 typedef TInputImage InputImageType;
00139 typedef typename TInputImage::Pointer InputImagePointer;
00140 typedef typename TInputImage::ConstPointer InputImageConstPointer;
00141
00144 typedef typename TInputImage::PixelType::VectorType
00145 InputImageVectorType;
00146
00148 typedef typename TInputImage::PixelType InputImagePixelType;
00149
00151 typedef
00152 ImageRegionIterator<TInputImage> InputImageIterator;
00153
00154 typedef
00155 ImageRegionConstIterator<TInputImage> InputImageConstIterator;
00156
00158 typedef typename TMembershipFunction::Pointer MembershipFunctionPointer ;
00159
00161 typedef vnl_matrix<double> CodebookMatrixOfDoubleType;
00162
00164 typedef vnl_matrix<int> CodebookMatrixOfIntegerType;
00165
00167 void SetCodebook(CodebookMatrixOfDoubleType InCodebook);
00168
00170 itkGetMacro(Codebook,CodebookMatrixOfDoubleType);
00171
00173 CodebookMatrixOfDoubleType GetOutCodebook()
00174 { return m_Codebook; }
00175
00177 itkSetMacro(Threshold,double);
00178
00180 itkGetMacro(Threshold,double);
00181
00183 itkSetMacro(OffsetAdd,double);
00184
00186 itkGetMacro(OffsetAdd,double);
00187
00189 itkSetMacro(OffsetMultiply,double);
00190
00192 itkGetMacro(OffsetMultiply,double);
00193
00195 itkSetMacro(MaxSplitAttempts,int);
00196
00198 itkGetMacro(MaxSplitAttempts,int);
00199
00201 CodebookMatrixOfDoubleType GetKmeansResults(void)
00202 { return m_Centroid; }
00203
00204 protected:
00205 ImageKmeansModelEstimator();
00206 ~ImageKmeansModelEstimator();
00207 virtual void PrintSelf(std::ostream& os, Indent indent) const;
00208
00210 void GenerateData() ;
00211
00213 void Allocate();
00214
00216 void PrintKmeansAlgorithmResults();
00217 private:
00218 ImageKmeansModelEstimator(const Self&);
00219 void operator=(const Self&);
00220
00227 virtual void EstimateModels();
00228
00229 void EstimateKmeansModelPrameters();
00230
00231 typedef typename TInputImage::SizeType ImageSizeType;
00232
00234 typedef typename TInputImage::PixelType::VectorType InputPixelVectorType;
00235
00236 void Reallocate(int oldSize, int newSize);
00237
00238
00239 int WithCodebookUseGLA();
00240 int WithoutCodebookUseLBG();
00241
00242 void NearestNeighborSearchBasic(double *distortion);
00243
00244 void SplitCodewords(int currentSize,
00245 int numDesired,
00246 int scale);
00247
00248 void Perturb(double *oldCodeword,
00249 int scale,
00250 double *newCodeword);
00251
00252 CodebookMatrixOfDoubleType m_Codebook;
00253
00254
00255 CodebookMatrixOfDoubleType m_Centroid;
00256
00257 double m_Threshold;
00258 double m_OffsetAdd;
00259 double m_OffsetMultiply;
00260 int m_MaxSplitAttempts;
00261
00262
00263 bool m_ValidInCodebook;
00264 double m_DoubleMaximum;
00265 double m_OutputDistortion;
00266 int m_OutputNumberOfEmptyCells;
00267
00268 unsigned long m_VectorDimension;
00269 unsigned long m_NumberOfCodewords;
00270 unsigned long m_CurrentNumberOfCodewords;
00271
00272 CodebookMatrixOfIntegerType m_CodewordHistogram;
00273 CodebookMatrixOfDoubleType m_CodewordDistortion;
00274
00275 };
00276
00277
00278 }
00279
00280 #ifndef ITK_MANUAL_INSTANTIATION
00281 #include "itkImageKmeansModelEstimator.txx"
00282 #endif
00283
00284
00285
00286 #endif