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: 2009-04-23 03:53:35 $
00007   Version:   $Revision: 1.12 $
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>
00134                                       Superclass;
00135 
00136   typedef SmartPointer<Self>          Pointer;
00137   typedef SmartPointer<const Self>    ConstPointer;
00138 
00140   itkNewMacro(Self);
00141 
00143   itkTypeMacro(ImageKmeansModelEstimator, ImageModelEstimatorBase);
00144 
00146   typedef TInputImage                           InputImageType;
00147   typedef typename TInputImage::Pointer         InputImagePointer;
00148   typedef typename TInputImage::ConstPointer    InputImageConstPointer;
00149 
00152   typedef typename TInputImage::PixelType::VectorType InputImageVectorType;
00153 
00155   typedef typename TInputImage::PixelType     InputImagePixelType;
00156 
00158   typedef ImageRegionIterator<TInputImage> InputImageIterator;
00159 
00160   typedef ImageRegionConstIterator<TInputImage> InputImageConstIterator;
00161 
00163   typedef typename TMembershipFunction::Pointer MembershipFunctionPointer;
00164 
00166   typedef vnl_matrix<double> CodebookMatrixOfDoubleType; 
00167 
00169   typedef vnl_matrix<int>    CodebookMatrixOfIntegerType;
00170 
00172   void SetCodebook(CodebookMatrixOfDoubleType InCodebook);
00173 
00175   itkGetConstMacro(Codebook,CodebookMatrixOfDoubleType);
00176 
00178   CodebookMatrixOfDoubleType GetOutCodebook()
00179     { return m_Codebook; }
00180 
00182   itkSetMacro(Threshold,double);
00183 
00185   itkGetConstMacro(Threshold,double);
00186 
00188   itkSetMacro(OffsetAdd,double);
00189 
00191   itkGetConstMacro(OffsetAdd,double);
00192 
00194   itkSetMacro(OffsetMultiply,double);
00195 
00197   itkGetConstMacro(OffsetMultiply,double);
00198 
00200   itkSetMacro(MaxSplitAttempts,int);
00201 
00203   itkGetConstMacro(MaxSplitAttempts,int);
00204 
00206   CodebookMatrixOfDoubleType GetKmeansResults(void)
00207     { return m_Centroid; }
00208 
00209 protected: 
00210   ImageKmeansModelEstimator();
00211   ~ImageKmeansModelEstimator();
00212   virtual void PrintSelf(std::ostream& os, Indent indent) const;
00213 
00215   void GenerateData();
00216 
00218   void Allocate();
00219 
00221   void PrintKmeansAlgorithmResults();
00222 private:
00223   ImageKmeansModelEstimator(const Self&); //purposely not implemented
00224   void operator=(const Self&); //purposely not implemented
00226 
00233   virtual void EstimateModels();
00234 
00235   void EstimateKmeansModelParameters();
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   //Local functions
00245   int  WithCodebookUseGLA(); // GLA stands for the Generalized Lloyd Algorithm
00246   int  WithoutCodebookUseLBG(); //LBG stands for the Lindo Buzo Gray Algorithm
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   // Buffer for K-means calcualtions
00261   CodebookMatrixOfDoubleType  m_Centroid;
00262 
00263   double              m_Threshold;
00264   double              m_OffsetAdd;
00265   double              m_OffsetMultiply;
00266   int                 m_MaxSplitAttempts;
00267 
00268   //unsigned long       m_NumberOfModels;
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 }; // class ImageKmeansModelEstimator
00282 
00283 
00284 } // namespace itk
00285 
00286 #ifndef ITK_MANUAL_INSTANTIATION
00287 #include "itkImageKmeansModelEstimator.txx"
00288 #endif
00289 
00290 #endif
00291 

Generated at Fri Apr 16 18:35:35 2010 for ITK by doxygen 1.6.1 written by Dimitri van Heesch, © 1997-2000