diff --git a/Code/BasicFilters/otbNormalizeVectorImageFilter.h b/Code/BasicFilters/otbNormalizeVectorImageFilter.h index 4f6e5bda6203631e8a6517f84fdf7e859c4b35f6..7acc859845dd45cc1d1b50fb9d8912f02550bed4 100644 --- a/Code/BasicFilters/otbNormalizeVectorImageFilter.h +++ b/Code/BasicFilters/otbNormalizeVectorImageFilter.h @@ -26,6 +26,8 @@ #include <otbVectorImage.h> #include <otbUnaryFunctorVectorImageFilter.h> +#include <otbStreamingStatisticsVectorImageFilter2.h> + namespace otb { namespace Functor @@ -153,10 +155,15 @@ public: typedef typename itk::NumericTraits< typename TInputImage::PixelType >::RealType RealVectorType; typedef typename itk::NumericTraits< typename RealVectorType::ValueType >::RealType RealType; + typedef StreamingStatisticsVectorImageFilter2< InputImageType > CovarianceEstimatorFilterType; + typedef typename CovarianceEstimatorFilterType::Pointer CovarianceEstimatorFilterPointerType; + + itkGetConstMacro(CovarianceEstimator,CovarianceEstimatorFilterType*); + template < class T > void SetMean ( const itk::VariableLengthVector<T> & m ) { - this->m_Functor->SetMean( m ); + this->GetFunctor().SetMean( m ); m_IsGivenMean = true; m_UseMean = true; this->Modified(); @@ -165,7 +172,7 @@ public: template < class T > void SetStdDev ( const itk::VariableLengthVector<T> & sigma ) { - this->m_Functor->SetStdDev( sigma ); + this->GetFunctor().SetStdDev( sigma ); m_IsGivenStdDev = true; m_UseStdDev = true; this->Modified(); @@ -174,7 +181,7 @@ public: template < class T > void SetVariance ( const itk::VariableLengthVector<T> & var ) { - this->m_Functor->SetVariance( var ); + this->GetFunctor().SetVariance( var ); m_IsGivenStdDev = true; m_UseStdDev = true; this->Modified(); @@ -199,6 +206,9 @@ private: bool m_UseMean; bool m_UseStdDev; + + CovarianceEstimatorFilterPointerType m_CovarianceEstimator; + }; // end of class NormalizeVectorImageFilter } // end of namespace otb diff --git a/Code/BasicFilters/otbNormalizeVectorImageFilter.txx b/Code/BasicFilters/otbNormalizeVectorImageFilter.txx index ee3039a9b96074b3e8bdb598afa8f0da2c08142e..6d2d4d8228adf591368730226e5ef45a86eef9e3 100644 --- a/Code/BasicFilters/otbNormalizeVectorImageFilter.txx +++ b/Code/BasicFilters/otbNormalizeVectorImageFilter.txx @@ -19,8 +19,6 @@ #define __otbNormalizeVectorImageFilter_txx #include "otbNormalizeVectorImageFilter.h" -#include "otbStreamingStatisticsVectorImageFilter2.h" - namespace otb { @@ -33,6 +31,8 @@ NormalizeVectorImageFilter< TInputImage, TOutputImage > m_UseMean = true; m_UseStdDev = true; + + m_CovarianceEstimator = CovarianceEstimatorFilterType::New(); } template < class TInputImage, class TOutputImage > @@ -56,19 +56,17 @@ NormalizeVectorImageFilter< TInputImage, TOutputImage > if ( !m_IsGivenMean ) { - typedef StreamingStatisticsVectorImageFilter2< InputImageType > CovarianceEstimatorFilterType; - typename CovarianceEstimatorFilterType::Pointer estimator = CovarianceEstimatorFilterType::New(); - estimator->SetInput( const_cast<InputImageType*>( this->GetInput() ) ); - estimator->Update(); + m_CovarianceEstimator->SetInput( const_cast<InputImageType*>( this->GetInput() ) ); + m_CovarianceEstimator->Update(); - this->GetFunctor().SetMean( estimator->GetMean() ); + this->GetFunctor().SetMean( m_CovarianceEstimator->GetMean() ); if ( !m_IsGivenStdDev && m_UseStdDev ) { typename StreamingStatisticsVectorImageFilter2< InputImageType >::RealPixelType sigma ( this->GetInput()->GetNumberOfComponentsPerPixel() ); for ( unsigned int i = 0; i < sigma.Size(); i++ ) - sigma[i] = vcl_sqrt( estimator->GetCovariance()(i,i) ); + sigma[i] = vcl_sqrt( m_CovarianceEstimator->GetCovariance()(i,i) ); this->GetFunctor().SetStdDev( sigma ); } diff --git a/Code/Hyperspectral/otbMNFImageFilter.h b/Code/Hyperspectral/otbMNFImageFilter.h index 7647958aadf5dcd525543957cea46f81b109712f..c6fc3744648bdb55184a400a9fb9abacb5f8c0b4 100644 --- a/Code/Hyperspectral/otbMNFImageFilter.h +++ b/Code/Hyperspectral/otbMNFImageFilter.h @@ -107,6 +107,15 @@ public: itkGetMacro(Transformer, TransformFilterType *); itkGetMacro(NoiseImageFilter, NoiseImageFilterType *); + VectorType GetMeanValues () const + { + return this->GetPCAImageFilter()->GetMeanValues(); + } + void SetMeanValues ( const VectorType & vec ) + { + this->GetPCAImageFilter()->SetMeanValues( vec ); + } + MatrixType GetCovarianceMatrix () const { return this->GetPCAImageFilter()->GetGovarianceMatrix(); diff --git a/Code/Hyperspectral/otbPCAImageFilter.txx b/Code/Hyperspectral/otbPCAImageFilter.txx index 72261831edfb23b380490c1fbf47eec19e1b20a3..15f7f25000a0ac8283c3c4fa8e19a907222ba04f 100644 --- a/Code/Hyperspectral/otbPCAImageFilter.txx +++ b/Code/Hyperspectral/otbPCAImageFilter.txx @@ -142,9 +142,17 @@ PCAImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation > m_Normalizer->Update(); if ( !m_GivenMeanValues ) - m_MeanValues = m_Normalizer->GetFunctor().GetMean(); + { + m_MeanValues = m_Normalizer->GetCovarianceEstimator()->GetMean(); + m_CovarianceMatrix = m_Normalizer->GetCovarianceEstimator()->GetCovariance(); + } + else + { + m_CovarianceEstimator->SetInput( m_Normalizer->GetOutput() ); + m_CovarianceEstimator->Update(); - m_CovarianceMatrix = m_CovarianceEstimator->GetCovariance(); + m_CovarianceMatrix = m_CovarianceEstimator->GetCovariance(); + } m_Transformer->SetInput( m_Normalizer->GetOutput() ); } diff --git a/Testing/otbMNFImageFilter.cxx b/Testing/otbMNFImageFilter.cxx index ba1ca21cfb40945df0ae9b2429e79db1568065a1..ded760535ea6a80adc8118c18b37a89ff9bf1736 100644 --- a/Testing/otbMNFImageFilter.cxx +++ b/Testing/otbMNFImageFilter.cxx @@ -128,6 +128,7 @@ int otbMNFImageFilterTest ( int argc, char* argv[] ) NoiseFilterType, otb::Transform::INVERSE > InvFilterType; InvFilterType::Pointer invFilter = InvFilterType::New(); invFilter->SetInput( filter->GetOutput() ); + invFilter->SetMeanValues( filter->GetMeanValues() ); invFilter->SetTransformationMatrix( filter->GetTransformationMatrix() ); invFilter->SetNoiseTransformationMatrix( filter->GetNoiseTransformationMatrix() );