ITK
4.1.0
Insight Segmentation and Registration Toolkit
|
00001 /*========================================================================= 00002 * 00003 * Copyright Insight Software Consortium 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0.txt 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 * 00017 *=========================================================================*/ 00018 #ifndef __itkImageKmeansModelEstimator_h 00019 #define __itkImageKmeansModelEstimator_h 00020 00021 #include <time.h> 00022 #include <math.h> 00023 #include <float.h> 00024 00025 #include "vnl/vnl_vector.h" 00026 #include "vnl/vnl_matrix.h" 00027 #include "vnl/vnl_math.h" 00028 #include "vnl/algo/vnl_matrix_inverse.h" 00029 00030 #include "itkImageRegionIterator.h" 00031 #include "itkMacro.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 { 00129 template< class TInputImage, 00130 class TMembershipFunction > 00131 class ITK_EXPORT ImageKmeansModelEstimator: 00132 public ImageModelEstimatorBase< TInputImage, TMembershipFunction > 00133 { 00134 public: 00136 typedef ImageKmeansModelEstimator Self; 00137 typedef ImageModelEstimatorBase< TInputImage, TMembershipFunction > 00138 Superclass; 00139 00140 typedef SmartPointer< Self > Pointer; 00141 typedef SmartPointer< const Self > ConstPointer; 00142 00144 itkNewMacro(Self); 00145 00147 itkTypeMacro(ImageKmeansModelEstimator, ImageModelEstimatorBase); 00148 00150 typedef TInputImage InputImageType; 00151 typedef typename TInputImage::Pointer InputImagePointer; 00152 typedef typename TInputImage::ConstPointer InputImageConstPointer; 00153 00156 typedef typename TInputImage::PixelType::VectorType InputImageVectorType; 00157 00159 typedef typename TInputImage::PixelType InputImagePixelType; 00160 00162 typedef ImageRegionIterator< TInputImage > InputImageIterator; 00163 00164 typedef ImageRegionConstIterator< TInputImage > InputImageConstIterator; 00165 00167 typedef typename TMembershipFunction::Pointer MembershipFunctionPointer; 00168 00170 typedef vnl_matrix< double > CodebookMatrixOfDoubleType; 00171 00173 typedef vnl_matrix< int > CodebookMatrixOfIntegerType; 00174 00176 void SetCodebook(CodebookMatrixOfDoubleType InCodebook); 00177 00179 itkGetConstMacro(Codebook, CodebookMatrixOfDoubleType); 00180 00182 CodebookMatrixOfDoubleType GetOutCodebook() { return m_Codebook; } 00183 00185 itkSetMacro(Threshold, double); 00186 00188 itkGetConstMacro(Threshold, double); 00189 00191 itkSetMacro(OffsetAdd, double); 00192 00194 itkGetConstMacro(OffsetAdd, double); 00195 00197 itkSetMacro(OffsetMultiply, double); 00198 00200 itkGetConstMacro(OffsetMultiply, double); 00201 00203 itkSetMacro(MaxSplitAttempts, int); 00204 00206 itkGetConstMacro(MaxSplitAttempts, int); 00207 00209 CodebookMatrixOfDoubleType GetKmeansResults(void) { return m_Centroid; } 00210 protected: 00211 ImageKmeansModelEstimator(); 00212 ~ImageKmeansModelEstimator(); 00213 virtual void PrintSelf(std::ostream & os, Indent indent) const; 00215 00217 void GenerateData(); 00218 00220 void Allocate(); 00221 00223 void PrintKmeansAlgorithmResults(); 00224 00225 private: 00226 ImageKmeansModelEstimator(const Self &); //purposely not implemented 00227 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 00249 int WithoutCodebookUseLBG(); //LBG stands for the Lindo Buzo Gray Algorithm 00250 00251 void NearestNeighborSearchBasic(double *distortion); 00252 00253 void SplitCodewords(int currentSize, 00254 int numDesired, 00255 int scale); 00256 00257 void Perturb(double *oldCodeword, 00258 int scale, 00259 double *newCodeword); 00260 00261 CodebookMatrixOfDoubleType m_Codebook; 00262 00263 // Buffer for K-means calcualtions 00264 CodebookMatrixOfDoubleType m_Centroid; 00265 00266 double m_Threshold; 00267 double m_OffsetAdd; 00268 double m_OffsetMultiply; 00269 int m_MaxSplitAttempts; 00270 00271 //SizeValueType m_NumberOfModels; 00272 bool m_ValidInCodebook; 00273 double m_DoubleMaximum; 00274 double m_OutputDistortion; 00275 int m_OutputNumberOfEmptyCells; 00276 00277 SizeValueType m_VectorDimension; 00278 SizeValueType m_NumberOfCodewords; 00279 SizeValueType m_CurrentNumberOfCodewords; 00280 00281 CodebookMatrixOfIntegerType m_CodewordHistogram; 00282 CodebookMatrixOfDoubleType m_CodewordDistortion; 00283 }; // class ImageKmeansModelEstimator 00284 } // namespace itk 00285 00286 #ifndef ITK_MANUAL_INSTANTIATION 00287 #include "itkImageKmeansModelEstimator.hxx" 00288 #endif 00289 00290 #endif 00291