Orfeo ToolBox  4.2
Orfeo ToolBox is not a black box
otbVarianceImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbVarianceImageFilter_txx
19 #define __otbVarianceImageFilter_txx
20 
21 #include "otbVarianceImageFilter.h"
22 
25 #include "itkImageRegionIterator.h"
28 #include "itkOffset.h"
29 #include "itkProgressReporter.h"
30 
31 namespace otb
32 {
33 
34 template <class TInputImage, class TOutputImage>
37 {
38  m_Radius.Fill(1);
39 }
40 
41 template <class TInputImage, class TOutputImage>
42 void
44 ::GenerateInputRequestedRegion() throw (itk::InvalidRequestedRegionError)
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 =
51  const_cast<TInputImage *>(this->GetInput());
52  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
53 
54  if (!inputPtr || !outputPtr)
55  {
56  return;
57  }
58 
59  // get a copy of the input requested region (should equal the output
60  // requested region)
61  typename TInputImage::RegionType inputRequestedRegion;
62  inputRequestedRegion = inputPtr->GetRequestedRegion();
63 
64  // pad the input requested region by the operator radius
65  inputRequestedRegion.PadByRadius(m_Radius);
66 
67  // crop the input requested region at the input's largest possible region
68  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
69  {
70  inputPtr->SetRequestedRegion(inputRequestedRegion);
71  return;
72  }
73  else
74  {
75  // Couldn't crop the region (requested region is outside the largest
76  // possible region). Throw an exception.
77 
78  // store what we tried to request (prior to trying to crop)
79  inputPtr->SetRequestedRegion(inputRequestedRegion);
80 
81  // build an exception
82  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
83  e.SetLocation(ITK_LOCATION);
84  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
85  e.SetDataObject(inputPtr);
86  throw e;
87  }
88  }
89 
90 template<class TInputImage, class TOutputImage>
91 void
93 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
94  itk::ThreadIdType threadId)
95 {
96  unsigned int i;
98 
101 
102  // Allocate output
103  typename OutputImageType::Pointer output = this->GetOutput();
104  typename InputImageType::ConstPointer input = this->GetInput();
105 
106  // Find the data-set boundary "faces"
109  faceList = bC(input, outputRegionForThread, m_Radius);
110 
112 
113  // support progress methods/callbacks
114  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
115 
116  InputRealType sum;
117  InputRealType sumOfSquares;
118 
119  // Process each of the boundary faces. These are N-d regions which border
120  // the edge of the buffer.
121  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
122  {
124  input, *fit);
125  unsigned int neighborhoodSize = bit.Size();
127  bit.OverrideBoundaryCondition(&nbc);
128  bit.GoToBegin();
129 
130  while (!bit.IsAtEnd())
131  {
134  for (i = 0; i < neighborhoodSize; ++i)
135  {
136  const InputRealType value = static_cast<InputRealType>(bit.GetPixel(i));
137  sum += value;
138  sumOfSquares += value * value;
139  }
140 
141  // get the mean value
142  const double num = static_cast<double>(neighborhoodSize);
143  it.Set(static_cast<OutputPixelType>(sumOfSquares - (sum * sum / num)) / (num - 1.0));
144 
145  ++bit;
146  ++it;
147  progress.CompletedPixel();
148  }
149  }
150 }
151 
155 template <class TInputImage, class TOutput>
156 void
159  std::ostream& os,
160  itk::Indent indent) const
161 {
162  Superclass::PrintSelf(os, indent);
163  os << indent << "Radius: " << m_Radius << std::endl;
164 
165 }
166 
167 } // end namespace otb
168 
169 #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
NeighborIndexType Size() const
virtual PixelType GetPixel(NeighborIndexType i, bool &IsInBounds) const
itk::NumericTraits< InputPixelType >::RealType InputRealType
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId)
OutputImageType::RegionType OutputImageRegionType
unsigned int ThreadIdType