OTB  9.0.0
Orfeo Toolbox
otbRadianceToReflectanceImageFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbRadianceToReflectanceImageFilter_h
23 #define otbRadianceToReflectanceImageFilter_h
24 
25 #include "otbVarSol.h"
27 #include "otbMacro.h"
28 #include <iomanip>
29 
30 namespace otb
31 {
32 namespace Functor
33 {
50 template <class TInput, class TOutput>
52 {
53 public:
55  {
56  }
57 
59  {
60  }
61 
62  void SetSolarIllumination(double solarIllumination)
63  {
64  m_SolarIllumination = solarIllumination;
65  }
67  {
69  }
70  void SetUseClamp(bool useClamp)
71  {
72  m_UseClamp = useClamp;
73  }
74 
76  {
77  return m_SolarIllumination;
78  }
80  {
82  }
83  bool GetUseClamp()
84  {
85  return m_UseClamp;
86  }
87 
88  inline TOutput operator()(const TInput& inPixel) const
89  {
90  TOutput outPixel;
91  double temp;
92  temp = static_cast<double>(inPixel) * static_cast<double>(CONST_PI) * m_IlluminationCorrectionCoefficient / m_SolarIllumination;
93 
94  if (m_UseClamp)
95  {
96  temp = std::max(temp, 0.);
97  temp = std::min(temp, 1.);
98  }
99  outPixel = static_cast<TOutput>(temp);
100 
101  return outPixel;
102  }
103 
104 private:
108 };
109 }
110 
128 template <class TInputImage, class TOutputImage>
131  TInputImage, TOutputImage,
132  typename Functor::RadianceToReflectanceImageFunctor<typename TInputImage::InternalPixelType, typename TOutputImage::InternalPixelType>>
133 {
134 public:
136  itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
137  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);
139 
141  typedef TInputImage InputImageType;
142  typedef TOutputImage OutputImageType;
145 
149  typedef itk::SmartPointer<Self> Pointer;
150  typedef itk::SmartPointer<const Self> ConstPointer;
151 
153  itkNewMacro(Self);
154 
156  itkTypeMacro(RadianceToReflectanceImageFilter, UnaryImageFunctorWithVectorImageFiltermageFilter);
157 
159  typedef typename InputImageType::PixelType InputPixelType;
160  typedef typename InputImageType::InternalPixelType InputInternalPixelType;
161  typedef typename InputImageType::RegionType InputImageRegionType;
162  typedef typename OutputImageType::PixelType OutputPixelType;
163  typedef typename OutputImageType::InternalPixelType OutputInternalPixelType;
164  typedef typename OutputImageType::RegionType OutputImageRegionType;
165 
166  typedef typename itk::VariableLengthVector<double> VectorType;
167 
169  typedef typename InputImageType::SizeType SizeType;
170 
172  itkSetMacro(SolarIllumination, VectorType);
173 
175  itkGetConstReferenceMacro(SolarIllumination, VectorType);
176 
178  itkSetMacro(ZenithalSolarAngle, double);
179 
181  itkGetConstReferenceMacro(ZenithalSolarAngle, double);
182 
184  virtual void SetElevationSolarAngle(double elevationAngle)
185  {
186  double zenithalAngle = 90.0 - elevationAngle;
187  if (this->m_ZenithalSolarAngle != zenithalAngle)
188  {
189  this->m_ZenithalSolarAngle = zenithalAngle;
190  this->Modified();
191  }
192  }
194 
195  virtual double GetElevationSolarAngle() const
196  {
197  return 90.0 - this->m_ZenithalSolarAngle;
198  }
199 
201  itkSetClampMacro(Day, int, 1, 31);
202 
204  itkGetConstReferenceMacro(Day, int);
205 
207  itkSetClampMacro(Month, int, 1, 12);
208 
210  itkGetConstReferenceMacro(Month, int);
211 
214  {
215  m_FluxNormalizationCoefficient = coef;
216  m_IsSetFluxNormalizationCoefficient = true;
217  this->Modified();
218  }
219 
221  itkGetConstReferenceMacro(FluxNormalizationCoefficient, double);
222 
224  void SetSolarDistance(double value)
225  {
226  m_SolarDistance = value;
227  m_IsSetSolarDistance = true;
228  this->Modified();
229  }
230 
232  itkGetConstReferenceMacro(SolarDistance, double);
233 
235  itkSetMacro(IsSetSolarDistance, bool);
236 
238  itkGetConstReferenceMacro(IsSetSolarDistance, bool);
239 
241  itkSetMacro(IsSetFluxNormalizationCoefficient, bool);
242 
244  itkGetConstReferenceMacro(IsSetFluxNormalizationCoefficient, bool);
245 
247  itkSetMacro(UseClamp, bool);
248 
250  itkGetConstReferenceMacro(UseClamp, bool);
251 
252 protected:
255  : m_ZenithalSolarAngle(120.0), // invalid value which will lead to negative radiometry
256  m_FluxNormalizationCoefficient(1.),
257  m_Day(0),
258  m_Month(0),
259  m_SolarDistance(1.0),
260  m_IsSetFluxNormalizationCoefficient(false),
261  m_IsSetSolarDistance(false),
262  m_UseClamp(true)
263  {
264  m_SolarIllumination.SetSize(0);
265  };
267 
270  {
271  }
272 
274  void BeforeThreadedGenerateData(void) override
275  {
276  const auto & metadata = this->GetInput()->GetImageMetadata();
277 
278  if (m_Day == 0 && (!m_IsSetFluxNormalizationCoefficient) && (!m_IsSetSolarDistance)
279  && metadata.Has(MDTime::AcquisitionDate))
280  {
281  m_Day = metadata[MDTime::AcquisitionDate].GetDay();
282  }
283 
284  if (m_Month == 0 && (!m_IsSetFluxNormalizationCoefficient) && (!m_IsSetSolarDistance)
285  && metadata.Has(MDTime::AcquisitionDate))
286  {
287  m_Month = metadata[MDTime::AcquisitionDate].GetMonth();
288  }
289 
290  if (m_SolarIllumination.GetSize() == 0 && metadata.HasBandMetadata(MDNum::SolarIrradiance))
291  {
292  m_SolarIllumination = metadata.GetAsVector(MDNum::SolarIrradiance);
293  }
294 
295  if (m_ZenithalSolarAngle == 120.0 && metadata.Has(MDNum::SunElevation))
296  {
297  // the zenithal angle is the complementary of the elevation angle
298  m_ZenithalSolarAngle = 90.0 - metadata[MDNum::SunElevation];
299  }
300 
301  otbMsgDevMacro(<< "Using correction parameters: ");
302  otbMsgDevMacro(<< "Day: " << m_Day);
303  otbMsgDevMacro(<< "Month: " << m_Month);
304  otbMsgDevMacro(<< "Solar irradiance: " << m_SolarIllumination);
305  otbMsgDevMacro(<< "Zenithal angle: " << m_ZenithalSolarAngle);
306 
307  if ((m_SolarIllumination.GetSize() != this->GetInput()->GetNumberOfComponentsPerPixel()))
308  {
309  itkExceptionMacro(<< "SolarIllumination parameter should have the same size as the number of bands");
310  }
311 
312  this->GetFunctorVector().clear();
313 
314  for (unsigned int i = 0; i < this->GetInput()->GetNumberOfComponentsPerPixel(); ++i)
315  {
316  FunctorType functor;
317  double coefTemp = 0.;
318  if (m_IsSetFluxNormalizationCoefficient)
319  {
320  coefTemp = std::cos(m_ZenithalSolarAngle * CONST_PI_180) * m_FluxNormalizationCoefficient * m_FluxNormalizationCoefficient;
321  }
322  else if (m_IsSetSolarDistance)
323  {
324  coefTemp = std::cos(m_ZenithalSolarAngle * CONST_PI_180) / (m_SolarDistance * m_SolarDistance);
325  }
326  else if (m_Day * m_Month != 0 && m_Day < 32 && m_Month < 13)
327  {
328  coefTemp = std::cos(m_ZenithalSolarAngle * CONST_PI_180) * VarSol::GetVarSol(m_Day, m_Month);
329  }
330  else
331  {
332  itkExceptionMacro(<< "Day has to be included between 1 and 31, Month between 1 and 12.");
333  }
334 
335  functor.SetIlluminationCorrectionCoefficient(1. / coefTemp);
336  functor.SetSolarIllumination(static_cast<double>(m_SolarIllumination[i]));
337  functor.SetUseClamp(m_UseClamp);
338 
339  this->GetFunctorVector().push_back(functor);
340  }
341  }
342 
343 private:
346 
349 
351  int m_Day;
352 
354  int m_Month;
355 
358 
361 
365 
369 
372 };
373 
374 } // end namespace otb
375 #endif
otb::RadianceToReflectanceImageFilter::SetSolarDistance
void SetSolarDistance(double value)
Definition: otbRadianceToReflectanceImageFilter.h:224
otb::Functor::RadianceToReflectanceImageFunctor::operator()
TOutput operator()(const TInput &inPixel) const
Definition: otbRadianceToReflectanceImageFilter.h:88
otb::Functor::RadianceToReflectanceImageFunctor::GetIlluminationCorrectionCoefficient
double GetIlluminationCorrectionCoefficient()
Definition: otbRadianceToReflectanceImageFilter.h:79
otb::RadianceToReflectanceImageFilter::InputImageRegionType
InputImageType::RegionType InputImageRegionType
Definition: otbRadianceToReflectanceImageFilter.h:161
otb::CONST_PI
constexpr double CONST_PI
Definition: otbMath.h:49
otb::RadianceToReflectanceImageFilter::m_IsSetSolarDistance
bool m_IsSetSolarDistance
Definition: otbRadianceToReflectanceImageFilter.h:368
otb::RadianceToReflectanceImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData(void) override
Definition: otbRadianceToReflectanceImageFilter.h:274
otb::RadianceToReflectanceImageFilter::OutputPixelType
OutputImageType::PixelType OutputPixelType
Definition: otbRadianceToReflectanceImageFilter.h:162
otb::RadianceToReflectanceImageFilter::GetElevationSolarAngle
virtual double GetElevationSolarAngle() const
Definition: otbRadianceToReflectanceImageFilter.h:195
otb::MDTime::AcquisitionDate
@ AcquisitionDate
otb::RadianceToReflectanceImageFilter
Convert radiance value into reflectance value.
Definition: otbRadianceToReflectanceImageFilter.h:129
otb::Functor::RadianceToReflectanceImageFunctor::GetUseClamp
bool GetUseClamp()
Definition: otbRadianceToReflectanceImageFilter.h:83
otb::RadianceToReflectanceImageFilter::Superclass
UnaryImageFunctorWithVectorImageFilter< InputImageType, OutputImageType, FunctorType > Superclass
Definition: otbRadianceToReflectanceImageFilter.h:148
otbVarSol.h
otb::RadianceToReflectanceImageFilter::FunctorType
Functor::RadianceToReflectanceImageFunctor< typename InputImageType::InternalPixelType, typename OutputImageType::InternalPixelType > FunctorType
Definition: otbRadianceToReflectanceImageFilter.h:144
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::RadianceToReflectanceImageFilter::Self
RadianceToReflectanceImageFilter Self
Definition: otbRadianceToReflectanceImageFilter.h:147
otb::Functor::RadianceToReflectanceImageFunctor::~RadianceToReflectanceImageFunctor
virtual ~RadianceToReflectanceImageFunctor()
Definition: otbRadianceToReflectanceImageFilter.h:58
otbMacro.h
otb::RadianceToReflectanceImageFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbRadianceToReflectanceImageFilter.h:149
otb::RadianceToReflectanceImageFilter::m_ZenithalSolarAngle
double m_ZenithalSolarAngle
Definition: otbRadianceToReflectanceImageFilter.h:345
otb::RadianceToReflectanceImageFilter::VectorType
itk::VariableLengthVector< double > VectorType
Definition: otbRadianceToReflectanceImageFilter.h:166
otb::RadianceToReflectanceImageFilter::InputImageType
TInputImage InputImageType
Definition: otbRadianceToReflectanceImageFilter.h:141
otb::Functor::RadianceToReflectanceImageFunctor::m_IlluminationCorrectionCoefficient
double m_IlluminationCorrectionCoefficient
Definition: otbRadianceToReflectanceImageFilter.h:106
otb::CONST_PI_180
constexpr double CONST_PI_180
Definition: otbMath.h:56
otb::RadianceToReflectanceImageFilter::OutputImageType
TOutputImage OutputImageType
Definition: otbRadianceToReflectanceImageFilter.h:142
otb::UnaryImageFunctorWithVectorImageFilter
Applies a functor to a VectorImage.
Definition: otbUnaryImageFunctorWithVectorImageFilter.h:46
otb::RadianceToReflectanceImageFilter::m_FluxNormalizationCoefficient
double m_FluxNormalizationCoefficient
Definition: otbRadianceToReflectanceImageFilter.h:348
otb::RadianceToReflectanceImageFilter::SetFluxNormalizationCoefficient
void SetFluxNormalizationCoefficient(double coef)
Definition: otbRadianceToReflectanceImageFilter.h:213
otb::Functor::RadianceToReflectanceImageFunctor::m_UseClamp
bool m_UseClamp
Definition: otbRadianceToReflectanceImageFilter.h:107
otb::Functor::RadianceToReflectanceImageFunctor::SetIlluminationCorrectionCoefficient
void SetIlluminationCorrectionCoefficient(double coef)
Definition: otbRadianceToReflectanceImageFilter.h:66
otb::RadianceToReflectanceImageFilter::~RadianceToReflectanceImageFilter
~RadianceToReflectanceImageFilter() override
Definition: otbRadianceToReflectanceImageFilter.h:269
otb::RadianceToReflectanceImageFilter::RadianceToReflectanceImageFilter
RadianceToReflectanceImageFilter()
Definition: otbRadianceToReflectanceImageFilter.h:254
otb::Functor::RadianceToReflectanceImageFunctor::RadianceToReflectanceImageFunctor
RadianceToReflectanceImageFunctor()
Definition: otbRadianceToReflectanceImageFilter.h:54
otb::RadianceToReflectanceImageFilter::SizeType
InputImageType::SizeType SizeType
Definition: otbRadianceToReflectanceImageFilter.h:169
otb::RadianceToReflectanceImageFilter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbRadianceToReflectanceImageFilter.h:150
otb::Functor::RadianceToReflectanceImageFunctor
Compute reflectance from the radiance value.
Definition: otbRadianceToReflectanceImageFilter.h:51
otb::RadianceToReflectanceImageFilter::m_SolarDistance
double m_SolarDistance
Definition: otbRadianceToReflectanceImageFilter.h:357
otb::Functor::RadianceToReflectanceImageFunctor::SetSolarIllumination
void SetSolarIllumination(double solarIllumination)
Definition: otbRadianceToReflectanceImageFilter.h:62
otb::RadianceToReflectanceImageFilter::m_SolarIllumination
VectorType m_SolarIllumination
Definition: otbRadianceToReflectanceImageFilter.h:360
otbUnaryImageFunctorWithVectorImageFilter.h
otb::Functor::RadianceToReflectanceImageFunctor::GetSolarIllumination
double GetSolarIllumination()
Definition: otbRadianceToReflectanceImageFilter.h:75
otb::RadianceToReflectanceImageFilter::InputPixelType
InputImageType::PixelType InputPixelType
Definition: otbRadianceToReflectanceImageFilter.h:156
otb::VarSol::GetVarSol
static double GetVarSol(const int day, const int month)
Definition: otbVarSol.h:47
otbMsgDevMacro
#define otbMsgDevMacro(x)
Definition: otbMacro.h:64
otb::RadianceToReflectanceImageFilter::m_Month
int m_Month
Definition: otbRadianceToReflectanceImageFilter.h:354
otb::RadianceToReflectanceImageFilter::SetElevationSolarAngle
virtual void SetElevationSolarAngle(double elevationAngle)
Definition: otbRadianceToReflectanceImageFilter.h:184
otb::RadianceToReflectanceImageFilter::m_UseClamp
bool m_UseClamp
Definition: otbRadianceToReflectanceImageFilter.h:371
otb::RadianceToReflectanceImageFilter::m_IsSetFluxNormalizationCoefficient
bool m_IsSetFluxNormalizationCoefficient
Definition: otbRadianceToReflectanceImageFilter.h:364
otb::RadianceToReflectanceImageFilter::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbRadianceToReflectanceImageFilter.h:164
otb::RadianceToReflectanceImageFilter::OutputInternalPixelType
OutputImageType::InternalPixelType OutputInternalPixelType
Definition: otbRadianceToReflectanceImageFilter.h:163
otb::Functor::RadianceToReflectanceImageFunctor::m_SolarIllumination
double m_SolarIllumination
Definition: otbRadianceToReflectanceImageFilter.h:105
otb::RadianceToReflectanceImageFilter::InputInternalPixelType
InputImageType::InternalPixelType InputInternalPixelType
Definition: otbRadianceToReflectanceImageFilter.h:160
otb::Functor::RadianceToReflectanceImageFunctor::SetUseClamp
void SetUseClamp(bool useClamp)
Definition: otbRadianceToReflectanceImageFilter.h:70
otb::RadianceToReflectanceImageFilter::m_Day
int m_Day
Definition: otbRadianceToReflectanceImageFilter.h:351
otb::MDNum::SolarIrradiance
@ SolarIrradiance
otb::MDNum::SunElevation
@ SunElevation