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