OTB  5.0.0
Orfeo Toolbox
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"
27 #include "itkOffset.h"
28 #include "itkProgressReporter.h"
29 
30 namespace otb
31 {
32 
33 template <class TInputImage, class TOutputImage>
36 {
37  m_Radius.Fill(1);
38 }
39 
40 template <class TInputImage, class TOutputImage>
41 void
43 ::GenerateInputRequestedRegion() throw (itk::InvalidRequestedRegionError)
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 =
50  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>
90 void
92 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
93  itk::ThreadIdType threadId)
94 {
95  unsigned int i;
97 
100 
101  // Allocate output
102  typename OutputImageType::Pointer output = this->GetOutput();
103  typename InputImageType::ConstPointer input = this->GetInput();
104 
105  // Find the data-set boundary "faces"
108  faceList = bC(input, outputRegionForThread, m_Radius);
109 
111 
112  // support progress methods/callbacks
113  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
114 
115  InputRealType sum;
116  InputRealType sumOfSquares;
117 
118  // Process each of the boundary faces. These are N-d regions which border
119  // the edge of the buffer.
120  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
121  {
123  input, *fit);
124  unsigned int neighborhoodSize = bit.Size();
126  bit.OverrideBoundaryCondition(&nbc);
127  bit.GoToBegin();
128 
129  while (!bit.IsAtEnd())
130  {
133  for (i = 0; i < neighborhoodSize; ++i)
134  {
135  const InputRealType value = static_cast<InputRealType>(bit.GetPixel(i));
136  sum += value;
137  sumOfSquares += value * value;
138  }
139 
140  // get the mean value
141  const double num = static_cast<double>(neighborhoodSize);
142  it.Set(static_cast<OutputPixelType>(sumOfSquares - (sum * sum / num)) / (num - 1.0));
143 
144  ++bit;
145  ++it;
146  progress.CompletedPixel();
147  }
148  }
149 }
150 
154 template <class TInputImage, class TOutput>
155 void
158  std::ostream& os,
159  itk::Indent indent) const
160 {
161  Superclass::PrintSelf(os, indent);
162  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
virtual PixelType GetPixel(NeighborIndexType i, bool &IsInBounds) const
itk::NumericTraits< InputPixelType >::RealType InputRealType
OutputImageType::RegionType OutputImageRegionType
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId)
unsigned int ThreadIdType