21 #ifndef otbEstimateInnerProductPCAImageFilter_hxx
22 #define otbEstimateInnerProductPCAImageFilter_hxx
26 #include <vnl/algo/vnl_generalized_eigensystem.h>
27 #include <vnl/vnl_math.h>
35 template <
class TInputImage,
class TOutputImage>
37 : m_NumberOfPrincipalComponentsRequired(1), m_CenterData(true)
44 template <
class TInputImage,
class TOutputImage>
47 this->Superclass::PrintSelf(os, indent);
53 template <
class TInputImage,
class TOutputImage>
56 Superclass::GenerateOutputInformation();
57 this->GetOutput()->SetNumberOfComponentsPerPixel(m_NumberOfPrincipalComponentsRequired);
64 template <
class TInputImage,
class TOutputImage>
69 streamingInnerProduct->SetInput(
const_cast<InputImageType*
>(this->GetInput()));
70 streamingInnerProduct->SetCenterData(m_CenterData);
71 streamingInnerProduct->Update();
72 m_InnerProduct = streamingInnerProduct->GetInnerProduct();
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;
81 template <
class TInputImage,
class TOutputImage>
83 itk::ThreadIdType threadId)
85 typename InputImageType::ConstPointer inputPtr = this->GetInput();
86 typename OutputImageType::Pointer outputPtr = this->GetOutput();
91 unsigned int numberOfTrainingImages = inputPtr->GetNumberOfComponentsPerPixel();
94 this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
97 itk::ImageRegionConstIterator<TInputImage> inputIt(inputPtr, inputRegionForThread);
98 itk::ImageRegionIterator<TOutputImage> outputIt(outputPtr, outputRegionForThread);
100 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
103 outputIt.GoToBegin();
105 while (!inputIt.IsAtEnd())
109 outputPixel.SetSize(m_NumberOfPrincipalComponentsRequired);
112 for (
unsigned int img_number = 0; img_number < numberOfTrainingImages; ++img_number)
114 unsigned int indexNumberOfTrainingImages = numberOfTrainingImages - 1;
115 for (
unsigned int vec_number = 0; vec_number < m_NumberOfPrincipalComponentsRequired; ++vec_number, indexNumberOfTrainingImages--)
118 static_cast<double>(inputPixel[img_number]) *
static_cast<double>(m_EigenVectorsOfInnerProductMatrix[img_number][indexNumberOfTrainingImages]));
121 outputIt.Set(outputPixel);
124 progress.CompletedPixel();