Orfeo Toolbox  4.0
otbFrostImageFilter.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 __otbFrostImageFilter_txx
19 #define __otbFrostImageFilter_txx
20 
21 #include "otbFrostImageFilter.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 }
44 
45 template <class TInputImage, class TOutputImage>
47  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;
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 sum;
122  InputRealType sum2;
123 
124  double Mean, Variance;
125  double Alpha;
126  double NormFilter;
127  double FrostFilter;
128  double CoefFilter;
129  double dPixel;
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  unsigned int neighborhoodSize = bit.Size();
138  bit.OverrideBoundaryCondition(&nbc);
139  bit.GoToBegin();
140 
141  while (!bit.IsAtEnd())
142  {
145  for (i = 0; i < neighborhoodSize; ++i)
146  {
147  dPixel = static_cast<double>(bit.GetPixel(i));
148  sum += dPixel;
149  sum2 += dPixel * dPixel;
150  }
151  Mean = sum / double(neighborhoodSize);
152  Variance = sum2 / double(neighborhoodSize) - Mean * Mean;
153 
154  if (Mean == 0)
155  {
156  Alpha = 0;
157  }
158  else
159  {
160  Alpha = m_Deramp * Variance / (Mean * Mean);
161  }
162 
163  NormFilter = 0.0;
164  FrostFilter = 0.0;
165 
166  const int rad_x = m_Radius[0];
167  const int rad_y = m_Radius[1];
168 
169  for (int x = -rad_x; x <= rad_x; ++x)
170  {
171  for (int y = -rad_y; y <= rad_y; ++y)
172  {
173  double Dist = vcl_sqrt(static_cast<double>(x * x + y * y));
174  off[0] = x;
175  off[1] = y;
176 
177  dPixel = static_cast<double>(bit.GetPixel(off));
178 
179  CoefFilter = Alpha * vcl_exp(-Alpha * Dist);
180  NormFilter += CoefFilter;
181  FrostFilter += (CoefFilter * dPixel);
182  }
183  }
184 
185  if (NormFilter == 0.) dPixel = 0.;
186  else dPixel = FrostFilter / NormFilter;
187 
188  it.Set(static_cast<OutputPixelType>(dPixel));
189 
190  ++bit;
191  ++it;
192  progress.CompletedPixel();
193 
194  }
195  }
196 }
197 
201 template <class TInputImage, class TOutput>
202 void
204 {
205  Superclass::PrintSelf(os, indent);
206  os << indent << "Radius: " << m_Radius << std::endl;
207 }
208 
209 } // end namespace otb
210 
211 #endif

Generated at Sat Mar 8 2014 15:56:13 for Orfeo Toolbox with doxygen 1.8.3.1