OTB  10.0.0
Orfeo Toolbox
otbNormalizeInnerProductPCAImageFilter.hxx
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 otbNormalizeInnerProductPCAImageFilter_hxx
22 #define otbNormalizeInnerProductPCAImageFilter_hxx
23 
25 #include "itkImageRegionIterator.h"
26 #include "itkNumericTraits.h"
27 
28 namespace otb
29 {
30 
34 template <class TInputImage, class TOutputImage>
36 {
37  this->SetNumberOfRequiredInputs(1);
38  this->SetNumberOfRequiredOutputs(1);
39  this->InPlaceOff();
40  this->DynamicMultiThreadingOn();
41  m_UseUnbiasedEstimator = true;
42 }
44 
48 template <class TInputImage, class TOutputImage>
50 {
51  Superclass::GenerateOutputInformation();
52 }
53 
57 template <class TInputImage, class TOutputImage>
59 {
60  StreamingStatisticsVectorImageFilterPointer stats = StreamingStatisticsVectorImageFilterType::New();
61  stats->SetInput(const_cast<InputImageType*>(this->GetInput()));
62  // set the normalization method to compute covariance to the StreamingStatisticsVectorImage filter
63  stats->SetUseUnbiasedEstimator(m_UseUnbiasedEstimator);
65 
66  stats->Update();
67 
68  RealPixelType means = stats->GetMean();
69  MatrixType cov = stats->GetCovariance();
70  double NbPixels = static_cast<double>(this->GetInput()->GetLargestPossibleRegion().GetSize()[0] * this->GetInput()->GetLargestPossibleRegion().GetSize()[1]);
71  if ((cov.Rows() != means.Size()) || (cov.Cols() != means.Size()))
72  {
73  itkExceptionMacro(<< "Covariance matrix with size (" << cov.Rows() << "," << cov.Cols() << ") is incompatible with mean vector with size" << means.Size());
74  }
75  m_CoefNorm.SetSize(means.Size());
76  for (unsigned int i = 0; i < m_CoefNorm.Size(); ++i)
77  {
78  m_CoefNorm[i] = (1. / std::sqrt(NbPixels * (cov[i][i] + means[i] * means[i])));
79  }
80 }
84 template <class TInputImage, class TOutputImage>
86 {
87  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
88  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
90 
91  // Define the iterators
92  itk::ImageRegionConstIterator<InputImageType> inputIt(inputPtr, outputRegionForThread);
93  itk::ImageRegionIterator<OutputImageType> outputIt(outputPtr, outputRegionForThread);
94 
95  inputIt.GoToBegin();
96  outputIt.GoToBegin();
97 
98  // Null pixel construction
99  InputPixelType nullPixel;
100  nullPixel.SetSize(inputPtr->GetNumberOfComponentsPerPixel());
101  nullPixel.Fill(itk::NumericTraits<OutputInternalPixelType>::Zero);
102  while (!inputIt.IsAtEnd())
103  {
104  InputPixelType inPixel = inputIt.Get();
105  OutputPixelType outPixel;
106  outPixel.SetSize(inputPtr->GetNumberOfComponentsPerPixel());
107  outPixel.Fill(itk::NumericTraits<OutputInternalPixelType>::Zero);
108  // outPixel = m_Means * inPixel;
109  for (unsigned int j = 0; j < inputPtr->GetNumberOfComponentsPerPixel(); ++j)
110  {
111  outPixel[j] = static_cast<OutputInternalPixelType>(m_CoefNorm[j] * static_cast<double>(inPixel[j]));
112  }
113 
114  outputIt.Set(outPixel);
115  ++inputIt;
116  ++outputIt;
117  }
118 }
119 
120 template <class TInputImage, class TOutputImage>
122 {
123  this->Superclass::PrintSelf(os, indent);
124 }
125 
126 } // end namespace otb
127 
128 #endif
StreamingStatisticsVectorImageFilterType::RealPixelType RealPixelType
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
void PrintSelf(std::ostream &os, itk::Indent indent) const override
StreamingStatisticsVectorImageFilterType::MatrixType MatrixType
StreamingStatisticsVectorImageFilterType::Pointer StreamingStatisticsVectorImageFilterPointer
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.