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