Orfeo Toolbox  3.16
otbConvolutionImageFilter.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 __otbConvolutionImageFilter_txx
19 #define __otbConvolutionImageFilter_txx
21 
24 #include "itkImageRegionIterator.h"
26 #include "itkOffset.h"
27 #include "itkProgressReporter.h"
29 
30 #include "otbMacro.h"
31 
32 namespace otb
33 {
34 
35 template <class TInputImage, class TOutputImage, class TBoundaryCondition, class TFilterPrecision>
38 {
39  m_Radius.Fill(1);
40  m_Filter.SetSize(3 * 3);
41  m_Filter.Fill(1);
42  m_NormalizeFilter = false;
43 }
44 
45 template <class TInputImage, class TOutputImage, class TBoundaryCondition, class TFilterPrecision>
46 void
48 ::GenerateInputRequestedRegion() throw (itk::InvalidRequestedRegionError)
49  {
50  // call the superclass' implementation of this method
51  Superclass::GenerateInputRequestedRegion();
52 
53  // get pointers to the input and output
54  typename Superclass::InputImagePointer inputPtr =
55  const_cast<TInputImage *>(this->GetInput());
56  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
57 
58  if (!inputPtr || !outputPtr)
59  {
60  return;
61  }
62 
63  // get a copy of the input requested region (should equal the output
64  // requested region)
65  typename TInputImage::RegionType inputRequestedRegion;
66  inputRequestedRegion = inputPtr->GetRequestedRegion();
67 
68  // pad the input requested region by the operator radius
69  inputRequestedRegion.PadByRadius(m_Radius);
70  otbMsgDevMacro(<< "Padding by " << m_Radius);
71  otbMsgDevMacro(<< "Region is now " << inputRequestedRegion.GetIndex() << ", " << inputRequestedRegion.GetSize());
72 
73  // crop the input requested region at the input's largest possible region
74  if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
75  {
76  inputPtr->SetRequestedRegion(inputRequestedRegion);
77  return;
78  }
79  else
80  {
81  // Couldn't crop the region (requested region is outside the largest
82  // possible region). Throw an exception.
83 
84  // store what we tried to request (prior to trying to crop)
85  inputPtr->SetRequestedRegion(inputRequestedRegion);
86 
87  // build an exception
88  itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
89  e.SetLocation(ITK_LOCATION);
90  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
91  e.SetDataObject(inputPtr);
92  throw e;
93  }
94  }
95 
96 template<class TInputImage, class TOutputImage, class TBoundaryCondition, class TFilterPrecision>
97 void
99 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
100  int threadId)
101 {
102  unsigned int i;
103 
104  // Allocate output
105  typename OutputImageType::Pointer output = this->GetOutput();
106  typename InputImageType::ConstPointer input = this->GetInput();
107 
108  // support progress methods/callbacks
109  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
110 
111  InputRealType sum = itk::NumericTraits<InputRealType>::Zero;
112  InputRealType norm = itk::NumericTraits<InputRealType>::Zero;
113 
114  InputImageRegionType inputRegionForThread;
115  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
116 
117  itk::ConstNeighborhoodIterator<InputImageType, BoundaryConditionType> inputIt(m_Radius, input, inputRegionForThread);
118  itk::ImageRegionIterator<OutputImageType> outputIt(output, outputRegionForThread);
119 
120  inputIt.GoToBegin();
121  unsigned int neighborhoodSize = inputIt.Size();
122 
123  // Compute the norm of the filter
124  if (m_NormalizeFilter)
125  {
126  norm = itk::NumericTraits<InputRealType>::Zero;
127  for (i = 0; i < neighborhoodSize; ++i)
128  {
129  norm += static_cast<InputRealType>(vcl_abs(m_Filter(i)));
130  }
131  }
132 
133  while (!inputIt.IsAtEnd())
134  {
135  sum = itk::NumericTraits<InputRealType>::Zero;
136 
137  for (i = 0; i < neighborhoodSize; ++i)
138  {
139  sum += static_cast<InputRealType>(inputIt.GetPixel(i) * m_Filter(i));
140  }
141 
142  // get the mean value
143  if (m_NormalizeFilter)
144  {
145  outputIt.Set(static_cast<OutputPixelType>(sum / double(norm)));
146  }
147  else
148  {
149  outputIt.Set(static_cast<OutputPixelType>(sum));
150  }
151 
152  ++inputIt;
153  ++outputIt;
154  progress.CompletedPixel();
155  }
156 }
157 
161 template <class TInputImage, class TOutput, class TBoundaryCondition, class TFilterPrecision>
162 void
164 ::PrintSelf(std::ostream& os, itk::Indent indent) const
165 {
166  Superclass::PrintSelf(os, indent);
167  os << indent << "Radius: " << m_Radius << std::endl;
168 
169 }
170 
171 } // end namespace otb
172 
173 #endif

Generated at Sun May 19 2013 00:20:40 for Orfeo Toolbox with doxygen 1.8.3.1