Orfeo ToolBox  4.2
Orfeo ToolBox is not a black box
otbLeeImageFilter.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 __otbLeeImageFilter_txx
19 #define __otbLeeImageFilter_txx
20 
21 #include "otbLeeImageFilter.h"
22 
23 #include "itkDataObject.h"
24 #include "itkMacro.h"
27 #include "itkImageRegionIterator.h"
30 #include "itkOffset.h"
31 #include "itkProgressReporter.h"
32 
33 namespace otb
34 {
35 
39 template <class TInputImage, class TOutputImage>
41 {
42  m_Radius.Fill(1);
43  SetNbLooks(1.0);
44 }
45 
46 template <class TInputImage, class TOutputImage>
47 void LeeImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion() throw (itk::InvalidRequestedRegionError)
48  {
49  // call the superclass' implementation of this method
50  Superclass::GenerateInputRequestedRegion();
51 
52  // get pointers to the input and output
53  typename Superclass::InputImagePointer inputPtr = 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  std::ostringstream msg;
86  msg << static_cast<const char *>(this->GetNameOfClass())
87  << "::GenerateInputRequestedRegion()";
88  e.SetLocation(msg.str().c_str());
89  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
90  e.SetDataObject(inputPtr);
91  throw e;
92  }
93  }
94 
95 template<class TInputImage, class TOutputImage>
97  const OutputImageRegionType& outputRegionForThread,
98  itk::ThreadIdType threadId
99  )
100 {
101  unsigned int i;
103 
106 
107  // Allocate output
108  typename OutputImageType::Pointer output = this->GetOutput();
109  typename InputImageType::ConstPointer input = this->GetInput();
110 
111  // Find the data-set boundary "faces"
114 
116  faceList = bC(input, outputRegionForThread, m_Radius);
117 
118  // support progress methods/callbacks
119  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
120 
121  // InputRealType pixel;
122  InputRealType sum;
123  InputRealType sum2;
124 
125  double Cr2, Cv2, E_I, I, Var_I, dPixel;
126 
127  //Compute the ratio using the number of looks
128  Cv2 = 1. / (vcl_sqrt(m_NbLooks));
129  Cv2 *= Cv2;
130 
131  // Process each of the boundary faces. These are N-d regions which border
132  // the edge of the buffer.
133  for (fit = faceList.begin(); fit != faceList.end(); ++fit)
134  {
135  bit = itk::ConstNeighborhoodIterator<InputImageType>(m_Radius, input, *fit);
136  const unsigned int neighborhoodSize = bit.Size();
138  bit.OverrideBoundaryCondition(&nbc);
139  bit.GoToBegin();
140 
141  while (!bit.IsAtEnd())
142  {
145  //Parcours du voisinage
146  for (i = 0; i < neighborhoodSize; ++i)
147  {
148  dPixel = static_cast<double>(bit.GetPixel(i));
149  sum += dPixel;
150  sum2 += dPixel * dPixel;
151  }
152  E_I = sum / static_cast<double>(neighborhoodSize);
153  Var_I = sum2 / static_cast<double>(neighborhoodSize) - E_I * E_I;
154  I = static_cast<double>(bit.GetCenterPixel());
155 
156  const double epsilon = 0.0000000001;
157  if (vcl_abs(E_I) < epsilon)
158  {
160  }
161  else
162  {
163  Cr2 = Var_I / (E_I * E_I);
164  dPixel = E_I + ((I - E_I) * (Cr2)) / (Cr2 + Cv2);
165 
166  }
167  // get the mean value
168  it.Set(static_cast<OutputPixelType>(dPixel));
169 
170  ++bit;
171  ++it;
172  progress.CompletedPixel();
173  }
174  }
175 }
176 
180 template <class TInputImage, class TOutput>
181 void
183 {
184  Superclass::PrintSelf(os, indent);
185  os << indent << "Radius: " << m_Radius << std::endl;
186 }
187 
188 } // end namespace otb
189 
190 #endif
virtual bool IsAtEnd() const
virtual void GenerateInputRequestedRegion()
OutputImageType::RegionType OutputImageRegionType
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)
itk::NumericTraits< InputPixelType >::RealType InputRealType
NeighborIndexType Size() const
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId)
PixelType GetCenterPixel() const
virtual PixelType GetPixel(NeighborIndexType i, bool &IsInBounds) const
void PrintSelf(std::ostream &os, itk::Indent indent) const
unsigned int ThreadIdType