OTB  9.0.0
Orfeo Toolbox
otbEstimateInnerProductPCAImageFilter.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 otbEstimateInnerProductPCAImageFilter_hxx
22 #define otbEstimateInnerProductPCAImageFilter_hxx
23 
25 
26 #include <vnl/algo/vnl_generalized_eigensystem.h>
27 #include <vnl/vnl_math.h>
28 
29 namespace otb
30 {
31 
35 template <class TInputImage, class TOutputImage>
37  : m_NumberOfPrincipalComponentsRequired(1), m_CenterData(true)
38 {
39 }
40 
44 template <class TInputImage, class TOutputImage>
45 void EstimateInnerProductPCAImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
46 {
47  this->Superclass::PrintSelf(os, indent);
48 }
49 
53 template <class TInputImage, class TOutputImage>
55 {
56  Superclass::GenerateOutputInformation();
57  this->GetOutput()->SetNumberOfComponentsPerPixel(m_NumberOfPrincipalComponentsRequired);
58 }
60 
64 template <class TInputImage, class TOutputImage>
66 {
67  // Instantiation object
68  StreamingInnerProductPointer streamingInnerProduct = StreamingInnerProductType::New();
69  streamingInnerProduct->SetInput(const_cast<InputImageType*>(this->GetInput()));
70  streamingInnerProduct->SetCenterData(m_CenterData);
71  streamingInnerProduct->Update();
72  m_InnerProduct = streamingInnerProduct->GetInnerProduct();
74 
75  MatrixType identityMatrix(m_InnerProduct.rows(), m_InnerProduct.columns());
76  identityMatrix.set_identity();
77  vnl_generalized_eigensystem eigenVectors_eigenValues(m_InnerProduct, identityMatrix);
78  m_EigenVectorsOfInnerProductMatrix = eigenVectors_eigenValues.V;
79 }
80 
81 template <class TInputImage, class TOutputImage>
83  itk::ThreadIdType threadId)
84 {
85  typename InputImageType::ConstPointer inputPtr = this->GetInput();
86  typename OutputImageType::Pointer outputPtr = this->GetOutput();
87 
88  // Define the portion of the input to walk for this thread, using
89  // the CallCopyOutputRegionToInputRegion method allows for the input
90  // and output images to be different dimensions
91  unsigned int numberOfTrainingImages = inputPtr->GetNumberOfComponentsPerPixel();
92 
93  InputImageRegionType inputRegionForThread;
94  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
95 
96  // Define the iterators
97  itk::ImageRegionConstIterator<TInputImage> inputIt(inputPtr, inputRegionForThread);
98  itk::ImageRegionIterator<TOutputImage> outputIt(outputPtr, outputRegionForThread);
99 
100  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
101 
102  inputIt.GoToBegin();
103  outputIt.GoToBegin();
104 
105  while (!inputIt.IsAtEnd())
106  {
107  InputPixelType inputPixel = inputIt.Get();
108  OutputPixelType outputPixel;
109  outputPixel.SetSize(m_NumberOfPrincipalComponentsRequired);
110  outputPixel.Fill(0);
111 
112  for (unsigned int img_number = 0; img_number < numberOfTrainingImages; ++img_number)
113  {
114  unsigned int indexNumberOfTrainingImages = numberOfTrainingImages - 1;
115  for (unsigned int vec_number = 0; vec_number < m_NumberOfPrincipalComponentsRequired; ++vec_number, indexNumberOfTrainingImages--)
116  {
117  outputPixel[vec_number] += static_cast<OutputInternalPixelType>(
118  static_cast<double>(inputPixel[img_number]) * static_cast<double>(m_EigenVectorsOfInnerProductMatrix[img_number][indexNumberOfTrainingImages]));
119  }
120  }
121  outputIt.Set(outputPixel);
122  ++inputIt;
123  ++outputIt;
124  progress.CompletedPixel(); // potential exception thrown here
125  }
126 }
127 }
128 
129 #endif
otb::EstimateInnerProductPCAImageFilter::MatrixType
StreamingInnerProductType::MatrixType MatrixType
Definition: otbEstimateInnerProductPCAImageFilter.h:65
otb::EstimateInnerProductPCAImageFilter::StreamingInnerProductPointer
StreamingInnerProductType::Pointer StreamingInnerProductPointer
Definition: otbEstimateInnerProductPCAImageFilter.h:64
otb::EstimateInnerProductPCAImageFilter::InputImageRegionType
InputImageType::RegionType InputImageRegionType
Definition: otbEstimateInnerProductPCAImageFilter.h:56
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::EstimateInnerProductPCAImageFilter::GenerateOutputInformation
void GenerateOutputInformation(void) override
Definition: otbEstimateInnerProductPCAImageFilter.hxx:54
otb::EstimateInnerProductPCAImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbEstimateInnerProductPCAImageFilter.hxx:82
otb::EstimateInnerProductPCAImageFilter::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbEstimateInnerProductPCAImageFilter.h:59
otb::EstimateInnerProductPCAImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: otbEstimateInnerProductPCAImageFilter.hxx:65
otb::EstimateInnerProductPCAImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbEstimateInnerProductPCAImageFilter.hxx:45
otb::EstimateInnerProductPCAImageFilter::InputImageType
TInputImage InputImageType
Definition: otbEstimateInnerProductPCAImageFilter.h:50
otbEstimateInnerProductPCAImageFilter.h
otb::EstimateInnerProductPCAImageFilter::EstimateInnerProductPCAImageFilter
EstimateInnerProductPCAImageFilter()
Definition: otbEstimateInnerProductPCAImageFilter.hxx:36
otb::EstimateInnerProductPCAImageFilter::OutputPixelType
OutputImageType::PixelType OutputPixelType
Definition: otbEstimateInnerProductPCAImageFilter.h:60
otb::EstimateInnerProductPCAImageFilter::OutputInternalPixelType
OutputImageType::InternalPixelType OutputInternalPixelType
Definition: otbEstimateInnerProductPCAImageFilter.h:61
otb::EstimateInnerProductPCAImageFilter::InputPixelType
InputImageType::PixelType InputPixelType
Definition: otbEstimateInnerProductPCAImageFilter.h:55