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