OTB  10.0.0
Orfeo Toolbox
otbPCAImageFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbPCAImageFilter_h
22 #define otbPCAImageFilter_h
23 
24 #include "otbMacro.h"
25 #include "otbMatrixImageFilter.h"
27 
28 
29 namespace otb
30 {
31 
32 namespace Transform
33 {
35 {
36  FORWARD = 0,
37  INVERSE = 1
38 };
39 }
40 
52 template <class TInputImage, class TOutputImage, Transform::TransformDirection TDirectionOfTransformation>
53 class ITK_EXPORT PCAImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>
54 {
55 public:
58  typedef itk::ImageToImageFilter<TInputImage, TOutputImage> Superclass;
59  typedef itk::SmartPointer<Self> Pointer;
60  typedef itk::SmartPointer<const Self> ConstPointer;
61 
63  itkNewMacro(Self);
64 
66  itkTypeMacro(PCAImageFilter, ImageToImageFilter);
67 
69  itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
70  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);
72 
74  itkStaticConstMacro(DirectionOfTransformation, TransformDirectionEnumType, TDirectionOfTransformation);
75 
77  typedef TInputImage InputImageType;
78  typedef TOutputImage OutputImageType;
79 
83 
87  typedef typename MatrixObjectType::ComponentType MatrixType;
88  typedef typename MatrixType::InternalMatrixType InternalMatrixType;
89  typedef typename InternalMatrixType::element_type MatrixElementType;
90 
93 
96 
102  itkSetMacro(NumberOfPrincipalComponentsRequired, unsigned int);
103  itkGetMacro(NumberOfPrincipalComponentsRequired, unsigned int);
105 
106  itkSetMacro(Whitening, bool);
107  itkGetMacro(Whitening, bool);
108 
109  itkGetMacro(CovarianceEstimator, CovarianceEstimatorFilterType*);
110  itkGetMacro(Transformer, TransformFilterType*);
111 
112  itkGetMacro(GivenCovarianceMatrix, bool);
114  {
115  if (m_GivenCovarianceMatrix)
116  return m_CovarianceMatrix;
117  else
118  return this->GetCovarianceEstimator()->GetCovariance();
119  }
120 
122  {
123  m_CovarianceMatrix = cov;
124  m_GivenCovarianceMatrix = true;
125  this->Modified();
126  }
127 
128  itkGetMacro(TransformationMatrix, MatrixType);
129  itkGetMacro(GivenTransformationMatrix, bool);
130  void SetTransformationMatrix(const MatrixType& transf, bool isForward = true)
131  {
132  m_TransformationMatrix = transf;
133  m_GivenTransformationMatrix = true;
134  m_IsTransformationMatrixForward = isForward;
135  this->Modified();
136  }
137 
138  itkGetConstMacro(EigenValues, VectorType);
139 
140  itkGetMacro(UseNormalization, bool);
141  void SetUseNormalization(bool norm)
142  {
143  m_UseNormalization = norm;
144  m_UseVarianceForNormalization = norm;
145  this->Modified();
146  }
147 
148  itkGetConstMacro(MeanValues, VectorType);
149  void SetMeanValues(const VectorType& data)
150  {
151  m_UseNormalization = true;
152  m_GivenMeanValues = true;
153  m_MeanValues = data;
154  this->Modified();
155  }
156 
157  itkGetConstMacro(UseVarianceForNormalization, bool);
158  itkSetMacro(UseVarianceForNormalization, bool);
159 
160  itkGetConstMacro(StdDevValues, VectorType);
161  void SetStdDevValues(const VectorType& vec)
162  {
163  m_UseNormalization = true;
164  m_UseVarianceForNormalization = true;
165  m_GivenStdDevValues = true;
166  m_StdDevValues = vec;
167  this->Modified();
168  }
169 
171  {
173  m_Normalizer->GetCovarianceEstimator()->SetUserIgnoredValue(value);
174  m_Normalizer->GetCovarianceEstimator()->SetIgnoreUserDefinedValue(true);
175 
177  m_CovarianceEstimator->SetUserIgnoredValue(value);
178  m_CovarianceEstimator->SetIgnoreUserDefinedValue(true);
179  }
181 
182 protected:
183  PCAImageFilter();
184  ~PCAImageFilter() override
185  {
186  }
187 
196  void GenerateOutputInformation() override;
197 
201  void GenerateData() override;
202 
203  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
204 
206  virtual void ForwardGenerateOutputInformation();
207  virtual void ReverseGenerateOutputInformation();
208  virtual void ForwardGenerateData();
209  virtual void ReverseGenerateData();
211 
212  void GenerateTransformationMatrix();
213 
224 
230 
234 
235 private:
236  PCAImageFilter(const Self&); // not implemented
237  void operator=(const Self&); // not implemented
238 };
239 
240 } // end of namespace otb
241 
242 #ifndef OTB_MANUAL_INSTANTIATION
243 #include "otbPCAImageFilter.hxx"
244 #endif
245 
246 #endif // otbPCAImageFilter_h
Apply a matrix multiplication over the channels of an image.
itk::SmartPointer< Self > Pointer
Normalize an VectorImage by setting its mean to zero and possibly variance to one (band by band).
Performs a Principal Component Analysis.
TransformFilterPointerType m_Transformer
TOutputImage OutputImageType
CovarianceEstimatorFilterPointerType m_CovarianceEstimator
TInputImage InputImageType
StreamingStatisticsVectorImageFilter< InputImageType > CovarianceEstimatorFilterType
itk::ImageToImageFilter< TInputImage, TOutputImage > Superclass
NormalizeVectorImageFilter< TInputImage, TOutputImage > NormalizeFilterType
InternalMatrixType::element_type MatrixElementType
void SetStatisticsUserIgnoredValue(RealType value)
MatrixType m_TransformationMatrix
CovarianceEstimatorFilterType::Pointer CovarianceEstimatorFilterPointerType
void SetCovarianceMatrix(const MatrixType &cov)
void SetTransformationMatrix(const MatrixType &transf, bool isForward=true)
void SetMeanValues(const VectorType &data)
MatrixType::InternalMatrixType InternalMatrixType
void SetStdDevValues(const VectorType &vec)
Transform::TransformDirection TransformDirectionEnumType
MatrixObjectType::ComponentType MatrixType
PCAImageFilter(const Self &)
MatrixType GetCovarianceMatrix() const
CovarianceEstimatorFilterType::RealPixelType VectorType
NormalizeFilterPointerType m_Normalizer
NormalizeFilterType::Pointer NormalizeFilterPointerType
CovarianceEstimatorFilterType::MatrixObjectType MatrixObjectType
itk::SmartPointer< const Self > ConstPointer
CovarianceEstimatorFilterType::RealType RealType
void SetUseNormalization(bool norm)
itk::SmartPointer< Self > Pointer
unsigned int m_NumberOfPrincipalComponentsRequired
MatrixImageFilter< TInputImage, TOutputImage > TransformFilterType
TransformFilterType::Pointer TransformFilterPointerType
void operator=(const Self &)
This class streams the whole input image through the PersistentStatisticsImageFilter.
Class to overload method passed to virtual pure in ITK V4.
Definition: otbTransform.h:42
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.