00001 /*========================================================================= 00002 00003 Program: F.R.E.E. - flexible registration evaluation engine 00004 Version: v.1.0.0 00005 Date: $Date: 2006/09/01 12:00:00 $ 00006 Module: $RCSfile: freVectorSampleCharacteristicsCalculator.txx,v $ 00007 Language: C++ 00008 00009 00010 00011 Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics, 00012 Institute for Medical Biometry and Informatics, University of Heidelberg, 00013 Germany). All rights reserved. 00014 See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm 00015 for details. 00016 00017 This software is distributed WITHOUT ANY WARRANTY; without even 00018 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00019 PURPOSE. See the above copyright notices for more information. 00020 00021 =========================================================================*/ 00022 #ifndef _freVectorSampleCharacteristicsCalculator_txx 00023 #define _freVectorSampleCharacteristicsCalculator_txx 00024 00025 #include "freVectorSampleCharacteristicsCalculator.h" 00026 00027 #include "itkNumericTraits.h" 00028 #include "itkImageToListAdaptor.h" 00029 #include "itkCovarianceCalculator.h" 00030 #include "itkMeanCalculator.h" 00031 00032 namespace FREE 00033 { 00034 00035 template <class TVectorField> 00036 void 00037 VectorSampleCharacteristicsCalculator<TVectorField>:: 00038 Compute() 00039 { 00040 typedef itk::Statistics::ImageToListAdaptor< VectorFieldType > SampleType; 00041 typedef itk::Statistics::CovarianceCalculator< SampleType > CovarianceAlgorithmType; 00042 00043 if (m_smpImage.IsNull()) return; 00044 00045 typename SampleType::Pointer sample = SampleType::New(); 00046 sample->SetImage(m_smpImage); 00047 00048 typename CovarianceAlgorithmType::Pointer covarianceAlgorithm = CovarianceAlgorithmType::New(); 00049 covarianceAlgorithm->SetInputSample( sample ); 00050 covarianceAlgorithm->Update(); 00051 00052 m_Mean = *(covarianceAlgorithm->GetMean()); 00053 m_CovarianceMatrix = *(covarianceAlgorithm->GetOutput()); 00054 m_iMatrixSize = m_CovarianceMatrix.Cols();//must be set here because the itk implementation is non const and cannot be used in GetVariance() 00055 }; 00056 00057 template <class TVectorField> 00058 void 00059 VectorSampleCharacteristicsCalculator<TVectorField>:: 00060 ComputeMean() 00061 { 00062 typedef itk::Statistics::ImageToListAdaptor< VectorFieldType > SampleType; 00063 typedef itk::Statistics::MeanCalculator< SampleType > MeanAlgorithmType; 00064 00065 if (m_smpImage.IsNull()) return; 00066 00067 typename SampleType::Pointer sample = SampleType::New(); 00068 sample->SetImage(m_smpImage); 00069 00070 typename MeanAlgorithmType::Pointer meanAlgorithm = MeanAlgorithmType::New(); 00071 meanAlgorithm->SetInputSample( sample ); 00072 meanAlgorithm->Update(); 00073 00074 m_Mean = *(meanAlgorithm->GetOutput()); 00075 }; 00076 00077 template <class TVectorField> 00078 void 00079 VectorSampleCharacteristicsCalculator<TVectorField>:: 00080 ComputeCovariance(const MeanType& mean) 00081 { 00082 typedef itk::Statistics::ImageToListAdaptor< VectorFieldType > SampleType; 00083 typedef itk::Statistics::CovarianceCalculator< SampleType > CovarianceAlgorithmType; 00084 00085 if (m_smpImage.IsNull()) return; 00086 00087 typename SampleType::Pointer sample = SampleType::New(); 00088 sample->SetImage(m_smpImage); 00089 00090 m_Mean = mean; 00091 00092 typename CovarianceAlgorithmType::Pointer covarianceAlgorithm = CovarianceAlgorithmType::New(); 00093 covarianceAlgorithm->SetInputSample( sample ); 00094 covarianceAlgorithm->SetMean( &m_Mean ); 00095 covarianceAlgorithm->Update(); 00096 00097 m_CovarianceMatrix = *(covarianceAlgorithm->GetOutput()); 00098 m_iMatrixSize = m_CovarianceMatrix.Cols();//must be set here because the itk implementation is non const and cannot be used in GetVariance() 00099 }; 00100 00101 template <class TVectorField> 00102 typename VectorSampleCharacteristicsCalculator<TVectorField>::MeanType 00103 VectorSampleCharacteristicsCalculator<TVectorField>:: 00104 GetMean() const 00105 { 00106 return m_Mean; 00107 }; 00108 00109 template <class TVectorField> 00110 typename VectorSampleCharacteristicsCalculator<TVectorField>::MeanType 00111 VectorSampleCharacteristicsCalculator<TVectorField>:: 00112 GetVariance() const 00113 { 00114 MeanType variance(m_iMatrixSize); 00115 00116 for (unsigned int i = 0; i<m_iMatrixSize; i++) variance[i] = m_CovarianceMatrix(i,i); 00117 00118 return variance; 00119 }; 00120 00121 template <class TVectorField> 00122 typename VectorSampleCharacteristicsCalculator<TVectorField>::CovarianceMatrixType 00123 VectorSampleCharacteristicsCalculator<TVectorField>:: 00124 GetCovariance() const 00125 { 00126 return m_CovarianceMatrix; 00127 }; 00128 00129 template <class TVectorField> 00130 void 00131 VectorSampleCharacteristicsCalculator<TVectorField>:: 00132 SetVectorField(const VectorFieldType* pField) 00133 { 00134 m_smpImage = pField; 00135 }; 00136 00137 template <class TVectorField> 00138 VectorSampleCharacteristicsCalculator<TVectorField>:: 00139 VectorSampleCharacteristicsCalculator() 00140 { 00141 typedef typename VectorType::ValueType PixelComponentType; 00142 typedef typename itk::NumericTraits< PixelComponentType >::RealType PixelComponentRealType; 00143 00144 m_Mean.Fill( itk::NumericTraits< PixelComponentRealType >::Zero ); 00145 m_CovarianceMatrix.Fill( itk::NumericTraits< PixelComponentRealType >::Zero ); 00146 m_iMatrixSize = 0; 00147 }; 00148 00149 } // namespace free 00150 00151 #endif
1.5.3 written by Dimitri van Heesch,
© 1997-2000