00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _freImageSampleCharacteristicsCalculator_txx
00023 #define _freImageSampleCharacteristicsCalculator_txx
00024
00025 #include "freImageSampleCharacteristicsCalculator.h"
00026
00027 #include "itkNumericTraits.h"
00028 #include "itkScalarImageToListAdaptor.h"
00029 #include "itkCovarianceCalculator.h"
00030 #include "itkMeanCalculator.h"
00031 #include "itkSubsample.h"
00032 #include "itkStatisticsAlgorithm.h"
00033 #include "itkScalarToArrayCastImageFilter.h"
00034
00035 namespace FREE
00036 {
00037
00038 template <class TImage>
00039 void
00040 ImageSampleCharacteristicsCalculator<TImage>::
00041 Compute()
00042 {
00043 if (m_smpImage.IsNull()) return;
00044
00045 typedef itk::Statistics::ScalarImageToListAdaptor< ImageType > SampleType;
00046 typename SampleType::Pointer sample = SampleType::New();
00047 sample->SetImage( m_smpImage );
00048
00049
00050 typedef itk::Statistics::CovarianceCalculator< SampleType > CovarianceAlgorithmType;
00051
00052 typename CovarianceAlgorithmType::Pointer covarianceAlgorithm = CovarianceAlgorithmType::New();
00053 covarianceAlgorithm->SetInputSample( sample );
00054 covarianceAlgorithm->SetMean( 0 );
00055 covarianceAlgorithm->Update();
00056
00057 m_Mean = covarianceAlgorithm->GetMean()->GetElement(0);
00058 const typename CovarianceAlgorithmType::OutputType* matrix = covarianceAlgorithm->GetOutput();
00059
00060 m_Variance = (*matrix)(0,0);
00061
00062
00063 typedef itk::Statistics::Subsample< SampleType > SubsampleType;
00064 typename SubsampleType::Pointer subsample = SubsampleType::New();
00065 subsample->SetSample( sample );
00066 subsample->InitializeWithAllInstances();
00067 m_Median = itk::Statistics::QuickSelect< SubsampleType >( subsample, 0, 0, subsample->Size(), subsample->Size()/2 );
00068 };
00069
00070 template <class TImage>
00071 void
00072 ImageSampleCharacteristicsCalculator<TImage>::
00073 ComputeMean()
00074 {
00075 typedef itk::Statistics::ImageToListAdaptor< ImageType > SampleType;
00076 typedef itk::Statistics::MeanCalculator< SampleType > MeanAlgorithmType;
00077
00078 if (m_smpImage.IsNull()) return;
00079
00080 typename SampleType::Pointer sample = SampleType::New();
00081 sample->SetImage(m_smpImage);
00082
00083 typename MeanAlgorithmType::Pointer meanAlgorithm = MeanAlgorithmType::New();
00084 meanAlgorithm->SetInputSample( sample );
00085 meanAlgorithm->Update();
00086
00087 m_Mean = meanAlgorithm->GetOutput();
00088 };
00089
00090 template <class TImage>
00091 void
00092 ImageSampleCharacteristicsCalculator<TImage>::
00093 ComputeVariance(const ResultType& mean)
00094 {
00095 if (m_smpImage.IsNull()) return;
00096
00097 typedef itk::Statistics::ScalarImageToListAdaptor< ImageType > SampleType;
00098 typename SampleType::Pointer sample = SampleType::New();
00099 sample->SetImage( m_smpImage );
00100
00101
00102 typedef itk::Statistics::CovarianceCalculator< SampleType > CovarianceAlgorithmType;
00103
00104 typename CovarianceAlgorithmType::Pointer covarianceAlgorithm = CovarianceAlgorithmType::New();
00105 covarianceAlgorithm->SetInputSample( sample );
00106 covarianceAlgorithm->SetMean( 0 );
00107 covarianceAlgorithm->Update();
00108
00109 const typename CovarianceAlgorithmType::OutputType* matrix = covarianceAlgorithm->GetOutput();
00110 m_Variance = (*matrix)(0,0);
00111 };
00112
00113 template <class TImage>
00114 void
00115 ImageSampleCharacteristicsCalculator<TImage>::
00116 ComputeMedian()
00117 {
00118 typedef itk::FixedArray< ResultType, 1 > MeasurementVectorType;
00119 typedef itk::Image< MeasurementVectorType, 2 > ArrayImageType;
00120 typedef itk::ScalarToArrayCastImageFilter< ImageType, ArrayImageType > CasterType;
00121
00122 if (m_smpImage.IsNull()) return;
00123
00124 typename CasterType::Pointer caster = CasterType::New();
00125 caster->SetInput( m_smpImage );
00126 caster->Update();
00127
00128 typedef itk::Statistics::ImageToListAdaptor< ArrayImageType > SampleType;
00129 typename SampleType::Pointer sample = SampleType::New();
00130 sample->SetImage( caster->GetOutput() );
00131
00132
00133 typedef itk::Statistics::Subsample< SampleType > SubsampleType;
00134 typename SubsampleType::Pointer subsample = SubsampleType::New();
00135 subsample->SetSample( sample );
00136 subsample->InitializeWithAllInstances();
00137 m_Median = itk::Statistics::QuickSelect< SubsampleType >( subsample, 0, 0, subsample->Size(), subsample->Size()/2 );
00138 };
00139
00140 template <class TImage>
00141 typename ImageSampleCharacteristicsCalculator<TImage>::ResultType
00142 ImageSampleCharacteristicsCalculator<TImage>::
00143 GetMean() const
00144 {
00145 return m_Mean;
00146 };
00147
00148 template <class TImage>
00149 typename ImageSampleCharacteristicsCalculator<TImage>::ResultType
00150 ImageSampleCharacteristicsCalculator<TImage>::
00151 GetVariance() const
00152 {
00153 return m_Variance;
00154 };
00155
00156 template <class TImage>
00157 typename ImageSampleCharacteristicsCalculator<TImage>::ValueType
00158 ImageSampleCharacteristicsCalculator<TImage>::
00159 GetMedian() const
00160 {
00161 return m_Median;
00162 };
00163
00164 template <class TImage>
00165 typename ImageSampleCharacteristicsCalculator<TImage>::ValueType
00166 ImageSampleCharacteristicsCalculator<TImage>::
00167 GetNativeMean() const
00168 {
00169 return m_Mean;
00170 };
00171
00172 template <class TImage>
00173 typename ImageSampleCharacteristicsCalculator<TImage>::ValueType
00174 ImageSampleCharacteristicsCalculator<TImage>::
00175 GetNativeVariance() const
00176 {
00177 return m_Variance;
00178 };
00179
00180 template <class TImage>
00181 void
00182 ImageSampleCharacteristicsCalculator<TImage>::
00183 SetImage(const ImageType* pField)
00184 {
00185 m_smpImage = pField;
00186 };
00187
00188 template <class TImage>
00189 ImageSampleCharacteristicsCalculator<TImage>::
00190 ImageSampleCharacteristicsCalculator()
00191 {
00192 m_Mean = itk::NumericTraits< ResultType >::Zero;
00193 m_Variance = itk::NumericTraits< ResultType >::Zero;
00194 m_Median = itk::NumericTraits< ValueType >::Zero;
00195 };
00196
00197 }
00198
00199 #endif