Orfeo Toolbox  3.16
itkGradientMagnitudeImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkGradientMagnitudeImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2009-04-06 00:19:17 $
7  Version: $Revision: 1.35 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkGradientMagnitudeImageFilter_txx
18 #define __itkGradientMagnitudeImageFilter_txx
20 
23 #include "itkImageRegionIterator.h"
24 #include "itkDerivativeOperator.h"
27 #include "itkOffset.h"
28 #include "itkProgressReporter.h"
29 
30 namespace itk
31 {
32 
33 template <typename TInputImage, typename TOutputImage>
34 void
36 ::PrintSelf(std::ostream& os, Indent indent) const
37 {
38  Superclass::PrintSelf(os,indent);
39  os << indent << "UseImageSpacing = " << m_UseImageSpacing << std::endl;
40 }
41 
42 template <typename TInputImage, typename TOutputImage>
43 void
46 {
47  // call the superclass' implementation of this method
48  Superclass::GenerateInputRequestedRegion();
49 
50  // get pointers to the input and output
51  InputImagePointer inputPtr =
52  const_cast< InputImageType * >( this->GetInput());
53  OutputImagePointer outputPtr = this->GetOutput();
54 
55  if ( !inputPtr || !outputPtr )
56  {
57  return;
58  }
59 
60  // Build an operator so that we can determine the kernel size
62  oper.SetDirection(0);
63  oper.SetOrder(1);
64  oper.CreateDirectional();
65  unsigned long radius = oper.GetRadius()[0];
66 
67  // get a copy of the input requested region (should equal the output
68  // requested region)
69  typename TInputImage::RegionType inputRequestedRegion;
70  inputRequestedRegion = inputPtr->GetRequestedRegion();
71 
72  // pad the input requested region by the operator radius
73  inputRequestedRegion.PadByRadius( radius );
74 
75  // crop the input requested region at the input's largest possible region
76  if ( inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()) )
77  {
78  inputPtr->SetRequestedRegion( inputRequestedRegion );
79  return;
80  }
81  else
82  {
83  // Couldn't crop the region (requested region is outside the largest
84  // possible region). Throw an exception.
85 
86  // store what we tried to request (prior to trying to crop)
87  inputPtr->SetRequestedRegion( inputRequestedRegion );
88 
89  // build an exception
90  InvalidRequestedRegionError e(__FILE__, __LINE__);
91  e.SetLocation(ITK_LOCATION);
92  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
93  e.SetDataObject(inputPtr);
94  throw e;
95  }
96 }
97 
98 
99 template< typename TInputImage, typename TOutputImage >
100 void
102 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
103  int threadId)
104 {
105  unsigned int i;
107 
111 
112 
114 
115  // Allocate output
116  typename OutputImageType::Pointer output = this->GetOutput();
117  typename InputImageType::ConstPointer input = this->GetInput();
118 
119  // Set up operators
121 
122  for (i = 0; i< ImageDimension; i++)
123  {
124  op[i].SetDirection(0);
125  op[i].SetOrder(1);
126  op[i].CreateDirectional();
127 
128  if (m_UseImageSpacing == true)
129  {
130  if ( this->GetInput()->GetSpacing()[i] == 0.0 )
131  {
132  itkExceptionMacro(<< "Image spacing cannot be zero.");
133  }
134  else
135  {
136  op[i].ScaleCoefficients( 1.0 / this->GetInput()->GetSpacing()[i] );
137  }
138  }
139  }
140 
141  // Calculate iterator radius
142  Size<ImageDimension> radius;
143  for (i = 0; i < ImageDimension; ++i)
144  {
145  radius[i] = op[0].GetRadius()[0];
146  }
147 
148  // Find the data-set boundary "faces"
150  FaceListType faceList;
152  faceList = bC(input, outputRegionForThread, radius);
153 
155  FaceListType::iterator fit;
156  fit = faceList.begin();
157 
158  // support progress methods/callbacks
159  ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
160 
161  // Process non-boundary face
162  nit = ConstNeighborhoodIterator<TInputImage>(radius, input, *fit);
163 
164  std::slice x_slice[ImageDimension];
165  const unsigned long center = nit.Size() / 2;
166  for (i = 0; i < ImageDimension; ++i)
167  {
168  x_slice[i] = std::slice( center - nit.GetStride(i) * radius[i],
169  op[i].GetSize()[0], nit.GetStride(i));
170  }
171 
172 
173  // Process each of the boundary faces. These are N-d regions which border
174  // the edge of the buffer.
175  for (fit=faceList.begin(); fit != faceList.end(); ++fit)
176  {
178  input, *fit);
179  it = ImageRegionIterator<OutputImageType>(output, *fit);
180  bit.OverrideBoundaryCondition(&nbc);
181  bit.GoToBegin();
182 
183  while ( ! bit.IsAtEnd() )
184  {
185  RealType a = NumericTraits<RealType>::Zero;
186  for (i = 0; i < ImageDimension; ++i)
187  {
188  const RealType g = SIP(x_slice[i], bit, op[i]);
189  a += g * g;
190  }
191  it.Value() = static_cast<OutputPixelType>(vcl_sqrt(a));
192  ++bit;
193  ++it;
194  progress.CompletedPixel();
195  }
196  }
197 }
198 
199 } // end namespace itk
200 
201 #endif

Generated at Sat Feb 2 2013 23:39:34 for Orfeo Toolbox with doxygen 1.8.1.1