| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkSobelOperator.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:48 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 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 |
DEF |
#ifndef _itkSobelOperator_txx |
| 18 |
DEF |
#define _itkSobelOperator_txx |
| 19 |
|
|
| 20 |
|
#include "itkSobelOperator.h" |
| 21 |
|
#include "itkObject.h" |
| 22 |
|
|
| 23 |
|
namespace itk |
| 24 |
|
{ |
| 25 |
|
|
| 26 |
|
template <class TPixel, unsigned int VDimension, class TAllocator> |
| 27 |
|
void |
| 28 |
|
SobelOperator <TPixel, VDimension, TAllocator> |
| 29 |
|
::Fill(const CoefficientVector &coeff) |
| 30 |
|
{ |
| 31 |
|
this->InitializeToZero(); |
| 32 |
|
|
| 33 |
|
// Note that this code is only good for 2d and 3d operators. Places the |
| 34 |
|
// coefficients in the exact center of the neighborhood |
| 35 |
|
unsigned int i; |
| 36 |
|
int x,y,z, pos; |
| 37 |
|
unsigned int center = this->GetCenterNeighborhoodIndex(); |
| 38 |
|
|
| 39 |
|
if (VDimension == 3) |
| 40 |
|
{ |
| 41 |
IND |
******i = 0; |
| 42 |
IND |
******for (z = -1; z <= 1; z++) |
| 43 |
|
{ |
| 44 |
|
for (y = -1; y <= 1; y++ ) |
| 45 |
|
{ |
| 46 |
|
for (x = -1; x <= 1; x++) |
| 47 |
|
{ |
| 48 |
LEN |
pos = center + z * this->GetStride(2) + y * this->GetStride(1) + |
| 49 |
IND |
********************x * this->GetStride(0); |
| 50 |
IND |
******************this->operator[](pos) = static_cast<TPixel>(coeff[i]); |
| 51 |
IND |
******************i++; |
| 52 |
IND |
****************} |
| 53 |
IND |
************} |
| 54 |
IND |
********} |
| 55 |
|
} |
| 56 |
|
else if (VDimension == 2) |
| 57 |
|
{ |
| 58 |
IND |
******i = 0; |
| 59 |
IND |
******for (y = -1; y <= 1; y++ ) |
| 60 |
|
{ |
| 61 |
|
for (x = -1; x <= 1; x++) |
| 62 |
|
{ |
| 63 |
|
pos = center + y * this->GetStride(1) + x * this->GetStride(0); |
| 64 |
IND |
**************this->operator[](pos) = static_cast<TPixel>(coeff[i]); |
| 65 |
IND |
**************i++; |
| 66 |
IND |
************} |
| 67 |
IND |
********} |
| 68 |
|
} |
| 69 |
|
else |
| 70 |
|
{ |
| 71 |
LEN,IND |
******itkExceptionMacro( << "The ND version of the Sobel operator is not yet implemented. Currently only the 2D and 3D versions are available." ); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
template <class TPixel, unsigned int VDimension, class TAllocator> |
| 77 |
|
typename SobelOperator<TPixel, VDimension, TAllocator> |
| 78 |
|
::CoefficientVector |
| 79 |
|
SobelOperator<TPixel, VDimension, TAllocator> |
| 80 |
|
::GenerateCoefficients() |
| 81 |
|
{ |
| 82 |
|
std::vector<double> coeff; |
| 83 |
|
if (VDimension == 2 && this->GetDirection() == 0) |
| 84 |
|
{ |
| 85 |
IND |
******coeff.push_back(-1.0); coeff.push_back(0.0); coeff.push_back(1.0); |
| 86 |
IND |
******coeff.push_back(-2.0); coeff.push_back(0.0); coeff.push_back(2); |
| 87 |
IND |
******coeff.push_back(-1.0); coeff.push_back(0.0); coeff.push_back(1.0); |
| 88 |
|
} |
| 89 |
|
else if (VDimension == 2 && this->GetDirection() == 1) |
| 90 |
|
{ |
| 91 |
IND |
******coeff.push_back(-1.0); coeff.push_back(-2); coeff.push_back(-1.0); |
| 92 |
IND |
******coeff.push_back(0.0); coeff.push_back(0.0); coeff.push_back(0.0); |
| 93 |
IND |
******coeff.push_back(1.0); coeff.push_back(2); coeff.push_back(1.0); |
| 94 |
|
} |
| 95 |
|
else if (VDimension == 3 && this->GetDirection() == 0) |
| 96 |
|
{ |
| 97 |
IND |
******coeff.push_back(-1.0); coeff.push_back(0.0); coeff.push_back(1.0); |
| 98 |
IND |
******coeff.push_back(-3.0); coeff.push_back(0.0); coeff.push_back(3.0); |
| 99 |
IND |
******coeff.push_back(-1.0); coeff.push_back(0.0); coeff.push_back(1.0); |
| 100 |
|
|
| 101 |
IND |
******coeff.push_back(-3.0); coeff.push_back(0.0); coeff.push_back(3.0); |
| 102 |
IND |
******coeff.push_back(-6.0); coeff.push_back(0.0); coeff.push_back(6.0); |
| 103 |
IND |
******coeff.push_back(-3.0); coeff.push_back(0.0); coeff.push_back(3.0); |
| 104 |
|
|
| 105 |
IND |
******coeff.push_back(-1.0); coeff.push_back(0.0); coeff.push_back(1.0); |
| 106 |
IND |
******coeff.push_back(-3.0); coeff.push_back(0.0); coeff.push_back(3.0); |
| 107 |
IND |
******coeff.push_back(-1.0); coeff.push_back(0.0); coeff.push_back(1.0); |
| 108 |
|
} |
| 109 |
|
else if (VDimension == 3 && this->GetDirection() == 1) |
| 110 |
|
{ |
| 111 |
IND |
******coeff.push_back(-1.0); coeff.push_back(-3.0); coeff.push_back(-1.0); |
| 112 |
IND |
******coeff.push_back(0.0); coeff.push_back(0.0); coeff.push_back(0.0); |
| 113 |
IND |
******coeff.push_back(1.0); coeff.push_back(3.0); coeff.push_back(1.0); |
| 114 |
|
|
| 115 |
IND |
******coeff.push_back(-3.0); coeff.push_back(-6.0); coeff.push_back(-3.0); |
| 116 |
IND |
******coeff.push_back(0.0); coeff.push_back(0.0); coeff.push_back(0.0); |
| 117 |
IND |
******coeff.push_back(3.0); coeff.push_back(6.0); coeff.push_back(3.0); |
| 118 |
|
|
| 119 |
IND |
******coeff.push_back(-1.0); coeff.push_back(-3.0); coeff.push_back(-1.0); |
| 120 |
IND |
******coeff.push_back(0.0); coeff.push_back(0.0); coeff.push_back(0.0); |
| 121 |
IND |
******coeff.push_back(1.0); coeff.push_back(3.0); coeff.push_back(1.0); |
| 122 |
|
} |
| 123 |
|
else if (VDimension == 3 && this->GetDirection() == 2) |
| 124 |
|
{ |
| 125 |
IND |
******coeff.push_back(-1.0); coeff.push_back(-3.0); coeff.push_back(-1.0); |
| 126 |
IND |
******coeff.push_back(-3.0); coeff.push_back(-6.0); coeff.push_back(-3.0); |
| 127 |
IND |
******coeff.push_back(-1.0); coeff.push_back(-3.0); coeff.push_back(-1.0); |
| 128 |
|
|
| 129 |
IND |
******coeff.push_back(0.0); coeff.push_back(0.0); coeff.push_back(0.0); |
| 130 |
IND |
******coeff.push_back(0.0); coeff.push_back(0.0); coeff.push_back(0.0); |
| 131 |
IND |
******coeff.push_back(0.0); coeff.push_back(0.0); coeff.push_back(0.0); |
| 132 |
|
|
| 133 |
IND |
******coeff.push_back(1.0); coeff.push_back(3.0); coeff.push_back(1.0); |
| 134 |
IND |
******coeff.push_back(3.0); coeff.push_back(6.0); coeff.push_back(3.0); |
| 135 |
IND |
******coeff.push_back(1.0); coeff.push_back(3.0); coeff.push_back(1.0); |
| 136 |
|
} |
| 137 |
|
else |
| 138 |
|
{ |
| 139 |
LEN,IND |
******itkExceptionMacro( << "The ND version of the Sobel operator has not been implemented. Currently only 2D and 3D versions are available." ); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
return coeff; |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
} // namespace itk |
| 146 |
|
|
| 147 |
|
#endif |
| 148 |
|
|