OTB  9.0.0
Orfeo Toolbox
otbLabelMapToSimulatedImageFilter.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 otbLabelMapToSimulatedImageFilter_hxx
22 #define otbLabelMapToSimulatedImageFilter_hxx
23 
25 
26 namespace otb
27 {
28 
29 template <class TInputLabelMap, class TSimuStep1, class TSimuStep2, class TOutputImage>
31 {
32  m_NumberOfComponentsPerPixel = 3;
33  m_SatRSRFilename = "";
34  m_PathRoot = "";
35  m_Mean = 0.0;
36  m_Variance = 1e-8;
37 }
38 
39 
40 template <class TInputLabelMap, class TSimuStep1, class TSimuStep2, class TOutputImage>
42 {
43  Superclass::GenerateOutputInformation();
44 
45  OutputImageType* output = this->GetOutput();
46  output->SetNumberOfComponentsPerPixel(m_NumberOfComponentsPerPixel);
47 }
48 
49 template <class TInputLabelMap, class TSimuStep1, class TSimuStep2, class TOutputImage>
51 {
52  OutputImageType* output = this->GetOutput();
53  const InputLabelMapType* input = this->GetInput();
54 
55  typename OutputImageType::PixelType pixel;
56  pixel.SetSize(m_NumberOfComponentsPerPixel);
57  pixel.Fill(input->GetBackgroundValue());
58 
59  output->FillBuffer(pixel);
60 
61  Superclass::BeforeThreadedGenerateData();
62 }
63 
64 template <class TInputLabelMap, class TSimuStep1, class TSimuStep2, class TOutputImage>
66 {
67  ReduceSpectralResponsePointer reduceSpectralResponse = ReduceSpectralResponseType::New();
68  SatelliteRSRPointer satRSR = SatelliteRSRType::New();
69  // Compute the spectral response associated to this object.
70  SpectralResponsePointer readSpectrum = SpectralResponseType::New();
71  bool hasPath = false;
72  // Check if the spectrum associated to this object is given by a database.
73  for (unsigned int i = 0; i < labelObject->GetNumberOfAttributes(); ++i)
74  {
75  if (labelObject->GetAvailableAttributes()[i].compare("path") == 0)
76  hasPath = true;
77  }
78  if (hasPath == true)
79  {
80  readSpectrum->Load(m_PathRoot + labelObject->GetAttribute("path"), 100);
81  reduceSpectralResponse->SetInputSpectralResponse(readSpectrum);
82  }
83  else // compute the spectrum using ProSail
84  {
85  LabelToParametersPointer labelToParams = LabelToParametersType::New();
86  SimulationStep1Pointer simuStep1 = SimulationStep1Type::New();
87  SimulationStep2Pointer simuStep2 = SimulationStep2Type::New();
88 
89  // Compute params needed to the spectrum simulator
90  labelToParams->SetLabel(labelObject->GetAttribute("area"));
91  labelToParams->GenerateData();
92 
93  simuStep1->SetInput(labelToParams->GetStep1Parameters());
94 
95  simuStep2->SetParameters(labelToParams->GetStep2Parameters());
96  simuStep2->SetReflectance(simuStep1->GetReflectance());
97  simuStep2->SetTransmittance(simuStep1->GetTransmittance());
98  simuStep2->Update();
99  reduceSpectralResponse->SetInputSpectralResponse(simuStep2->GetViewingReflectance());
100  }
101  // compute the satellite response of this spectrum
102  satRSR->Clear();
103  satRSR->SetNbBands(m_NumberOfComponentsPerPixel);
104  satRSR->Load(m_SatRSRFilename);
105  reduceSpectralResponse->SetInputSatRSR(satRSR);
106  reduceSpectralResponse->CalculateResponse();
107 
108  typename OutputImageType::PixelType pixel;
109  pixel.SetSize(m_NumberOfComponentsPerPixel);
110 
111  // TODO Change with a multithreaded method
112  RandomGeneratorPointer randomGen = RandomGeneratorType::GetInstance();
113 
114  ConstLineIteratorType lit = ConstLineIteratorType(labelObject);
115 
116  while (!lit.IsAtEnd())
117  {
118  IndexType idx = lit.GetLine().GetIndex();
119  unsigned long length = lit.GetLine().GetLength();
120  for (unsigned int i = 0; i < length; ++i)
121  {
122  // add gaussian white noise
123  for (unsigned int j = 0; j < m_NumberOfComponentsPerPixel; ++j)
124  {
125  double ran = randomGen->GetNormalVariate(m_Mean, m_Variance);
126  pixel[j] = static_cast<InternalPixelType>(reduceSpectralResponse->GetReduceResponse()->GetResponse()[j].second + ran);
127  }
128  this->GetOutput()->SetPixel(idx, pixel);
129  idx[0]++;
130  }
131  ++lit;
132  }
133 }
134 
135 
136 template <class TInputLabelMap, class TSimuStep1, class TSimuStep2, class TOutputImage>
138 {
139  Superclass::PrintSelf(os, indent);
140 }
141 
142 } // end namespace otb
143 
144 #endif
otb::LabelMapToSimulatedImageFilter::SimulationStep1Pointer
SimulationStep1Type::Pointer SimulationStep1Pointer
Definition: otbLabelMapToSimulatedImageFilter.h:81
otb::LabelMapToSimulatedImageFilter::ConstLineIteratorType
LabelObjectType::ConstLineIterator ConstLineIteratorType
Definition: otbLabelMapToSimulatedImageFilter.h:70
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::LabelMapToSimulatedImageFilter::OutputImageType
TOutputImage OutputImageType
Definition: otbLabelMapToSimulatedImageFilter.h:74
otb::LabelMapToSimulatedImageFilter::LabelObjectType
InputLabelMapType::LabelObjectType LabelObjectType
Definition: otbLabelMapToSimulatedImageFilter.h:69
otb::LabelMapToSimulatedImageFilter::ThreadedProcessLabelObject
void ThreadedProcessLabelObject(LabelObjectType *labelObject) override
Definition: otbLabelMapToSimulatedImageFilter.hxx:65
otb::LabelMapToSimulatedImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbLabelMapToSimulatedImageFilter.hxx:137
otbLabelMapToSimulatedImageFilter.h
otb::LabelMapToSimulatedImageFilter::RandomGeneratorPointer
RandomGeneratorType::Pointer RandomGeneratorPointer
Definition: otbLabelMapToSimulatedImageFilter.h:98
otb::LabelMapToSimulatedImageFilter::LabelMapToSimulatedImageFilter
LabelMapToSimulatedImageFilter()
Definition: otbLabelMapToSimulatedImageFilter.hxx:30
otb::LabelMapToSimulatedImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: otbLabelMapToSimulatedImageFilter.hxx:50
otb::LabelMapToSimulatedImageFilter::SatelliteRSRPointer
SatelliteRSRType::Pointer SatelliteRSRPointer
Definition: otbLabelMapToSimulatedImageFilter.h:92
otb::LabelMapToSimulatedImageFilter::InternalPixelType
OutputImageType::InternalPixelType InternalPixelType
Definition: otbLabelMapToSimulatedImageFilter.h:78
otb::LabelMapToSimulatedImageFilter::LabelToParametersPointer
LabelToParametersType::Pointer LabelToParametersPointer
Definition: otbLabelMapToSimulatedImageFilter.h:87
otb::LabelMapToSimulatedImageFilter::SpectralResponsePointer
SpectralResponseType::Pointer SpectralResponsePointer
Definition: otbLabelMapToSimulatedImageFilter.h:90
otb::LabelMapToSimulatedImageFilter::ReduceSpectralResponsePointer
ReduceSpectralResponseType::Pointer ReduceSpectralResponsePointer
Definition: otbLabelMapToSimulatedImageFilter.h:95
otb::LabelMapToSimulatedImageFilter::IndexType
OutputImageType::IndexType IndexType
Definition: otbLabelMapToSimulatedImageFilter.h:77
otb::LabelMapToSimulatedImageFilter::SimulationStep2Pointer
SimulationStep2Type::Pointer SimulationStep2Pointer
Definition: otbLabelMapToSimulatedImageFilter.h:83
otb::LabelMapToSimulatedImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbLabelMapToSimulatedImageFilter.hxx:41
otb::LabelMapToSimulatedImageFilter::InputLabelMapType
TInputLabelMap InputLabelMapType
Definition: otbLabelMapToSimulatedImageFilter.h:66