OTB  6.7.0
Orfeo Toolbox
otbShiftScaleSampleListFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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 otbShiftScaleSampleListFilter_hxx
22 #define otbShiftScaleSampleListFilter_hxx
23 
25 #include "itkProgressReporter.h"
26 
27 namespace otb {
28 namespace Statistics {
29 
30 template < class TInputSampleList, class TOutputSampleList >
33 
34 template < class TInputSampleList, class TOutputSampleList >
35 void
38 {
39  // Retrieve input and output pointers
40  InputSampleListConstPointer inputSampleListPtr = this->GetInput();
41 
42  OutputSampleListPointer outputSampleListPtr = this->GetOutput();
43  outputSampleListPtr->SetMeasurementVectorSize(inputSampleListPtr->GetMeasurementVectorSize());
44 
45  // Check if the inputSampleList is not empty
46  if(inputSampleListPtr->Size() == 0)
47  itkExceptionMacro(<< "Input Sample List is empty");
48 
49  // Check if the size of the scale and the shift measurement vectors
50  // are the same than the input vector
51  if(inputSampleListPtr->GetMeasurementVectorSize() != m_Scales.Size()
52  || inputSampleListPtr->GetMeasurementVectorSize() != m_Shifts.Size())
53  itkExceptionMacro(<< "Inconsistent measurement vector size : Input Sample List size "
54  << inputSampleListPtr->GetMeasurementVectorSize()
55  << " Scale measurement vector size "
56  <<m_Scales.Size()
57  <<" Shift measurement vector size "
58  <<m_Shifts.Size());
59 
60  // Compute the 1/(sigma) vector
61  InputMeasurementVectorType invertedScales = m_Scales;
62  for(unsigned int idx = 0; idx < invertedScales.Size(); ++idx)
63  {
64  if(m_Scales[idx]-1e-10 < 0.)
65  invertedScales[idx] = 0.;
66  else
67  invertedScales[idx] = 1 / m_Scales[idx];
68  }
69 
70  // Clear any previous output
71  outputSampleListPtr->Clear();
72 
73  typename InputSampleListType::ConstIterator inputIt = inputSampleListPtr->Begin();
74 
75  // Set-up progress reporting
76  itk::ProgressReporter progress(this, 0, inputSampleListPtr->Size());
77 
78  // Iterate on the InputSampleList
79  while(inputIt != inputSampleListPtr->End())
80  {
81  // Retrieve current input sample
82  InputMeasurementVectorType currentInputMeasurement = inputIt.GetMeasurementVector();
83 
84  // Build current output sample
85  OutputMeasurementVectorType currentOutputMeasurement;
86  currentOutputMeasurement.SetSize(currentInputMeasurement.GetSize());
87 
88  // Center and reduce each component
89  for(unsigned int idx = 0; idx < invertedScales.Size(); ++idx)
90  {
91  currentOutputMeasurement[idx] = static_cast<OutputValueType>(
92  (currentInputMeasurement[idx]-m_Shifts[idx])*invertedScales[idx]);
93  }
94 
95  // Add the current output sample to the output SampleList
96  outputSampleListPtr->PushBack(currentOutputMeasurement);
97 
98  // Update progress
99  progress.CompletedPixel();
100 
101  ++inputIt;
102  }
103 }
104 
105 template < class TInputSampleList, class TOutputSampleList >
106 void
108 ::PrintSelf(std::ostream& os, itk::Indent indent) const
109 {
110  // Call superclass implementation
111  Superclass::PrintSelf(os, indent);
112 }
113 
114 } // End namespace Statistics
115 } // End namespace otb
116 
117 #endif
Superclass::OutputMeasurementVectorType OutputMeasurementVectorType
InputSampleListType::MeasurementVectorType InputMeasurementVectorType
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Superclass::OutputSampleListPointer OutputSampleListPointer