OTB  6.1.0
Orfeo Toolbox
otbVarianceImageFilter.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 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_txx
22 #define otbVarianceImageFilter_txx
23 
24 #include "otbVarianceImageFilter.h"
25 
28 #include "itkImageRegionIterator.h"
30 #include "itkOffset.h"
31 #include "itkProgressReporter.h"
32 
33 namespace otb
34 {
35 
36 template <class TInputImage, class TOutputImage>
39 {
40  m_Radius.Fill(1);
41 }
42 
43 template <class TInputImage, class TOutputImage>
44 void
46 ::GenerateInputRequestedRegion() throw (itk::InvalidRequestedRegionError)
47  {
48  // call the superclass' implementation of this method
49  Superclass::GenerateInputRequestedRegion();
50 
51  // get pointers to the input and output
52  typename Superclass::InputImagePointer inputPtr =
53  const_cast<TInputImage *>(this->GetInput());
54  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
55 
56  if (!inputPtr || !outputPtr)
57  {
58  return;
59  }
60 
61  // get a copy of the input requested region (should equal the output
62  // requested region)
63  typename TInputImage::RegionType inputRequestedRegion;
64  inputRequestedRegion = inputPtr->GetRequestedRegion();
65 
66  // pad the input requested region by the operator radius
67  inputRequestedRegion.PadByRadius(m_Radius);
68 
69  // crop the input requested region at the input's largest possible region
70  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
71  {
72  inputPtr->SetRequestedRegion(inputRequestedRegion);
73  return;
74  }
75  else
76  {
77  // Couldn't crop the region (requested region is outside the largest
78  // possible region). Throw an exception.
79 
80  // store what we tried to request (prior to trying to crop)
81  inputPtr->SetRequestedRegion(inputRequestedRegion);
82 
83  // build an exception
84  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
85  e.SetLocation(ITK_LOCATION);
86  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
87  e.SetDataObject(inputPtr);
88  throw e;
89  }
90  }
91 
92 template<class TInputImage, class TOutputImage>
93 void
95 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
96  itk::ThreadIdType threadId)
97 {
98  unsigned int i;
100 
103 
104  // Allocate output
105  typename OutputImageType::Pointer output = this->GetOutput();
106  typename InputImageType::ConstPointer input = this->GetInput();
107 
108  // Find the data-set boundary "faces"
111  faceList = bC(input, outputRegionForThread, m_Radius);
112 
114 
115  // support progress methods/callbacks
116  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
117 
118  InputRealType sum;
119  InputRealType sumOfSquares;
120 
121  // Process each of the boundary faces. These are N-d regions which border
122  // the edge of the buffer.
123  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
124  {
126  input, *fit);
127  unsigned int neighborhoodSize = bit.Size();
129  bit.OverrideBoundaryCondition(&nbc);
130  bit.GoToBegin();
131 
132  while (!bit.IsAtEnd())
133  {
136  for (i = 0; i < neighborhoodSize; ++i)
137  {
138  const InputRealType value = static_cast<InputRealType>(bit.GetPixel(i));
139  sum += value;
140  sumOfSquares += value * value;
141  }
142 
143  // get the mean value
144  const double num = static_cast<double>(neighborhoodSize);
145  it.Set(static_cast<OutputPixelType>(sumOfSquares - (sum * sum / num)) / (num - 1.0));
146 
147  ++bit;
148  ++it;
149  progress.CompletedPixel();
150  }
151  }
152 }
153 
157 template <class TInputImage, class TOutput>
158 void
161  std::ostream& os,
162  itk::Indent indent) const
163 {
164  Superclass::PrintSelf(os, indent);
165  os << indent << "Radius: " << m_Radius << std::endl;
167 
168 }
169 
170 } // end namespace otb
171 
172 #endif
virtual bool IsAtEnd() const
void SetDataObject(DataObject *dobj)
virtual void SetDescription(const std::string &s)
virtual void SetLocation(const std::string &s)
void Set(const PixelType &value) const
virtual void OverrideBoundaryCondition(const ImageBoundaryConditionPointerType i)
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
void GenerateInputRequestedRegion() ITK_OVERRIDE
virtual PixelType GetPixel(NeighborIndexType i, bool &IsInBounds) const
itk::NumericTraits< InputPixelType >::RealType InputRealType
unsigned int ThreadIdType
OutputImageType::RegionType OutputImageRegionType
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) ITK_OVERRIDE