Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkImageKmeansModelEstimator.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkImageKmeansModelEstimator.h,v $
00005   Language:  C++
00006   Date:      $Date: 2004/11/12 00:54:30 $
00007   Version:   $Revision: 1.10 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
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&); //purposely not implemented
00226   void operator=(const Self&); //purposely not implemented
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   //Local functions
00247   int  WithCodebookUseGLA(); // GLA stands for the Generalized Lloyd Algorithm
00248   int  WithoutCodebookUseLBG(); //LBG stands for the Lindo Buzo Gray Algorithm
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   // Buffer for K-means calcualtions
00263   CodebookMatrixOfDoubleType  m_Centroid;
00264 
00265   double              m_Threshold;
00266   double              m_OffsetAdd;
00267   double              m_OffsetMultiply;
00268   int                 m_MaxSplitAttempts;
00269 
00270   //unsigned long       m_NumberOfModels;
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 }; // class ImageKmeansModelEstimator
00284 
00285 
00286 } // namespace itk
00287 
00288 #ifndef ITK_MANUAL_INSTANTIATION
00289 #include "itkImageKmeansModelEstimator.txx"
00290 #endif
00291 
00292 
00293 
00294 #endif
00295 

Generated at Wed Nov 5 22:01:06 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000