OTB  10.0.0
Orfeo Toolbox
otbSpectralAngleDistanceImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 otbSpectralAngleDistanceImageFilter_hxx
22 #define otbSpectralAngleDistanceImageFilter_hxx
23 
25 #include "itkImageRegionIterator.h"
26 #include "itkProgressReporter.h"
27 #include "otbMacro.h"
28 #include "otbMath.h"
29 
30 namespace otb
31 {
35 template <class TInputImage, class TOutputImage>
37 {
38  this->DynamicMultiThreadingOn();
39  m_ReferencePixel = 0;
40 }
42 
43 template <class TInputImage, class TOutputImage>
45 {
46  if (this->GetInput()->GetNumberOfComponentsPerPixel() == 1)
47  {
48  itkExceptionMacro(<< "Not valid input image : mono channel image gives a nul output image.");
49  }
50 }
51 
52 template <class TInputImage, class TOutputImage>
53 void SpectralAngleDistanceImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData(const OutputImageRegionType& outputRegionForThread)
54 {
55 
56  if (m_ReferencePixel.Size() == 0)
57  {
58  itkExceptionMacro(<< "Reference pixel is not set!");
59  }
60 
61  InputImageConstPointerType inputPtr = this->GetInput();
62  OutputImagePointerType outputPtr = this->GetOutput();
63 
64  // inputPtr->UpdateOutputInformation();
65  // Check if the reference pixel size matches the input image number of components.
66  if (m_ReferencePixel.GetSize() != inputPtr->GetNumberOfComponentsPerPixel())
67  {
68  itkExceptionMacro(<< "Reference pixel size (" << m_ReferencePixel.GetSize() << " and input image pixel size (" << inputPtr->GetNumberOfComponentsPerPixel()
69  << ") don't match!");
70  }
71 
72  // Define the portion of the input to walk for this thread, using
73  // the CallCopyOutputRegionToInputRegion method allows for the input
74  // and output images to be different dimensions
75  InputImageRegionType inputRegionForThread;
76  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
77 
78  // Define the iterators
79  itk::ImageRegionConstIterator<InputImageType> inputIt(inputPtr, inputRegionForThread);
80  itk::ImageRegionIterator<OutputImageType> outputIt(outputPtr, outputRegionForThread);
81 
82  inputIt.GoToBegin();
83  outputIt.GoToBegin();
84 
85  while (!inputIt.IsAtEnd() && !outputIt.IsAtEnd())
86  {
87  double dist = 0.0;
88  double scalarProd = 0.0;
89  double normProd = 0.0;
90  double normProd1 = 0.0;
91  double normProd2 = 0.0;
92  InputPixelType pixel = inputIt.Get();
93  for (unsigned int i = 0; i < pixel.Size(); ++i)
94  {
95  scalarProd += pixel[i] * m_ReferencePixel[i];
96  normProd1 += pixel[i] * pixel[i];
97  normProd2 += m_ReferencePixel[i] * m_ReferencePixel[i];
98  }
99  normProd = normProd1 * normProd2;
100 
101  if (normProd == 0.0)
102  {
103  dist = 0.0;
104  }
105  else
106  {
107  dist = std::acos(scalarProd / std::sqrt(normProd));
108  }
109  //------ This part was suppressed since the filter must perform only the spectral angle computation ---
110  // Spectral angle normalization
111  // dist = dist/(CONST_PI_2);
112  // square ponderation
113  // dist = std::sqrt(dist);
114  outputIt.Set(static_cast<OutputPixelType>(dist));
115  ++inputIt;
116  ++outputIt;
117  }
118 }
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.