OTB  10.0.0
Orfeo Toolbox
otbKuanImageFilter.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 otbKuanImageFilter_hxx
22 #define otbKuanImageFilter_hxx
23 
24 #include "otbKuanImageFilter.h"
25 
26 #include "itkDataObject.h"
27 #include "itkConstNeighborhoodIterator.h"
28 #include "itkNeighborhoodInnerProduct.h"
29 #include "itkImageRegionIterator.h"
30 #include "itkNeighborhoodAlgorithm.h"
31 #include "itkOffset.h"
32 
33 namespace otb
34 {
35 
39 template <class TInputImage, class TOutputImage>
41 {
42  m_Radius.Fill(1);
43  SetNbLooks(1.0);
44  this->DynamicMultiThreadingOn();
45 }
47 
48 template <class TInputImage, class TOutputImage>
50 {
51  // call the superclass' implementation of this method
52  Superclass::GenerateInputRequestedRegion();
53 
54  // get pointers to the input and output
55  typename Superclass::InputImagePointer inputPtr = const_cast<TInputImage*>(this->GetInput());
56  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
57 
58  if (!inputPtr || !outputPtr)
59  {
60  return;
61  }
62 
63  // get a copy of the input requested region (should equal the output
64  // requested region)
65  typename TInputImage::RegionType inputRequestedRegion;
66  inputRequestedRegion = inputPtr->GetRequestedRegion();
67 
68  // pad the input requested region by the operator radius
69  inputRequestedRegion.PadByRadius(m_Radius);
70 
71  // crop the input requested region at the input's largest possible region
72  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
73  {
74  inputPtr->SetRequestedRegion(inputRequestedRegion);
75  return;
76  }
77  else
78  {
79  // Couldn't crop the region (requested region is outside the largest
80  // possible region). Throw an exception.
81 
82  // store what we tried to request (prior to trying to crop)
83  inputPtr->SetRequestedRegion(inputRequestedRegion);
84 
85  // build an exception
86  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
87  std::ostringstream msg;
88  msg << static_cast<const char*>(this->GetNameOfClass()) << "::GenerateInputRequestedRegion()";
89  e.SetLocation(msg.str());
90  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
91  e.SetDataObject(inputPtr);
92  throw e;
93  }
94 }
95 
96 template <class TInputImage, class TOutputImage>
98 {
99  unsigned int i;
100  itk::ZeroFluxNeumannBoundaryCondition<InputImageType> nbc;
101 
102  itk::ConstNeighborhoodIterator<InputImageType> bit;
103  itk::ImageRegionIterator<OutputImageType> it;
104 
105  typename OutputImageType::Pointer output = this->GetOutput();
106  typename InputImageType::ConstPointer input = this->GetInput();
107 
108  // Find the data-set boundary "faces"
109  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType faceList;
110  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType::iterator fit;
111 
112  itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> bC;
113  faceList = bC(input, outputRegionForThread, m_Radius);
114 
115  // InputRealType pixel;
116  InputRealType sum;
117  InputRealType sum2;
118 
119  double Ci2, Cu2, w, E_I, I, Var_I, dPixel;
120 
121  // Compute the ratio using the number of looks
122  Cu2 = 1.0 / m_NbLooks;
123 
124  // Process each of the boundary faces. These are N-d regions which border
125  // the edge of the buffer.
126  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
127  {
128  bit = itk::ConstNeighborhoodIterator<InputImageType>(m_Radius, input, *fit);
129  const unsigned int neighborhoodSize = bit.Size();
130  it = itk::ImageRegionIterator<OutputImageType>(output, *fit);
131  bit.OverrideBoundaryCondition(&nbc);
132 
133  bit.GoToBegin();
134  it.GoToBegin();
135 
136 
137  while (!bit.IsAtEnd())
138  {
139  sum = itk::NumericTraits<InputRealType>::Zero;
140  sum2 = itk::NumericTraits<InputRealType>::Zero;
141 
142  // Parcours du voisinage
143  for (i = 0; i < neighborhoodSize; ++i)
144  {
145  dPixel = static_cast<double>(bit.GetPixel(i));
146  sum += dPixel;
147  }
148  E_I = sum / static_cast<double>(neighborhoodSize);
149 
150  for (i = 0; i < neighborhoodSize; ++i)
151  {
152  dPixel = static_cast<double>(bit.GetPixel(i));
153  sum2 += (dPixel - E_I) * (dPixel - E_I);
154  }
155  Var_I = sum2 / static_cast<double>(neighborhoodSize - 1);
156 
157  I = static_cast<double>(bit.GetCenterPixel());
158 
159  Ci2 = Var_I / (E_I * E_I);
160 
161  const double epsilon = 0.0000000001;
162  if (std::abs(E_I) < epsilon)
163  {
164  dPixel = itk::NumericTraits<OutputPixelType>::Zero;
165  }
166  else if (std::abs(Var_I) < epsilon)
167  {
168  dPixel = E_I;
169  }
170  else if (Ci2 < Cu2)
171  {
172  dPixel = E_I;
173  }
174  else
175  {
176  w = (1 - Cu2 / Ci2) / (1 + Cu2);
177  dPixel = I * w + E_I * (1 - w);
178  }
179 
180  // set the weighted value
181  it.Set(static_cast<OutputPixelType>(dPixel));
182 
183  ++bit;
184  ++it;
185 
186  }
187  }
188 }
189 
193 template <class TInputImage, class TOutput>
194 void KuanImageFilter<TInputImage, TOutput>::PrintSelf(std::ostream& os, itk::Indent indent) const
195 {
196  Superclass::PrintSelf(os, indent);
197  os << indent << "Radius: " << m_Radius << std::endl;
198 }
200 
201 } // end namespace otb
202 
203 #endif
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
void PrintSelf(std::ostream &os, itk::Indent indent) const override
void GenerateInputRequestedRegion() override
OutputImageType::RegionType OutputImageRegionType
OutputImageType::PixelType OutputPixelType
itk::NumericTraits< InputPixelType >::RealType InputRealType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.