Orfeo Toolbox  4.2
otbNormalizeInnerProductPCAImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbNormalizeInnerProductPCAImageFilter_txx
19 #define __otbNormalizeInnerProductPCAImageFilter_txx
20 
22 #include "itkImageRegionIterator.h"
24 #include "itkProgressReporter.h"
25 #include "itkNumericTraits.h"
26 
27 namespace otb
28 {
29 
33 template <class TInputImage, class TOutputImage>
36 {
37  this->SetNumberOfRequiredInputs(1);
38  this->SetNumberOfRequiredOutputs(1);
39  this->InPlaceOff();
40 
41  m_UseUnbiasedEstimator = true;
42 }
43 
47 template<class TInputImage, class TOutputImage>
48 void
51 {
52  Superclass::GenerateOutputInformation();
53 }
54 
58 template <class TInputImage, class TOutputImage>
59 void
62 {
63  StreamingStatisticsVectorImageFilterPointer stats = StreamingStatisticsVectorImageFilterType::New();
64  stats->SetInput(const_cast<InputImageType*>(this->GetInput()));
65  //set the normalization method to compute covariance to the StreamingStatisticsVectorImage filter
66  stats->SetUseUnbiasedEstimator(m_UseUnbiasedEstimator);
67 
68  stats->Update();
69 
70  RealPixelType means = stats->GetMean();
71  MatrixType cov = stats->GetCovariance();
72  double NbPixels = static_cast<double>(
73  this->GetInput()->GetLargestPossibleRegion().GetSize()[0] *
74  this->GetInput()->GetLargestPossibleRegion().GetSize()[1]);
75  if ((cov.Rows() != means.Size()) || (cov.Cols() != means.Size()))
76  {
77  itkExceptionMacro(<< "Covariance matrix with size (" << cov.Rows() << "," <<
78  cov.Cols() << ") is incompatible with mean vector with size" << means.Size());
79  }
80  m_CoefNorm.SetSize(means.Size());
81  for (unsigned int i = 0; i < m_CoefNorm.Size(); ++i)
82  {
83  m_CoefNorm[i] = (1. / vcl_sqrt(NbPixels * (cov[i][i] + means[i] * means[i])));
84  }
85 }
89 template <class TInputImage, class TOutputImage>
90 void
92 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
93 {
94  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
95  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
96 
97  // Define the iterators
98  itk::ImageRegionConstIterator<InputImageType> inputIt(inputPtr, outputRegionForThread);
99  itk::ImageRegionIterator<OutputImageType> outputIt(outputPtr, outputRegionForThread);
100 
101  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
102 
103  inputIt.GoToBegin();
104  outputIt.GoToBegin();
105 
106  // Null pixel construction
107  InputPixelType nullPixel;
108  nullPixel.SetSize(inputPtr->GetNumberOfComponentsPerPixel());
110  while (!inputIt.IsAtEnd())
111  {
112  InputPixelType inPixel = inputIt.Get();
113  OutputPixelType outPixel;
114  outPixel.SetSize(inputPtr->GetNumberOfComponentsPerPixel());
116  //outPixel = m_Means * inPixel;
117  for (unsigned int j = 0; j < inputPtr->GetNumberOfComponentsPerPixel(); ++j)
118  {
119  outPixel[j] = static_cast<OutputInternalPixelType>(m_CoefNorm[j] * static_cast<double>(inPixel[j]));
120  }
121 
122  outputIt.Set(outPixel);
123  ++inputIt;
124  ++outputIt;
125  progress.CompletedPixel(); // potential exception thrown here
126  }
127 }
128 
129 template <class TInputImage, class TOutputImage>
130 void
132 ::PrintSelf(std::ostream& os, itk::Indent indent) const
133 {
134  this->Superclass::PrintSelf(os, indent);
135 }
136 
137 } // end namespace otb
138 
139 #endif

Generated at Sat Aug 30 2014 16:17:49 for Orfeo Toolbox with doxygen 1.8.3.1