Orfeo Toolbox  3.16
itkSobelEdgeDetectionImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkSobelEdgeDetectionImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2008-10-17 20:50:03 $
7  Version: $Revision: 1.16 $
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 __itkSobelEdgeDetectionImageFilter_txx
18 #define __itkSobelEdgeDetectionImageFilter_txx
20 
22 #include "itkSobelOperator.h"
24 #include "itkNaryAddImageFilter.h"
25 #include "itkMultiplyImageFilter.h"
26 #include "itkSqrtImageFilter.h"
27 
28 
29 namespace itk
30 {
31 
32 template <class TInputImage, class TOutputImage>
33 void
36 {
37  // call the superclass' implementation of this method. this should
38  // copy the output requested region to the input requested region
39  Superclass::GenerateInputRequestedRegion();
40 
41  // get pointers to the input and output
42  InputImagePointer inputPtr =
43  const_cast< TInputImage * >( this->GetInput() );
44 
45  if ( !inputPtr )
46  {
47  return;
48  }
49 
50  // Build an operator so that we can determine the kernel size
52  oper.CreateDirectional();
53 
54  // get a copy of the input requested region (should equal the output
55  // requested region)
56  typename TInputImage::RegionType inputRequestedRegion;
57  inputRequestedRegion = inputPtr->GetRequestedRegion();
58 
59  // pad the input requested region by the operator radius
60  inputRequestedRegion.PadByRadius( oper.GetRadius() );
61 
62  // crop the input requested region at the input's largest possible region
63  if ( inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()) )
64  {
65  inputPtr->SetRequestedRegion( inputRequestedRegion );
66  return;
67  }
68  else
69  {
70  // Couldn't crop the region (requested region is outside the largest
71  // possible region). Throw an exception.
72 
73  // store what we tried to request (prior to trying to crop)
74  inputPtr->SetRequestedRegion( inputRequestedRegion );
75 
76  // build an exception
77  InvalidRequestedRegionError e(__FILE__, __LINE__);
78  e.SetLocation(ITK_LOCATION);
79  e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
80  e.SetDataObject(inputPtr);
81  throw e;
82  }
83 }
84 
85 
86 template< class TInputImage, class TOutputImage >
87 void
90 {
91  // Test whether the output pixel type (or its components) are not of type
92  // float or double:
93  if( NumericTraits< OutputPixelType >::is_integer )
94  {
95  itkWarningMacro("Output pixel type MUST be float or double to prevent computational errors");
96  }
97 
98  // Define the filter types used.
100  OutputImageType> OpFilter;
102  OutputImageType,
103  OutputImageType> MultFilter;
106 
107  unsigned int i;
108 
109  typename TOutputImage::Pointer output = this->GetOutput();
110  output->SetBufferedRegion(output->GetRequestedRegion());
111  output->Allocate();
112 
113  // Create the sobel operator
116 
117  // Setup mini-pipelines along each axis.
118  typename OpFilter::Pointer opFilter[ImageDimension];
119  typename MultFilter::Pointer multFilter[ImageDimension];
120  typename AddFilter::Pointer addFilter = AddFilter::New();
121  typename SqrtFilter::Pointer sqrtFilter = SqrtFilter::New();
122  for(i=0; i < ImageDimension; ++i)
123  {
124  // Create the filters for this axis.
125  opFilter[i] = OpFilter::New();
126  multFilter[i] = MultFilter::New();
127 
128  // Set boundary condition and operator for this axis.
129  opers[i].SetDirection(i);
130  opers[i].CreateDirectional();
131  opFilter[i]->OverrideBoundaryCondition(&nbc);
132  opFilter[i]->SetOperator(opers[i]);
133 
134  // Setup the mini-pipeline for this axis.
135  opFilter[i]->SetInput(this->GetInput());
136  multFilter[i]->SetInput1(opFilter[i]->GetOutput());
137  multFilter[i]->SetInput2(opFilter[i]->GetOutput());
138 
139  // All axes' mini-pipelines come together in addFilter.
140  addFilter->SetInput(i, multFilter[i]->GetOutput());
141  }
142 
143  // calculate the gradient magnitude
144  sqrtFilter->SetInput(addFilter->GetOutput());
145 
146  // setup the mini-pipeline to calculate the correct regions and
147  // write to the appropriate bulk data block
148  sqrtFilter->GraftOutput( this->GetOutput() );
149 
150  // execute the mini-pipeline
151  sqrtFilter->Update();
152 
153  // graft the mini-pipeline output back onto this filter's output.
154  // this is needed to get the appropriate regions passed back.
155  this->GraftOutput(sqrtFilter->GetOutput());
156 }
157 
158 } // end namespace itk
159 
160 #endif

Generated at Sun Feb 3 2013 00:07:45 for Orfeo Toolbox with doxygen 1.8.1.1