Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
otb
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Youssefi
otb
Commits
95025b55
Commit
95025b55
authored
14 years ago
by
Grégoire Mercier
Browse files
Options
Downloads
Patches
Plain Diff
ENL: Use mean and variance in the PCA normalization
parent
faa7c58a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Code/Hyperspectral/otbPCAImageFilter.h
+15
-0
15 additions, 0 deletions
Code/Hyperspectral/otbPCAImageFilter.h
Code/Hyperspectral/otbPCAImageFilter.txx
+44
-10
44 additions, 10 deletions
Code/Hyperspectral/otbPCAImageFilter.txx
Testing/otbPCAImageFilter.cxx
+5
-0
5 additions, 0 deletions
Testing/otbPCAImageFilter.cxx
with
64 additions
and
10 deletions
Code/Hyperspectral/otbPCAImageFilter.h
+
15
−
0
View file @
95025b55
...
...
@@ -132,6 +132,18 @@ public:
this
->
Modified
();
}
itkGetConstMacro
(
UseVarianceForNormalization
,
bool
);
itkSetMacro
(
UseVarianceForNormalization
,
bool
);
itkGetConstMacro
(
StdDevValues
,
VectorType
);
void
SetStdDevValues
(
const
VectorType
&
vec
)
{
m_GivenStdDevValues
=
true
;
m_UseVarianceForNormalization
=
true
;
m_StdDevValues
=
vec
;
this
->
Modified
();
}
protected
:
PCAImageFilter
();
virtual
~
PCAImageFilter
()
{
}
...
...
@@ -162,11 +174,14 @@ protected:
/** Internal attributes */
unsigned
int
m_NumberOfPrincipalComponentsRequired
;
bool
m_GivenMeanValues
;
bool
m_GivenStdDevValues
;
bool
m_GivenCovarianceMatrix
;
bool
m_GivenTransformationMatrix
;
bool
m_IsTransformationMatrixForward
;
bool
m_UseVarianceForNormalization
;
VectorType
m_MeanValues
;
VectorType
m_StdDevValues
;
MatrixType
m_CovarianceMatrix
;
VectorType
m_EigenValues
;
MatrixType
m_TransformationMatrix
;
...
...
This diff is collapsed.
Click to expand it.
Code/Hyperspectral/otbPCAImageFilter.txx
+
44
−
10
View file @
95025b55
...
...
@@ -36,9 +36,11 @@ PCAImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation >
m_NumberOfPrincipalComponentsRequired = 0;
m_GivenMeanValues = false;
m_GivenStdDevValues = false;
m_GivenCovarianceMatrix = false;
m_GivenTransformationMatrix = false;
m_IsTransformationMatrixForward = true;
m_UseVarianceForNormalization = false;
m_CovarianceEstimator = CovarianceEstimatorFilterType::New();
m_Transformer = TransformFilterType::New();
...
...
@@ -107,6 +109,7 @@ void
PCAImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation >
::GenerateData ()
{
std::cerr << __PRETTY_FUNCTION__ << "\n";
switch ( DirectionOfTransformation )
{
case Transform::FORWARD:
...
...
@@ -134,17 +137,27 @@ PCAImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation >
if ( !m_GivenCovarianceMatrix )
{
m_Normalizer->SetInput( inputImgPtr );
m_Normalizer->SetUseStdDev(
false
);
m_Normalizer->SetUseStdDev(
m_UseVarianceForNormalization
);
if ( m_GivenMeanValues )
m_Normalizer->SetMean( m_MeanValues );
if ( m_GivenStdDevValues )
m_Normalizer->SetStdDev( m_StdDevValues );
m_Normalizer->Update();
if ( !m_GivenMeanValues )
{
m_MeanValues = m_Normalizer->GetCovarianceEstimator()->GetMean();
m_CovarianceMatrix = m_Normalizer->GetCovarianceEstimator()->GetCovariance();
if ( !m_GivenStdDevValues )
m_StdDevValues = m_Normalizer->GetFunctor().GetStdDev();
if ( m_UseVarianceForNormalization )
m_CovarianceMatrix = m_Normalizer->GetCovarianceEstimator()->GetCorrelation();
else
m_CovarianceMatrix = m_Normalizer->GetCovarianceEstimator()->GetCovariance();
}
else
{
...
...
@@ -203,7 +216,8 @@ PCAImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation >
}
else if ( m_IsTransformationMatrixForward )
{
std::cerr << "Transposition\n";
// prevents from multiple transpositions...
m_IsTransformationMatrixForward = false;
m_TransformationMatrix = m_TransformationMatrix.GetTranspose();
}
...
...
@@ -217,15 +231,35 @@ PCAImageFilter< TInputImage, TOutputImage, TDirectionOfTransformation >
m_Transformer->SetInput( this->GetInput() );
m_Transformer->SetMatrix( m_TransformationMatrix.GetVnlMatrix() );
if ( m_GivenMeanValues )
if ( m_GivenMeanValues
|| m_GivenStdDevValues
)
{
VectorType revMean ( m_MeanValues.Size() );
for ( unsigned int i = 0; i < m_MeanValues.Size(); i++ )
revMean[i] = -m_MeanValues[i];
m_Normalizer->SetInput( m_Transformer->GetOutput() );
m_Normalizer->SetMean( revMean );
m_Normalizer->SetUseStdDev( false );
if ( m_GivenStdDevValues )
{
VectorType revStdDev ( m_StdDevValues.Size() );
for ( unsigned int i = 0; i < m_StdDevValues.Size(); i++ )
revStdDev[i] = 1. / m_StdDevValues[i];
m_Normalizer->SetStdDev( revStdDev );
}
if ( m_GivenMeanValues )
{
VectorType revMean ( m_MeanValues.Size() );
if ( m_GivenStdDevValues )
{
for ( unsigned int i = 0; i < m_MeanValues.Size(); i++ )
revMean[i] = - m_MeanValues[i] / m_StdDevValues[i];
m_Normalizer->SetUseStdDev( true );
}
else
{
for ( unsigned int i = 0; i < m_MeanValues.Size(); i++ )
revMean[i] = -m_MeanValues[i];
m_Normalizer->SetUseStdDev( false );
}
m_Normalizer->SetMean( revMean );
}
m_Normalizer->GraftOutput( this->GetOutput() );
m_Normalizer->Update();
...
...
This diff is collapsed.
Click to expand it.
Testing/otbPCAImageFilter.cxx
+
5
−
0
View file @
95025b55
...
...
@@ -47,6 +47,7 @@ int otbPCAImageFilterTest ( int argc, char* argv[] )
parser
->
AddInputImage
();
parser
->
AddOption
(
"--NumComponents"
,
"Number of components to keep for output"
,
"-n"
,
1
,
false
);
parser
->
AddOption
(
"--Inverse"
,
"Performs also the inverse transformation (give the output name)"
,
"-inv"
,
1
,
false
);
parser
->
AddOption
(
"--NormalizeVariance"
,
"center AND reduce data before PCA"
,
"-norm"
,
0
,
false
);
parser
->
AddOutputImage
();
typedef
otb
::
CommandLineArgumentParseResult
ParserResultType
;
...
...
@@ -73,6 +74,7 @@ int otbPCAImageFilterTest ( int argc, char* argv[] )
const
char
*
outputImageName
=
parseResult
->
GetOutputImage
().
c_str
();
const
unsigned
int
nbComponents
=
parseResult
->
IsOptionPresent
(
"--NumComponents"
)
?
parseResult
->
GetParameterUInt
(
"--NumComponents"
)
:
0
;
const
bool
normalization
=
parseResult
->
IsOptionPresent
(
"--NormalizeVariance"
);
// Main type definition
const
unsigned
int
Dimension
=
2
;
...
...
@@ -89,6 +91,7 @@ int otbPCAImageFilterTest ( int argc, char* argv[] )
FilterType
::
Pointer
filter
=
FilterType
::
New
();
filter
->
SetInput
(
reader
->
GetOutput
()
);
filter
->
SetNumberOfPrincipalComponentsRequired
(
nbComponents
);
filter
->
SetUseVarianceForNormalization
(
normalization
);
typedef
otb
::
CommandProgressUpdate
<
FilterType
>
CommandType
;
CommandType
::
Pointer
observer
=
CommandType
::
New
();
...
...
@@ -108,6 +111,8 @@ int otbPCAImageFilterTest ( int argc, char* argv[] )
InvFilterType
::
Pointer
invFilter
=
InvFilterType
::
New
();
invFilter
->
SetInput
(
filter
->
GetOutput
()
);
invFilter
->
SetMeanValues
(
filter
->
GetMeanValues
()
);
if
(
normalization
)
invFilter
->
SetStdDevValues
(
filter
->
GetStdDevValues
()
);
invFilter
->
SetTransformationMatrix
(
filter
->
GetTransformationMatrix
()
);
typedef
otb
::
CommandProgressUpdate
<
InvFilterType
>
CommandType2
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment