File: otbPCAImageFilter.h

package info (click to toggle)
otb 5.8.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,496 kB
  • ctags: 40,282
  • sloc: cpp: 306,573; ansic: 3,575; python: 450; sh: 214; perl: 74; java: 72; makefile: 70
file content (219 lines) | stat: -rw-r--r-- 7,295 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef otbPCAImageFilter_h
#define otbPCAImageFilter_h

#include "otbMacro.h"
#include "otbMatrixImageFilter.h"
#include "otbNormalizeVectorImageFilter.h"


namespace otb
{

  namespace Transform
  {
    enum TransformDirection {FORWARD = 0, INVERSE = 1};
  }

/** \class PCAImageFilter
 * \brief Performs a Principal Component Analysis
 *
 * The internal structure of this filter is a filter-to-filter like structure.
 * The estimation of the covariance matrix has persistent capabilities...
 *
 * \sa otbStreamingStatisticsVectorImageFilter
 * \sa MatrixMultiplyImageFilter
 *
 * \ingroup OTBDimensionalityReduction
 */
template <class TInputImage, class TOutputImage, Transform::TransformDirection TDirectionOfTransformation >
class ITK_EXPORT PCAImageFilter
  : public itk::ImageToImageFilter<TInputImage, TOutputImage>
{
public:
  /** Standard typedefs */
  typedef PCAImageFilter                                     Self;
  typedef itk::ImageToImageFilter<TInputImage, TOutputImage> Superclass;
  typedef itk::SmartPointer<Self>                            Pointer;
  typedef itk::SmartPointer<const Self>                      ConstPointer;

  /** Type macro */
  itkNewMacro(Self);

  /** Creation through object factory macro */
  itkTypeMacro(PCAImageFilter, ImageToImageFilter);

  /** Dimension */
  itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);

  typedef Transform::TransformDirection TransformDirectionEnumType;
  itkStaticConstMacro(DirectionOfTransformation, TransformDirectionEnumType, TDirectionOfTransformation);

  /** Template parameters typedefs */
  typedef TInputImage  InputImageType;
  typedef TOutputImage OutputImageType;

  /** Filter types and related */
  typedef StreamingStatisticsVectorImageFilter< InputImageType > CovarianceEstimatorFilterType;
  typedef typename CovarianceEstimatorFilterType::Pointer        CovarianceEstimatorFilterPointerType;

  typedef typename CovarianceEstimatorFilterType::RealType         RealType;
  typedef typename CovarianceEstimatorFilterType::RealPixelType    VectorType;
  typedef typename CovarianceEstimatorFilterType::MatrixObjectType MatrixObjectType;
  typedef typename MatrixObjectType::ComponentType                 MatrixType;
  typedef typename MatrixType::InternalMatrixType                  InternalMatrixType;
  typedef typename InternalMatrixType::element_type                MatrixElementType;

  typedef MatrixImageFilter< TInputImage, TOutputImage > TransformFilterType;
  typedef typename TransformFilterType::Pointer          TransformFilterPointerType;

  typedef NormalizeVectorImageFilter< TInputImage, TOutputImage > NormalizeFilterType;
  typedef typename NormalizeFilterType::Pointer                   NormalizeFilterPointerType;

  /**
   * Set/Get the number of required largest principal components.
   * The filter produces the required number of principal components plus one outputs.
   * Output index 0 represents the mean image and the remaining outputs the requested principal components.
   */
  itkSetMacro(NumberOfPrincipalComponentsRequired, unsigned int);
  itkGetMacro(NumberOfPrincipalComponentsRequired, unsigned int);

  itkGetMacro(CovarianceEstimator, CovarianceEstimatorFilterType *);
  itkGetMacro(Transformer, TransformFilterType *);

  itkGetMacro(GivenCovarianceMatrix, bool);
  MatrixType GetCovarianceMatrix () const
  {
    if ( m_GivenCovarianceMatrix )
      return m_CovarianceMatrix;
    else
      return this->GetCovarianceEstimator()->GetCovariance();
  }

  void SetCovarianceMatrix ( const MatrixType & cov )
  {
    m_CovarianceMatrix = cov;
    m_GivenCovarianceMatrix = true;
    this->Modified();
  }

  itkGetMacro(TransformationMatrix, MatrixType);
  itkGetMacro(GivenTransformationMatrix, bool);
  void SetTransformationMatrix( const MatrixType & transf, bool isForward = true )
  {
    m_TransformationMatrix = transf;
    m_GivenTransformationMatrix = true;
    m_IsTransformationMatrixForward = isForward;
    this->Modified();
  }

  itkGetConstMacro(EigenValues, VectorType);

  itkGetMacro(UseNormalization, bool);
  void SetUseNormalization ( bool norm )
  {
    m_UseNormalization = norm;
    m_UseVarianceForNormalization = norm;
    this->Modified();
  }

  itkGetConstMacro(MeanValues, VectorType);
  void SetMeanValues ( const VectorType & data )
  {
    m_UseNormalization = true;
    m_GivenMeanValues = true;
    m_MeanValues = data;
    this->Modified();
  }

  itkGetConstMacro(UseVarianceForNormalization, bool);
  itkSetMacro(UseVarianceForNormalization, bool);

  itkGetConstMacro(StdDevValues, VectorType);
  void SetStdDevValues ( const VectorType & vec )
  {
    m_UseNormalization = true;
    m_UseVarianceForNormalization = true;
    m_GivenStdDevValues = true;
    m_StdDevValues = vec;
    this->Modified();
  }

protected:
  PCAImageFilter();
  ~PCAImageFilter() ITK_OVERRIDE { }

  /** GenerateOutputInformation
   * Propagate vector length info and modify if needed
   * NumberOfPrincipalComponentsRequired
   *
   * In REVERSE mode, the covariance matrix or the transformation matrix
   * (which may not be square) has to be given,
   * otherwize, GenerateOutputInformation throws an itk::ExceptionObject
   */
  void GenerateOutputInformation() ITK_OVERRIDE;

  /** GenerateData
   * Through a filter of filter structure
   */
  void GenerateData () ITK_OVERRIDE;

  void PrintSelf(std::ostream& os, itk::Indent indent) const ITK_OVERRIDE;

  /** Internal methods */
  virtual void ForwardGenerateOutputInformation();
  virtual void ReverseGenerateOutputInformation();
  virtual void ForwardGenerateData();
  virtual void ReverseGenerateData();
  
  void GenerateTransformationMatrix();

  /** Internal attributes */
  unsigned int m_NumberOfPrincipalComponentsRequired;
  bool         m_UseNormalization;
  bool         m_UseVarianceForNormalization;
  bool         m_GivenMeanValues;
  bool         m_GivenStdDevValues;
  bool         m_GivenCovarianceMatrix;
  bool         m_GivenTransformationMatrix;
  bool         m_IsTransformationMatrixForward;

  VectorType m_MeanValues;
  VectorType m_StdDevValues;
  MatrixType m_CovarianceMatrix;
  VectorType m_EigenValues;
  MatrixType m_TransformationMatrix;

  CovarianceEstimatorFilterPointerType m_CovarianceEstimator;
  TransformFilterPointerType           m_Transformer;
  NormalizeFilterPointerType           m_Normalizer;

private:
  PCAImageFilter( const Self & ); // not implemented
  void operator= ( const Self & ); // not implemented
};

} // end of namespace otb

#ifndef OTB_MANUAL_INSTANTIATION
#include "otbPCAImageFilter.txx"
#endif

#endif // otbPCAImageFilter_h