21 #ifndef otbMaximumAutocorrelationFactorImageFilter_hxx
22 #define otbMaximumAutocorrelationFactorImageFilter_hxx
27 #include "itkSubtractImageFilter.h"
29 #include "vnl/algo/vnl_matrix_inverse.h"
30 #include "vnl/algo/vnl_generalized_eigensystem.h"
32 #include "itkChangeInformationImageFilter.h"
33 #include "itkImageRegionIterator.h"
34 #include "itkProgressReporter.h"
38 template <
class TInputImage,
class TOutputImage>
41 m_CovarianceEstimator = CovarianceEstimatorType::New();
42 m_CovarianceEstimatorH = CovarianceEstimatorType::New();
43 m_CovarianceEstimatorV = CovarianceEstimatorType::New();
46 template <
class TInputImage,
class TOutputImage>
50 Superclass::GenerateOutputInformation();
53 TInputImage* inputPtr =
const_cast<TInputImage*
>(this->GetInput());
57 unsigned int nbComp = inputPtr->GetNumberOfComponentsPerPixel();
61 typedef itk::SubtractImageFilter<InternalImageType, InternalImageType, InternalImageType> DifferenceFilterType;
62 typedef itk::ChangeInformationImageFilter<InternalImageType> ChangeInformationImageFilter;
69 referenceRegion.SetSize(size);
71 referenceRegion.SetIndex(index);
78 dhRegion.SetSize(size);
79 dhRegion.SetIndex(index);
84 dvRegion.SetSize(size);
85 dvRegion.SetIndex(index);
87 typename ExtractFilterType::Pointer referenceExtract = ExtractFilterType::New();
88 referenceExtract->SetInput(inputPtr);
89 referenceExtract->SetExtractionRegion(referenceRegion);
91 typename ExtractFilterType::Pointer dhExtract = ExtractFilterType::New();
92 dhExtract->SetInput(inputPtr);
93 dhExtract->SetExtractionRegion(dhRegion);
95 typename ChangeInformationImageFilter::Pointer dhExtractShift = ChangeInformationImageFilter::New();
96 dhExtractShift->SetInput(dhExtract->GetOutput());
97 dhExtractShift->SetReferenceImage(referenceExtract->GetOutput());
98 dhExtractShift->SetUseReferenceImage(
true);
99 dhExtractShift->SetChangeOrigin(
true);
101 typename ExtractFilterType::Pointer dvExtract = ExtractFilterType::New();
102 dvExtract->SetInput(inputPtr);
103 dvExtract->SetExtractionRegion(dvRegion);
105 typename ChangeInformationImageFilter::Pointer dvExtractShift = ChangeInformationImageFilter::New();
106 dvExtractShift->SetInput(dvExtract->GetOutput());
107 dvExtractShift->SetReferenceImage(referenceExtract->GetOutput());
108 dvExtractShift->SetUseReferenceImage(
true);
109 dvExtractShift->SetChangeOrigin(
true);
112 typename DifferenceFilterType::Pointer diffh = DifferenceFilterType::New();
113 diffh->SetInput1(referenceExtract->GetOutput());
114 diffh->SetInput2(dhExtractShift->GetOutput());
116 typename DifferenceFilterType::Pointer diffv = DifferenceFilterType::New();
117 diffv->SetInput1(referenceExtract->GetOutput());
118 diffv->SetInput2(dvExtractShift->GetOutput());
121 m_CovarianceEstimatorH->SetInput(diffh->GetOutput());
122 m_CovarianceEstimatorH->Update();
123 VnlMatrixType sigmadh = m_CovarianceEstimatorH->GetCovariance().GetVnlMatrix();
125 m_CovarianceEstimatorV->SetInput(diffv->GetOutput());
126 m_CovarianceEstimatorV->Update();
127 VnlMatrixType sigmadv = m_CovarianceEstimatorV->GetCovariance().GetVnlMatrix();
133 referenceExtract->SetExtractionRegion(inputPtr->GetLargestPossibleRegion());
134 m_CovarianceEstimator->SetInput(referenceExtract->GetOutput());
135 m_CovarianceEstimator->Update();
136 VnlMatrixType sigma = m_CovarianceEstimator->GetCovariance().GetVnlMatrix();
140 for (
unsigned int i = 0; i < nbComp; ++i)
142 m_Mean[i] = m_CovarianceEstimator->GetMean()[i];
145 vnl_generalized_eigensystem ges(sigmad, sigma);
150 m_AutoCorrelation -= 0.5 * d.get_diagonal();
153 invstderr.set_diagonal(sigma.get_diagonal());
154 invstderr = invstderr.apply(&std::sqrt);
158 invstderrmaf.set_diagonal((m_V.transpose() * sigma * m_V).get_diagonal());
159 invstderrmaf = invstderrmaf.apply(&std::sqrt);
168 for (
unsigned int i = 0; i < nbComp; ++i)
170 aux2 = aux2 + aux1.get_row(i);
173 sign.set_diagonal(aux2);
181 template <
class TInputImage,
class TOutputImage>
183 itk::ThreadIdType threadId)
186 const TInputImage* inputPtr = this->GetInput();
187 TOutputImage* outputPtr = this->GetOutput();
190 typedef itk::ImageRegionConstIterator<InputImageType> ConstIteratorType;
191 typedef itk::ImageRegionIterator<OutputImageType> IteratorType;
193 IteratorType outIt(outputPtr, outputRegionForThread);
194 ConstIteratorType inIt(inputPtr, outputRegionForThread);
200 unsigned int outNbComp = outputPtr->GetNumberOfComponentsPerPixel();
203 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
205 while (!inIt.IsAtEnd() && !outIt.IsAtEnd())
210 for (
unsigned int i = 0; i < outNbComp; ++i)
212 x[i] = inIt.Get()[i];
215 maf = (x - m_Mean) * m_V;
217 typename OutputImageType::PixelType outPixel(outNbComp);
219 for (
unsigned int i = 0; i < outNbComp; ++i)
221 outPixel[i] = maf[i];
228 progress.CompletedPixel();