Commit 88c2b29d authored by Cédric Traizet's avatar Cédric Traizet

ENH: add parameter to disable whitening in pca

parent 6cd71442
Pipeline #2523 canceled with stages
in 3 minutes and 54 seconds
......@@ -130,6 +130,10 @@ private:
AddParameter(ParameterType_OutputFilename, "method.pca.outeigenvalues", "Output file containing eigenvalues (txt format)");
SetParameterDescription("method.pca.outeigenvalues", "Output file containing eigenvalues (txt format).");
MandatoryOff("method.pca.outeigenvalues");
AddParameter(ParameterType_Bool, "method.pca.whiten", "Perform pca whitening");
SetParameterDescription("method.pca.whiten", "Perform whitening and ensure uncorrelated outputs with unit component wise variances");
SetParameterInt("method.pca.whiten", 1);
MandatoryOff("method.pca.whiten");
AddChoice("method.napca", "NA-PCA");
SetParameterDescription("method.napca", "Noise Adjusted Principal Component Analysis.");
......@@ -257,6 +261,7 @@ private:
filter->SetInput(GetParameterFloatVectorImage("in"));
filter->SetNumberOfPrincipalComponentsRequired(nbComp);
filter->SetWhitening(GetParameterInt("method.pca.whiten"));
// Center AND reduce the input data.
if (normalize)
......
......@@ -97,6 +97,9 @@ public:
*/
itkSetMacro(NumberOfPrincipalComponentsRequired, unsigned int);
itkGetMacro(NumberOfPrincipalComponentsRequired, unsigned int);
itkSetMacro(Whitening, bool);
itkGetMacro(Whitening, bool);
itkGetMacro(CovarianceEstimator, CovarianceEstimatorFilterType *);
itkGetMacro(Transformer, TransformFilterType *);
......@@ -197,6 +200,7 @@ protected:
bool m_GivenCovarianceMatrix;
bool m_GivenTransformationMatrix;
bool m_IsTransformationMatrixForward;
bool m_Whitening;
VectorType m_MeanValues;
VectorType m_StdDevValues;
......
......@@ -39,7 +39,7 @@ PCAImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation >
this->SetNumberOfRequiredInputs(1);
m_NumberOfPrincipalComponentsRequired = 0;
m_Whitening = true;
m_UseNormalization = false;
m_UseVarianceForNormalization = false;
m_GivenMeanValues = false;
......@@ -181,7 +181,7 @@ PCAImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation >
{
for (unsigned int c = 0; c < numberOfComponent; ++c)
{
m_CovarianceMatrix(r,c) = cov(r,c) / (std::sqrt(cov(r,r)) *std::sqrt( cov(c,c)));
m_CovarianceMatrix(r,c) = cov(r,c) / std::sqrt(cov(r,r)*cov(c,c));
}
}
}
......@@ -376,19 +376,23 @@ PCAImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation >
m_EigenValues.SetSize( m_NumberOfPrincipalComponentsRequired );
for ( unsigned int i = 0; i < m_NumberOfPrincipalComponentsRequired; ++i )
m_EigenValues[m_NumberOfPrincipalComponentsRequired - 1 - i] = static_cast< RealType >( valP(i, i) );
m_EigenValues[m_NumberOfPrincipalComponentsRequired - 1 - i] = static_cast< RealType >( vectValP[i] );
/* We used normalized PCA */
for ( unsigned int i = 0; i < valP.rows(); ++i )
{
if ( valP(i, i) > 0. )
{
valP(i, i) = 1. / std::sqrt( valP(i, i) );
if (m_Whitening)
valP(i, i) = 1. / std::sqrt( valP(i, i) );
}
else if ( valP(i, i) < 0. )
{
otbMsgDebugMacro( << "ValP(" << i << ") neg : " << valP(i, i) << " taking abs value" );
valP(i, i) = 1. / std::sqrt( std::abs( valP(i, i) ) );
if (m_Whitening)
valP(i, i) = 1. / std::sqrt( std::abs( valP(i, i) ) );
else
valP(i, i) = std::abs( valP(i, i));
}
else
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment