/usr/include/ITK-4.9/itkGaussianDerivativeOperator.hxx is in libinsighttoolkit4-dev 4.9.0-4ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | /*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkGaussianDerivativeOperator_hxx
#define itkGaussianDerivativeOperator_hxx
#include "itkGaussianDerivativeOperator.h"
#include "itkOutputWindow.h"
#include "itkMacro.h"
#include <numeric>
namespace itk
{
/* Constructor. */
template< typename TPixel, unsigned int VDimension, typename TAllocator >
GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::GaussianDerivativeOperator()
{
m_Order = 1;
m_Variance = 1.0;
m_Spacing = 1.0;
m_MaximumError = 0.005;
m_MaximumKernelWidth = 30;
m_NormalizeAcrossScale = true;
}
/* Copy constructor */
template< typename TPixel, unsigned int VDimension, typename TAllocator >
GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::GaussianDerivativeOperator(const Self & other)
: NeighborhoodOperator< TPixel, VDimension, TAllocator >(other)
{
m_NormalizeAcrossScale = other.m_NormalizeAcrossScale;
m_Spacing = other.m_Spacing;
m_Order = other.m_Order;
m_Variance = other.m_Variance;
m_MaximumError = other.m_MaximumError;
m_MaximumKernelWidth = other.m_MaximumKernelWidth;
}
/** Assignment operator */
template< typename TPixel, unsigned int VDimension, typename TAllocator >
GaussianDerivativeOperator< TPixel, VDimension, TAllocator > &
GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::operator=(const Self & other)
{
if(this != &other)
{
Superclass::operator=(other);
m_NormalizeAcrossScale = other.m_NormalizeAcrossScale;
m_Spacing = other.m_Spacing;
m_Order = other.m_Order;
m_Variance = other.m_Variance;
m_MaximumError = other.m_MaximumError;
m_MaximumKernelWidth = other.m_MaximumKernelWidth;
}
return *this;
}
template< typename TPixel, unsigned int VDimension, typename TAllocator >
typename GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::CoefficientVector
GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::GenerateCoefficients()
{
// compute gaussian kernel of 0-order
CoefficientVector coeff = this->GenerateGaussianCoefficients();
if ( m_Order == 0 )
{
return coeff;
}
// Calculate scale-space normalization factor for derivatives
double norm = (m_NormalizeAcrossScale && m_Order ? std::pow(m_Variance, m_Order / 2.0) : 1.0 );
// additional normalization for spacing
norm /= std::pow( m_Spacing, static_cast< int >( m_Order ) );
DerivativeOperatorType derivOp;
derivOp.SetDirection( this->GetDirection() );
derivOp.SetOrder( m_Order );
derivOp.CreateDirectional();
// The input gaussian kernel needs to be padded with a clamped
// boundary condition. If N is the radius of the derivative
// operator, then the output kernel needs to be padded by N-1. For
// these values to be computed the input kernel needs to be padded
// by 2N-1 on both sides.
unsigned int N = ( derivOp.Size() - 1 ) / 2;
// copy the gaussian operator adding clamped boundary condition
CoefficientVector paddedCoeff( coeff.size() + 4*N - 2);
// copy the whole gaussuan operator in coeff to paddedCoef
// starting after the padding
std::copy( coeff.begin(), coeff.end(), paddedCoeff.begin() + 2*N - 1);
// padd paddedCoeff with 2*N-1 number of boundary conditions
std::fill( paddedCoeff.begin(), paddedCoeff.begin() + 2*N, coeff.front() );
std::fill( paddedCoeff.rbegin(), paddedCoeff.rbegin() + 2*N, coeff.back() );
// clear for output kernel/coeffs
coeff = CoefficientVector();
// Now perform convolution between derivative operators and padded gaussian
for ( unsigned int i = N; i < paddedCoeff.size() - N; ++i )
{
double conv = 0.0;
// current index in derivative op
for ( unsigned int j = 0; j < derivOp.Size(); ++j )
{
unsigned int k = i + j - derivOp.Size() / 2;
conv += paddedCoeff[k] * derivOp[derivOp.Size() - 1 - j];
}
// normalize for scale-space and spacing
coeff.push_back(norm * conv);
}
return coeff;
}
template< typename TPixel, unsigned int VDimension, typename TAllocator >
typename GaussianDerivativeOperator< TPixel, VDimension, TAllocator >::CoefficientVector
GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::GenerateGaussianCoefficients() const
{
CoefficientVector coeff;
// Use image spacing to modify variance
const double pixelVariance = m_Variance / ( m_Spacing * m_Spacing );
// Now create coefficients as if they were zero order coeffs
const double et = std::exp(-pixelVariance);
const double cap = 1.0 - m_MaximumError;
double sum = 0.0;
// Create the kernel coefficients as a std::vector
coeff.push_back( et * ModifiedBesselI0(pixelVariance) );
sum += coeff[0];
coeff.push_back( et * ModifiedBesselI1(pixelVariance) );
sum += coeff[1] * 2.0;
for ( int i = 2; sum < cap; i++ )
{
coeff.push_back( et * ModifiedBesselI(i, pixelVariance) );
sum += coeff[i] * 2.0;
if ( coeff[i] < sum*NumericTraits<double>::epsilon() )
{
// if the coeff is less then this value then the value of cap
// will not change, and it's will not contribute to the operator
itkWarningMacro( "Kernel failed to accumulate to approximately one with current remainder "
<< cap-sum << " and current coefficient " << coeff[i] << "." );
break;
}
if ( coeff.size() > m_MaximumKernelWidth )
{
itkWarningMacro("Kernel size has exceeded the specified maximum width of "
<< m_MaximumKernelWidth << " and has been truncated to "
<< static_cast< unsigned long >( coeff.size() ) << " elements. You can raise "
"the maximum width using the SetMaximumKernelWidth method.");
break;
}
}
// re-accumulate from smallest number to largest for maximum precision
sum = std::accumulate( coeff.rbegin(), coeff.rend() - 1, 0.0 );
sum *= 2.0;
sum += coeff[0]; // the first is only needed once
// Normalize the coefficients so they sum one
for ( typename CoefficientVector::iterator it = coeff.begin(); it != coeff.end(); ++it )
{
*it /= sum;
}
// Make symmetric
size_t s = coeff.size() - 1;
coeff.insert(coeff.begin(), s, 0);
std::copy( coeff.rbegin(), coeff.rbegin() + s, coeff.begin() );
return coeff;
}
template< typename TPixel, unsigned int VDimension, typename TAllocator >
double
GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::ModifiedBesselI0(double y)
{
double d, accumulator;
double m;
if ( ( d = std::fabs(y) ) < 3.75 )
{
m = y / 3.75;
m *= m;
accumulator = 1.0 + m * ( 3.5156229 + m * ( 3.0899424 + m * ( 1.2067492
+ m
* ( 0.2659732 + m * ( 0.360768e-1 + m * 0.45813e-2 ) ) ) ) );
}
else
{
m = 3.75 / d;
accumulator = ( std::exp(d) / std::sqrt(d) ) * ( 0.39894228 + m * ( 0.1328592e-1
+ m
* ( 0.225319e-2 + m
* ( -0.157565e-2 + m * ( 0.916281e-2
+
m
* ( -0.2057706e-1
+ m *
( 0.2635537e-1 +
m *
( -0.1647633e-1
+
m
*
0.392377e-2 ) ) ) ) ) ) ) );
}
return accumulator;
}
template< typename TPixel, unsigned int VDimension, typename TAllocator >
double
GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::ModifiedBesselI1(double y)
{
double d, accumulator;
double m;
if ( ( d = std::fabs(y) ) < 3.75 )
{
m = y / 3.75;
m *= m;
accumulator = d * ( 0.5 + m * ( 0.87890594 + m * ( 0.51498869 + m * ( 0.15084934
+ m
* ( 0.2658733e-1 + m
* ( 0.301532e-2 + m * 0.32411e-3 ) ) ) ) ) );
}
else
{
m = 3.75 / d;
accumulator = 0.2282967e-1 + m * ( -0.2895312e-1 + m * ( 0.1787654e-1
- m * 0.420059e-2 ) );
accumulator = 0.39894228 + m * ( -0.3988024e-1 + m * ( -0.362018e-2
+ m * ( 0.163801e-2 + m * ( -0.1031555e-1 + m * accumulator ) ) ) );
accumulator *= ( std::exp(d) / std::sqrt(d) );
}
if ( y < 0.0 ) { return -accumulator; }
else { return accumulator; }
}
template< typename TPixel, unsigned int VDimension, typename TAllocator >
double
GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::ModifiedBesselI(int n, double y)
{
const double DIGITS = 10.0;
int j;
double qim, qi, qip, toy;
double accumulator;
if ( n < 2 )
{
throw ExceptionObject(__FILE__, __LINE__, "Order of modified bessel is > 2.", ITK_LOCATION); //
// placeholder
}
if ( y == 0.0 ) { return 0.0; }
else
{
toy = 2.0 / std::fabs(y);
qip = accumulator = 0.0;
qi = 1.0;
for ( j = 2 * ( n + (int)(DIGITS*std::sqrt((double)n) ) ); j > 0; j-- )
{
qim = qip + j * toy * qi;
qip = qi;
qi = qim;
if ( std::fabs(qi) > 1.0e10 )
{
accumulator *= 1.0e-10;
qi *= 1.0e-10;
qip *= 1.0e-10;
}
if ( j == n ) { accumulator = qip; }
}
accumulator *= ModifiedBesselI0(y) / qi;
if ( y < 0.0 && ( n & 1 ) ) { return -accumulator; }
else { return accumulator; }
}
}
/* Prints some debugging information. */
template< typename TPixel, unsigned int VDimension, typename TAllocator >
void
GaussianDerivativeOperator< TPixel, VDimension, TAllocator >
::PrintSelf(std::ostream & os, Indent i) const
{
os << i << "GaussianDerivativeOperator { this=" << this
<< ", m_NormalizeAcrossScale = " << m_NormalizeAcrossScale
<< ", m_Order = " << m_Order
<< ", m_Spacing = " << m_Spacing
<< ", m_Variance = " << m_Variance
<< ", m_MaximumError = " << m_MaximumError
<< ", m_MaximumKernelWidth = " << m_MaximumKernelWidth
<< "} " << std::endl;
Superclass::PrintSelf( os, i.GetNextIndent() );
}
} // end namespace itk
#endif
|