OTB  9.0.0
Orfeo Toolbox
otbGaussianAdditiveNoiseSampleListFilter.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 otbGaussianAdditiveNoiseSampleListFilter_hxx
22 #define otbGaussianAdditiveNoiseSampleListFilter_hxx
23 
25 #include "itkMersenneTwisterRandomVariateGenerator.h"
26 #include "itkProgressReporter.h"
27 
28 namespace otb
29 {
30 namespace Statistics
31 {
32 
33 template <class TInputSampleList, class TOutputSampleList>
35 {
36  m_Mean = 0.;
37  m_Variance = 1e-3;
38  m_NumberOfIteration = 1;
39 }
40 
41 template <class TInputSampleList, class TOutputSampleList>
43 {
44  typedef itk::Statistics::MersenneTwisterRandomVariateGenerator GeneratorType;
45  GeneratorType::Pointer generator = GeneratorType::GetInstance();
46 
47  // Clear the coefficients vector first
48  m_WhiteGaussianNoiseCoefficients.clear();
49 
50  // Get the size of the measurement vectors
51  unsigned int size = this->GetInput()->GetMeasurementVectorSize();
52  if (size == 0)
53  {
54  itkExceptionMacro(<< "MeasurementVector size is " << size << " , expected non null size ");
55  }
56  else
57  for (unsigned int i = 0; i < size; ++i)
58  {
59  double ran = generator->GetNormalVariate(m_Mean, m_Variance);
60  m_WhiteGaussianNoiseCoefficients.push_back(ran);
61  }
62 }
63 
64 template <class TInputSampleList, class TOutputSampleList>
66 {
67  // Retrieve input and output pointers
68  InputSampleListConstPointer inputSampleListPtr = this->GetInput();
69  OutputSampleListPointer outputSampleListPtr = this->GetOutput();
70 
71  // Clear any previous output
72  outputSampleListPtr->Clear();
73 
74  // Set the measurement vector size
75  outputSampleListPtr->SetMeasurementVectorSize(inputSampleListPtr->GetMeasurementVectorSize());
76 
77  // Set-up progress reporting
78  itk::ProgressReporter progress(this, 0, inputSampleListPtr->Size() * m_NumberOfIteration);
79 
80  // Iterate m_NumberOfIteration-times the noising process
81  for (unsigned int currIteration = 0; currIteration < m_NumberOfIteration; currIteration++)
82  {
83  typename InputSampleListType::ConstIterator inputIt = inputSampleListPtr->Begin();
84 
85  // Iterate on the InputSampleList
86  while (inputIt != inputSampleListPtr->End())
87  {
88  // Generate Random sequence
89  this->GenerateRandomSequence();
90 
91  // Retrieve current input sample
92  InputMeasurementVectorType currentInputMeasurement = inputIt.GetMeasurementVector();
93 
94  // Build current output sample
95  OutputMeasurementVectorType currentOutputMeasurement;
96  currentOutputMeasurement.SetSize(currentInputMeasurement.GetSize());
97 
98  // Add the white noise to each component of the sample
99  for (unsigned int idx = 0; idx < inputSampleListPtr->GetMeasurementVectorSize(); ++idx)
100  {
101  currentOutputMeasurement[idx] =
102  static_cast<OutputValueType>((static_cast<double>(currentInputMeasurement[idx]) + m_WhiteGaussianNoiseCoefficients[idx]));
103  }
104 
105  // Add the current output sample to the output SampleList
106  outputSampleListPtr->PushBack(currentOutputMeasurement);
107 
108  // Update progress
109  progress.CompletedPixel();
110 
111  ++inputIt;
112  }
113  }
114 }
115 
116 template <class TInputSampleList, class TOutputSampleList>
118 {
119  // Call superclass implementation
120  Superclass::PrintSelf(os, indent);
121 }
122 
123 } // End namespace Statistics
124 } // End namespace otb
125 
126 #endif
otb::Statistics::GaussianAdditiveNoiseSampleListFilter::OutputSampleListPointer
OutputSampleListType::Pointer OutputSampleListPointer
Definition: otbGaussianAdditiveNoiseSampleListFilter.h:72
otb::Statistics::GaussianAdditiveNoiseSampleListFilter::GenerateData
void GenerateData() override
Definition: otbGaussianAdditiveNoiseSampleListFilter.hxx:65
otb::Statistics::GaussianAdditiveNoiseSampleListFilter::GenerateRandomSequence
void GenerateRandomSequence()
Definition: otbGaussianAdditiveNoiseSampleListFilter.hxx:42
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Statistics::GaussianAdditiveNoiseSampleListFilter::InputMeasurementVectorType
InputSampleListType::MeasurementVectorType InputMeasurementVectorType
Definition: otbGaussianAdditiveNoiseSampleListFilter.h:67
otb::Statistics::GaussianAdditiveNoiseSampleListFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbGaussianAdditiveNoiseSampleListFilter.hxx:117
otb::Statistics::GaussianAdditiveNoiseSampleListFilter::InputSampleListConstPointer
InputSampleListType::ConstPointer InputSampleListConstPointer
Definition: otbGaussianAdditiveNoiseSampleListFilter.h:66
otb::Statistics::GaussianAdditiveNoiseSampleListFilter::OutputValueType
OutputMeasurementVectorType::ValueType OutputValueType
Definition: otbGaussianAdditiveNoiseSampleListFilter.h:75
otb::Statistics::GaussianAdditiveNoiseSampleListFilter::OutputMeasurementVectorType
OutputSampleListType::MeasurementVectorType OutputMeasurementVectorType
Definition: otbGaussianAdditiveNoiseSampleListFilter.h:74
otb::Statistics::GaussianAdditiveNoiseSampleListFilter::GaussianAdditiveNoiseSampleListFilter
GaussianAdditiveNoiseSampleListFilter()
Definition: otbGaussianAdditiveNoiseSampleListFilter.hxx:34
otbGaussianAdditiveNoiseSampleListFilter.h