OTB  6.7.0
Orfeo Toolbox
otbNormalizeVectorImageFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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 otbNormalizeVectorImageFilter_h
22 #define otbNormalizeVectorImageFilter_h
23 
24 #include <vnl/vnl_math.h>
25 
26 #include <itkMacro.h>
27 #include <otbVectorImage.h>
29 
31 
32 namespace otb
33 {
34 namespace Functor
35 {
39 template < class TInput, class TOutput >
41 {
42 public:
45 
48 
49  TOutput operator() ( const TInput & input )
50  {
51  unsigned int length = input.Size();
52  TOutput output ( length );
53  for ( unsigned int i = 0; i < length; ++i )
54  {
55  output[i] = static_cast<typename TOutput::ValueType>(
56  ( static_cast< RealType >( input[i] ) - m_Mean[i] )
57  / m_StdDev[i] );
58  }
59  return output;
60  }
61 
62  template < class T >
64  {
65  m_Mean.SetSize( m.Size() );
66  for ( unsigned int i = 0; i < m_Mean.Size(); ++i )
67  m_Mean[i] = static_cast< RealType >( m[i] );
68  }
69 
71  {
72  return this->m_Mean;
73  }
74 
75  template < class T>
76  void SetStdDev ( const itk::VariableLengthVector<T> & sigma )
77  {
78  m_StdDev.SetSize( sigma.Size() );
79  for ( unsigned int i = 0; i < m_StdDev.Size(); ++i )
80  {
81  m_StdDev[i] = static_cast< RealType >( sigma[i] );
83  {
84  throw itk::ExceptionObject(__FILE__, __LINE__,
85  "Cannot divide by zero !", ITK_LOCATION);
86  }
87  }
88  }
89 
90  template < class T >
92  {
93  m_StdDev.SetSize( var.Size() );
94  for ( unsigned int i = 0; i < m_StdDev.Size(); ++i )
95  {
96  m_StdDev[i] = std::sqrt( static_cast< RealType >( var[i] ) );
98  {
99  throw itk::ExceptionObject(__FILE__, __LINE__,
100  "Cannot divide by zero !", ITK_LOCATION);
101  }
102  }
103  }
104 
106  {
107  return this->m_StdDev;
108  }
109 
110 protected:
111 
114 }; // end of class NormalizeVectorImageFunctor
115 
116 } // end of namespace Functor
117 
118 
132 template < class TInputImage, class TOutputImage >
134  : public UnaryFunctorVectorImageFilter< TInputImage, TOutputImage,
135  Functor::NormalizeVectorImageFunctor<
136  typename TInputImage::PixelType,
137  typename TOutputImage::PixelType > >
138 {
139 public:
142  typedef UnaryFunctorVectorImageFilter< TInputImage, TOutputImage,
144  typename TInputImage::PixelType,
145  typename TOutputImage::PixelType > > Superclass;
148 
150  itkNewMacro(Self);
151 
153  itkTypeMacro(NormalizeVectorImageFilter, ImageToImageFilter);
154 
155  typedef TInputImage InputImageType;
156  typedef TOutputImage OutputImageType;
157 
160 
163 
164  itkGetConstMacro(CovarianceEstimator, CovarianceEstimatorFilterType*);
165 
166  template < class T >
168  {
169  this->GetFunctor().SetMean( m );
170  m_IsGivenMean = true;
171  m_UseMean = true;
172  this->Modified();
173  }
174 
175  template < class T >
177  {
178  this->GetFunctor().SetStdDev( sigma );
179  m_IsGivenStdDev = true;
180  m_UseStdDev = true;
181  this->Modified();
182  }
183 
184  template < class T >
186  {
187  this->GetFunctor().SetVariance( var );
188  m_IsGivenStdDev = true;
189  m_UseStdDev = true;
190  this->Modified();
191  }
192 
193 
194  itkSetMacro(UseMean, bool);
195  itkSetMacro(UseStdDev, bool);
196 
197 protected:
200 
201  void GenerateOutputInformation() override;
202 
203 
204 private:
205  NormalizeVectorImageFilter ( const Self & );
206  void operator=( const Self & );
207 
210 
211  bool m_UseMean;
213 
215 
216 }; // end of class NormalizeVectorImageFilter
217 
218 } // end of namespace otb
219 
220 #ifndef OTB_MANUAL_INSTANTIATION
222 #endif
223 
224 #endif // otbNormalizeVectorImageFilter_h
225 
void SetStdDev(const itk::VariableLengthVector< T > &sigma)
void SetMean(const itk::VariableLengthVector< T > &m)
CovarianceEstimatorFilterType::Pointer CovarianceEstimatorFilterPointerType
itk::NumericTraits< typename RealVectorType::ValueType >::RealType RealType
itk::NumericTraits< typename TInputImage::PixelType >::RealType RealVectorType
unsigned int Size(void) const noexcept
void SetVariance(const itk::VariableLengthVector< T > &var)
itk::NumericTraits< typename RealVectorType::ValueType >::RealType RealType
CovarianceEstimatorFilterPointerType m_CovarianceEstimator
void SetVariance(const itk::VariableLengthVector< T > &var)
itk::NumericTraits< TInput >::RealType RealVectorType
Implements neighborhood-wise generic operation of one vector image.
void SetMean(const itk::VariableLengthVector< T > &m)
StreamingStatisticsVectorImageFilter< InputImageType > CovarianceEstimatorFilterType
UnaryFunctorVectorImageFilter< TInputImage, TOutputImage, Functor::NormalizeVectorImageFunctor< typename TInputImage::PixelType, typename TOutputImage::PixelType > > Superclass
itk::SmartPointer< const Self > ConstPointer
This class streams the whole input image through the PersistentStatisticsImageFilter.
void SetStdDev(const itk::VariableLengthVector< T > &sigma)
Normalize an VectorImage by setting its mean to zero and possibly variance to one (band by band)...