OTB  9.0.0
Orfeo Toolbox
otbNormalizeVectorImageFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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:
44  {
45  }
47  {
48  }
49 
50  typedef typename itk::NumericTraits<TInput>::RealType RealVectorType;
51  typedef typename itk::NumericTraits<typename RealVectorType::ValueType>::RealType RealType;
52 
53  TOutput operator()(const TInput& input)
54  {
55  unsigned int length = input.Size();
56  TOutput output(length);
57  for (unsigned int i = 0; i < length; ++i)
58  {
59  output[i] = static_cast<typename TOutput::ValueType>((static_cast<RealType>(input[i]) - m_Mean[i]) / m_StdDev[i]);
60  }
61  return output;
62  }
63 
64  template <class T>
65  void SetMean(const itk::VariableLengthVector<T>& m)
66  {
67  m_Mean.SetSize(m.Size());
68  for (unsigned int i = 0; i < m_Mean.Size(); ++i)
69  m_Mean[i] = static_cast<RealType>(m[i]);
70  }
71 
73  {
74  return this->m_Mean;
75  }
76 
77  template <class T>
78  void SetStdDev(const itk::VariableLengthVector<T>& sigma)
79  {
80  m_StdDev.SetSize(sigma.Size());
81  for (unsigned int i = 0; i < m_StdDev.Size(); ++i)
82  {
83  m_StdDev[i] = static_cast<RealType>(sigma[i]);
84  if (m_StdDev[i] == itk::NumericTraits<RealType>::Zero)
85  {
86  throw itk::ExceptionObject(__FILE__, __LINE__, "Cannot divide by zero !", ITK_LOCATION);
87  }
88  }
89  }
90 
91  template <class T>
92  void SetVariance(const itk::VariableLengthVector<T>& var)
93  {
94  m_StdDev.SetSize(var.Size());
95  for (unsigned int i = 0; i < m_StdDev.Size(); ++i)
96  {
97  m_StdDev[i] = std::sqrt(static_cast<RealType>(var[i]));
98  if (m_StdDev[i] == itk::NumericTraits<RealType>::Zero)
99  {
100  throw itk::ExceptionObject(__FILE__, __LINE__, "Cannot divide by zero !", ITK_LOCATION);
101  }
102  }
103  }
104 
106  {
107  return this->m_StdDev;
108  }
109 
110 protected:
111  RealVectorType m_Mean;
113 }; // end of class NormalizeVectorImageFunctor
114 
115 } // end of namespace Functor
116 
117 
131 template <class TInputImage, class TOutputImage>
133  : public UnaryFunctorVectorImageFilter<TInputImage, TOutputImage,
134  Functor::NormalizeVectorImageFunctor<typename TInputImage::PixelType, typename TOutputImage::PixelType>>
135 {
136 public:
139  typedef UnaryFunctorVectorImageFilter<TInputImage, TOutputImage,
142  typedef itk::SmartPointer<Self> Pointer;
143  typedef itk::SmartPointer<const Self> ConstPointer;
144 
146  itkNewMacro(Self);
147 
149  itkTypeMacro(NormalizeVectorImageFilter, ImageToImageFilter);
150 
151  typedef TInputImage InputImageType;
152  typedef TOutputImage OutputImageType;
153 
154  typedef typename itk::NumericTraits<typename TInputImage::PixelType>::RealType RealVectorType;
155  typedef typename itk::NumericTraits<typename RealVectorType::ValueType>::RealType RealType;
156 
159 
160  itkGetConstMacro(CovarianceEstimator, CovarianceEstimatorFilterType*);
161 
162  template <class T>
163  void SetMean(const itk::VariableLengthVector<T>& m)
164  {
165  this->GetFunctor().SetMean(m);
166  m_IsGivenMean = true;
167  m_UseMean = true;
168  this->Modified();
169  }
170 
171  template <class T>
172  void SetStdDev(const itk::VariableLengthVector<T>& sigma)
173  {
174  this->GetFunctor().SetStdDev(sigma);
175  m_IsGivenStdDev = true;
176  m_UseStdDev = true;
177  this->Modified();
178  }
179 
180  template <class T>
181  void SetVariance(const itk::VariableLengthVector<T>& var)
182  {
183  this->GetFunctor().SetVariance(var);
184  m_IsGivenStdDev = true;
185  m_UseStdDev = true;
186  this->Modified();
187  }
188 
189 
190  itkSetMacro(UseMean, bool);
191  itkSetMacro(UseStdDev, bool);
192 
193 protected:
196  {
197  }
198 
199  void GenerateOutputInformation() override;
200 
201 
202 private:
203  NormalizeVectorImageFilter(const Self&);
204  void operator=(const Self&);
205 
208 
209  bool m_UseMean;
211 
213 
214 }; // end of class NormalizeVectorImageFilter
215 
216 } // end of namespace otb
217 
218 #ifndef OTB_MANUAL_INSTANTIATION
220 #endif
221 
222 #endif // otbNormalizeVectorImageFilter_h
otb::Functor::NormalizeVectorImageFunctor::m_StdDev
RealVectorType m_StdDev
Definition: otbNormalizeVectorImageFilter.h:112
otb::Functor::NormalizeVectorImageFunctor::~NormalizeVectorImageFunctor
virtual ~NormalizeVectorImageFunctor()
Definition: otbNormalizeVectorImageFilter.h:46
otb::NormalizeVectorImageFilter::CovarianceEstimatorFilterPointerType
CovarianceEstimatorFilterType::Pointer CovarianceEstimatorFilterPointerType
Definition: otbNormalizeVectorImageFilter.h:158
otbNormalizeVectorImageFilter.hxx
otbVectorImage.h
otb::NormalizeVectorImageFilter::m_IsGivenMean
bool m_IsGivenMean
Definition: otbNormalizeVectorImageFilter.h:206
otbStreamingStatisticsVectorImageFilter.h
otb::NormalizeVectorImageFilter::SetVariance
void SetVariance(const itk::VariableLengthVector< T > &var)
Definition: otbNormalizeVectorImageFilter.h:181
otb::var
Definition: otbParserXPlugins.h:282
otb::NormalizeVectorImageFilter::SetStdDev
void SetStdDev(const itk::VariableLengthVector< T > &sigma)
Definition: otbNormalizeVectorImageFilter.h:172
otb::StreamingStatisticsVectorImageFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbStreamingStatisticsVectorImageFilter.h:304
otb::NormalizeVectorImageFilter::~NormalizeVectorImageFilter
~NormalizeVectorImageFilter() override
Definition: otbNormalizeVectorImageFilter.h:195
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::NormalizeVectorImageFunctor::SetMean
void SetMean(const itk::VariableLengthVector< T > &m)
Definition: otbNormalizeVectorImageFilter.h:65
otb::Functor::NormalizeVectorImageFunctor::m_Mean
RealVectorType m_Mean
Definition: otbNormalizeVectorImageFilter.h:111
otb::NormalizeVectorImageFilter::Self
NormalizeVectorImageFilter Self
Definition: otbNormalizeVectorImageFilter.h:138
otb::Functor::NormalizeVectorImageFunctor::operator()
TOutput operator()(const TInput &input)
Definition: otbNormalizeVectorImageFilter.h:53
otb::Functor::NormalizeVectorImageFunctor::RealType
itk::NumericTraits< typename RealVectorType::ValueType >::RealType RealType
Definition: otbNormalizeVectorImageFilter.h:51
otb::NormalizeVectorImageFilter::OutputImageType
TOutputImage OutputImageType
Definition: otbNormalizeVectorImageFilter.h:152
otb::NormalizeVectorImageFilter::m_UseMean
bool m_UseMean
Definition: otbNormalizeVectorImageFilter.h:209
otb::StreamingStatisticsVectorImageFilter
This class streams the whole input image through the PersistentStatisticsImageFilter.
Definition: otbStreamingStatisticsVectorImageFilter.h:297
otb::Functor::NormalizeVectorImageFunctor
NormalizeVectorImageFunctor.
Definition: otbNormalizeVectorImageFilter.h:40
otb::UnaryFunctorVectorImageFilter
Implements neighborhood-wise generic operation of one vector image.
Definition: otbUnaryFunctorVectorImageFilter.h:40
otb::NormalizeVectorImageFilter::CovarianceEstimatorFilterType
StreamingStatisticsVectorImageFilter< InputImageType > CovarianceEstimatorFilterType
Definition: otbNormalizeVectorImageFilter.h:157
otb::NormalizeVectorImageFilter::m_IsGivenStdDev
bool m_IsGivenStdDev
Definition: otbNormalizeVectorImageFilter.h:207
otb::Functor::NormalizeVectorImageFunctor::GetStdDev
RealVectorType GetStdDev() const
Definition: otbNormalizeVectorImageFilter.h:105
otb::NormalizeVectorImageFilter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbNormalizeVectorImageFilter.h:143
otb::NormalizeVectorImageFilter::Superclass
UnaryFunctorVectorImageFilter< TInputImage, TOutputImage, Functor::NormalizeVectorImageFunctor< typename TInputImage::PixelType, typename TOutputImage::PixelType > > Superclass
Definition: otbNormalizeVectorImageFilter.h:141
otb::Functor::NormalizeVectorImageFunctor::SetVariance
void SetVariance(const itk::VariableLengthVector< T > &var)
Definition: otbNormalizeVectorImageFilter.h:92
otb::NormalizeVectorImageFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbNormalizeVectorImageFilter.h:142
otb::Functor::NormalizeVectorImageFunctor::NormalizeVectorImageFunctor
NormalizeVectorImageFunctor()
Definition: otbNormalizeVectorImageFilter.h:43
otb::Functor::NormalizeVectorImageFunctor::SetStdDev
void SetStdDev(const itk::VariableLengthVector< T > &sigma)
Definition: otbNormalizeVectorImageFilter.h:78
otb::NormalizeVectorImageFilter::SetMean
void SetMean(const itk::VariableLengthVector< T > &m)
Definition: otbNormalizeVectorImageFilter.h:163
otb::NormalizeVectorImageFilter::m_CovarianceEstimator
CovarianceEstimatorFilterPointerType m_CovarianceEstimator
Definition: otbNormalizeVectorImageFilter.h:212
otb::NormalizeVectorImageFilter::RealVectorType
itk::NumericTraits< typename TInputImage::PixelType >::RealType RealVectorType
Definition: otbNormalizeVectorImageFilter.h:154
otb::NormalizeVectorImageFilter
Normalize an VectorImage by setting its mean to zero and possibly variance to one (band by band).
Definition: otbNormalizeVectorImageFilter.h:132
otb::Functor::NormalizeVectorImageFunctor::GetMean
RealVectorType GetMean() const
Definition: otbNormalizeVectorImageFilter.h:72
otbUnaryFunctorVectorImageFilter.h
otb::NormalizeVectorImageFilter::RealType
itk::NumericTraits< typename RealVectorType::ValueType >::RealType RealType
Definition: otbNormalizeVectorImageFilter.h:155
otb::NormalizeVectorImageFilter::InputImageType
TInputImage InputImageType
Definition: otbNormalizeVectorImageFilter.h:149
otb::NormalizeVectorImageFilter::m_UseStdDev
bool m_UseStdDev
Definition: otbNormalizeVectorImageFilter.h:210
otb::Functor::NormalizeVectorImageFunctor::RealVectorType
itk::NumericTraits< TInput >::RealType RealVectorType
Definition: otbNormalizeVectorImageFilter.h:50