OTB  10.0.0
Orfeo Toolbox
otbFrostImageFilter.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 otbFrostImageFilter_hxx
22 #define otbFrostImageFilter_hxx
23 
24 #include "otbFrostImageFilter.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  m_Deramp = 2;
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  itk::ConstNeighborhoodIterator<InputImageType> bit;
102  typename itk::ConstNeighborhoodIterator<InputImageType>::OffsetType off;
103  itk::ImageRegionIterator<OutputImageType> it;
104 
105  // Allocate output
106  typename OutputImageType::Pointer output = this->GetOutput();
107  typename InputImageType::ConstPointer input = this->GetInput();
108 
109  // Find the data-set boundary "faces"
110  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType faceList;
111  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType::iterator fit;
112 
113  itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> bC;
114  faceList = bC(input, outputRegionForThread, m_Radius);
115 
116  InputRealType sum;
117  InputRealType sum2;
118 
119  double Mean, Variance;
120  double Alpha;
121  double NormFilter;
122  double FrostFilter;
123  double CoefFilter;
124  double dPixel;
125 
126  // Process each of the boundary faces. These are N-d regions which border
127  // the edge of the buffer.
128  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
129  {
130  bit = itk::ConstNeighborhoodIterator<InputImageType>(m_Radius, input, *fit);
131  unsigned int neighborhoodSize = bit.Size();
132  it = itk::ImageRegionIterator<OutputImageType>(output, *fit);
133  bit.OverrideBoundaryCondition(&nbc);
134 
135  bit.GoToBegin();
136  it.GoToBegin();
137 
138  while (!bit.IsAtEnd())
139  {
140  sum = itk::NumericTraits<InputRealType>::Zero;
141  sum2 = itk::NumericTraits<InputRealType>::Zero;
142 
143  for (i = 0; i < neighborhoodSize; ++i)
144  {
145  dPixel = static_cast<double>(bit.GetPixel(i));
146  sum += dPixel;
147  }
148  Mean = 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 - Mean) * (dPixel - Mean);
154  }
155  Variance = sum2 / double(neighborhoodSize - 1);
156 
157  const double epsilon = 0.0000000001;
158  if (std::abs(Mean) < epsilon)
159  {
160  dPixel = itk::NumericTraits<OutputPixelType>::Zero;
161  }
162  else if (std::abs(Variance) < epsilon)
163  {
164  dPixel = Mean;
165  }
166  else
167  {
168  Alpha = m_Deramp * Variance / (Mean * Mean);
169 
170  NormFilter = 0.0;
171  FrostFilter = 0.0;
172 
173  const int rad_x = m_Radius[0];
174  const int rad_y = m_Radius[1];
175 
176  for (int x = -rad_x; x <= rad_x; ++x)
177  {
178  for (int y = -rad_y; y <= rad_y; ++y)
179  {
180  double Dist = std::sqrt(static_cast<double>(x * x + y * y));
181  off[0] = x;
182  off[1] = y;
183 
184  dPixel = static_cast<double>(bit.GetPixel(off));
185 
186  CoefFilter = std::exp(-Alpha * Dist);
187  NormFilter += CoefFilter;
188  FrostFilter += (CoefFilter * dPixel);
189  }
190  }
191 
192  dPixel = FrostFilter / NormFilter;
193  }
194 
195  it.Set(static_cast<OutputPixelType>(dPixel));
196 
197  ++bit;
198  ++it;
199  }
200  }
201 }
202 
206 template <class TInputImage, class TOutput>
207 void FrostImageFilter<TInputImage, TOutput>::PrintSelf(std::ostream& os, itk::Indent indent) const
208 {
209  Superclass::PrintSelf(os, indent);
210  os << indent << "Radius: " << m_Radius << std::endl;
211 }
213 
214 } // end namespace otb
215 
216 #endif
itk::NumericTraits< InputPixelType >::RealType InputRealType
void GenerateInputRequestedRegion() override
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
OutputImageType::RegionType OutputImageRegionType
OutputImageType::PixelType OutputPixelType
void PrintSelf(std::ostream &os, itk::Indent indent) const override
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.