OTB  10.0.0
Orfeo Toolbox
otbVarianceImageFilter.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 otbVarianceImageFilter_hxx
22 #define otbVarianceImageFilter_hxx
23 
24 #include "otbVarianceImageFilter.h"
25 
26 #include "itkConstNeighborhoodIterator.h"
27 #include "itkNeighborhoodInnerProduct.h"
28 #include "itkImageRegionIterator.h"
29 #include "itkNeighborhoodAlgorithm.h"
30 #include "itkOffset.h"
31 #include "itkProgressReporter.h"
32 
33 namespace otb
34 {
35 
36 template <class TInputImage, class TOutputImage>
38 {
39  m_Radius.Fill(1);
40  this->DynamicMultiThreadingOn();
41 }
42 
43 template <class TInputImage, class TOutputImage>
45 {
46  // call the superclass' implementation of this method
47  Superclass::GenerateInputRequestedRegion();
48 
49  // get pointers to the input and output
50  typename Superclass::InputImagePointer inputPtr = const_cast<TInputImage*>(this->GetInput());
51  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
52 
53  if (!inputPtr || !outputPtr)
54  {
55  return;
56  }
57 
58  // get a copy of the input requested region (should equal the output
59  // requested region)
60  typename TInputImage::RegionType inputRequestedRegion;
61  inputRequestedRegion = inputPtr->GetRequestedRegion();
62 
63  // pad the input requested region by the operator radius
64  inputRequestedRegion.PadByRadius(m_Radius);
65 
66  // crop the input requested region at the input's largest possible region
67  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
68  {
69  inputPtr->SetRequestedRegion(inputRequestedRegion);
70  return;
71  }
72  else
73  {
74  // Couldn't crop the region (requested region is outside the largest
75  // possible region). Throw an exception.
76 
77  // store what we tried to request (prior to trying to crop)
78  inputPtr->SetRequestedRegion(inputRequestedRegion);
79 
80  // build an exception
81  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
82  e.SetLocation(ITK_LOCATION);
83  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
84  e.SetDataObject(inputPtr);
85  throw e;
86  }
87 }
88 
89 template <class TInputImage, class TOutputImage>
91 {
92  unsigned int i;
93  itk::ZeroFluxNeumannBoundaryCondition<InputImageType> nbc;
94 
95  itk::ConstNeighborhoodIterator<InputImageType> bit;
96  itk::ImageRegionIterator<OutputImageType> it;
97 
98  // Allocate output
99  typename OutputImageType::Pointer output = this->GetOutput();
100  typename InputImageType::ConstPointer input = this->GetInput();
101 
102  // Find the data-set boundary "faces"
103  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType faceList;
104  itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> bC;
105  faceList = bC(input, outputRegionForThread, m_Radius);
106 
107  typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType>::FaceListType::iterator fit;
108 
109  InputRealType sum;
110  InputRealType sumOfSquares;
111 
112  // Process each of the boundary faces. These are N-d regions which border
113  // the edge of the buffer.
114  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
115  {
116  bit = itk::ConstNeighborhoodIterator<InputImageType>(m_Radius, input, *fit);
117  unsigned int neighborhoodSize = bit.Size();
118  it = itk::ImageRegionIterator<OutputImageType>(output, *fit);
119  bit.OverrideBoundaryCondition(&nbc);
120  bit.GoToBegin();
121 
122  while (!bit.IsAtEnd())
123  {
124  sum = itk::NumericTraits<InputRealType>::Zero;
125  sumOfSquares = itk::NumericTraits<InputRealType>::Zero;
126  for (i = 0; i < neighborhoodSize; ++i)
127  {
128  const InputRealType value = static_cast<InputRealType>(bit.GetPixel(i));
129  sum += value;
130  sumOfSquares += value * value;
131  }
132 
133  // get the mean value
134  const double num = static_cast<double>(neighborhoodSize);
135  it.Set(static_cast<OutputPixelType>(sumOfSquares - (sum * sum / num)) / (num - 1.0));
136 
137  ++bit;
138  ++it;
139  }
140  }
141 }
142 
146 template <class TInputImage, class TOutput>
147 void VarianceImageFilter<TInputImage, TOutput>::PrintSelf(std::ostream& os, itk::Indent indent) const
148 {
149  Superclass::PrintSelf(os, indent);
150  os << indent << "Radius: " << m_Radius << std::endl;
151 }
153 
154 } // end namespace otb
155 
156 #endif
void PrintSelf(std::ostream &os, itk::Indent indent) const override
OutputImageType::PixelType OutputPixelType
OutputImageType::RegionType OutputImageRegionType
void GenerateInputRequestedRegion() override
itk::NumericTraits< InputPixelType >::RealType InputRealType
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.