This file is indexed.

/usr/include/InsightToolkit/Review/itkDiscreteGradientMagnitudeGaussianImageFunction.txx is in libinsighttoolkit3-dev 3.20.1-1.

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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkDiscreteGradientMagnitudeGaussianImageFunction.txx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#ifndef __itkDiscreteGradientMagnitudeGaussianImageFunction_txx
#define __itkDiscreteGradientMagnitudeGaussianImageFunction_txx

#include "itkDiscreteGradientMagnitudeGaussianImageFunction.h"
#include "itkNeighborhoodOperatorImageFilter.h"

namespace itk
{

/** Set the Input Image */
template <class TInputImage, class TOutput>
DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>
::DiscreteGradientMagnitudeGaussianImageFunction() :
  m_MaximumError( 0.005 ),
  m_MaximumKernelWidth( 30 ),
  m_NormalizeAcrossScale( true ),
  m_UseImageSpacing( true ),
  m_InterpolationMode( NearestNeighbourInterpolation )
{
  m_Variance.Fill( 1.0 );
  m_OperatorImageFunction = OperatorImageFunctionType::New();
}


/** Print self method */
template <class TInputImage, class TOutput>
void
DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>
::PrintSelf(std::ostream& os, Indent indent) const
{
  this->Superclass::PrintSelf(os,indent);
  os << indent << "UseImageSpacing: " << m_UseImageSpacing << std::endl;
  os << indent << "NormalizeAcrossScale: " << m_NormalizeAcrossScale << std::endl;
  os << indent << "Variance: " << m_Variance << std::endl;
  os << indent << "MaximumError: " << m_MaximumError << std::endl;
  os << indent << "MaximumKernelWidth: " << m_MaximumKernelWidth << std::endl;
  os << indent << "InterpolationMode: " << m_InterpolationMode << std::endl;
  os << indent << "OperatorArray: " << m_OperatorArray << std::endl;
  os << indent << "KernelArray: " << m_KernelArray << std::endl;
  os << indent << "OperatorImageFunction: " << m_OperatorImageFunction << std::endl;
}


/** Set the input image */
template <class TInputImage, class TOutput>
void
DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>
::SetInputImage( const InputImageType * ptr )
{
  Superclass::SetInputImage(ptr);
  m_OperatorImageFunction->SetInputImage(ptr);
}


/** Recompute the gaussian kernel used to evaluate indexes
 *  This should use a fastest Derivative Gaussian operator */
template <class TInputImage, class TOutput>
void
DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>
::RecomputeGaussianKernel()
{
  /** Create 2*N operators (N=ImageDimension) where the
   * first N are zero-order and the second N are first-order */

  unsigned int idx;
  unsigned int maxRadius = 0;

  for(unsigned int direction=0; direction <
    itkGetStaticConstMacro(ImageDimension2); direction++ )
    {
    for( unsigned int order=0; order <= 1; ++order )
      {
      idx = itkGetStaticConstMacro(ImageDimension2)*order + direction;
      m_OperatorArray[idx].SetDirection( direction );
      m_OperatorArray[idx].SetMaximumKernelWidth( m_MaximumKernelWidth );
      m_OperatorArray[idx].SetMaximumError( m_MaximumError );

      if( ( m_UseImageSpacing == true ) && ( this->GetInputImage() ) )
        {
        if ( this->GetInputImage()->GetSpacing()[direction] == 0.0)
          {
          itkExceptionMacro(<< "Pixel spacing cannot be zero");
          }
        else
          {
        m_OperatorArray[idx].SetSpacing(this->GetInputImage()->GetSpacing()[direction]);
          }
        }

      // GaussianDerivativeOperator modifies the variance when setting image spacing
      m_OperatorArray[idx].SetVariance( m_Variance[direction] );
      m_OperatorArray[idx].SetOrder(order);
      m_OperatorArray[idx].SetNormalizeAcrossScale( m_NormalizeAcrossScale );
      m_OperatorArray[idx].CreateDirectional();

      // Check for maximum radius
      for( unsigned int i=0; i<itkGetStaticConstMacro(ImageDimension2); ++i )
        {
        if( m_OperatorArray[idx].GetRadius()[i] > maxRadius )
          maxRadius = m_OperatorArray[idx].GetRadius()[i];
        }
      }
    }

  // Now precompute the N-dimensional kernel. This fastest as we don't have to perform
  // N convolutions for each point we calculate but only one.

  typedef itk::Image<TOutput,itkGetStaticConstMacro(ImageDimension2)>  KernelImageType;
  typename KernelImageType::Pointer kernelImage = KernelImageType::New();

  typedef typename KernelImageType::RegionType RegionType;
  RegionType region;

  typename RegionType::SizeType size;
  size.Fill( 4 * maxRadius + 1 );
  region.SetSize( size );

  kernelImage->SetRegions( region );
  kernelImage->Allocate();
  kernelImage->FillBuffer( itk::NumericTraits<TOutput>::Zero );

  // Initially the kernel image will be an impulse at the center
  typename KernelImageType::IndexType centerIndex;
  centerIndex.Fill( 2 * maxRadius ); // include also boundaries

  // Create an image region to be used later that does not include boundaries
  RegionType kernelRegion;
  size.Fill( 2 * maxRadius + 1 );
  typename RegionType::IndexType origin;
  origin.Fill( maxRadius );
  kernelRegion.SetSize( size );
  kernelRegion.SetIndex( origin );

  // Now create an image filter to perform sucessive convolutions
  typedef itk::NeighborhoodOperatorImageFilter<KernelImageType,KernelImageType>
    NeighborhoodFilterType;
  typename NeighborhoodFilterType::Pointer convolutionFilter = NeighborhoodFilterType::New();

  unsigned int opidx; // current operator index in m_OperatorArray

  for( unsigned int i=0; i<itkGetStaticConstMacro(ImageDimension2); ++i )
    {
      // Reset kernel image
    kernelImage->FillBuffer( itk::NumericTraits<TOutput>::Zero );
    kernelImage->SetPixel( centerIndex, itk::NumericTraits<TOutput>::One );

    for( unsigned int direction = 0; direction<itkGetStaticConstMacro(ImageDimension2); ++direction )
      {
      opidx = ( direction == i ? itkGetStaticConstMacro(ImageDimension2) + direction : direction );
      convolutionFilter->SetInput( kernelImage );
      convolutionFilter->SetOperator( m_OperatorArray[opidx] );
      convolutionFilter->Update();
      kernelImage = convolutionFilter->GetOutput();
      kernelImage->DisconnectPipeline();
      }

    // Set the size of the current kernel
    m_KernelArray[i].SetRadius( maxRadius );

    // Copy kernel image to neighborhood. Do not copy boundaries.
    ImageRegionConstIterator<KernelImageType> it( kernelImage, kernelRegion );
    it.GoToBegin();
    idx = 0;

    while( !it.IsAtEnd() )
      {
      m_KernelArray[i][idx] = it.Get();
      ++idx;
      ++it;
      }
    }
}


/** Evaluate the function at the specifed index */
template <class TInputImage, class TOutput>
typename DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>::OutputType
DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>
::EvaluateAtIndex(const IndexType& index) const
{
  OutputType gradientMagnitude = itk::NumericTraits<OutputType>::Zero;
  OutputType temp;

  for( unsigned int i=0; i<m_KernelArray.Size(); ++i )
    {
    m_OperatorImageFunction->SetOperator( m_KernelArray[i] );
    temp = m_OperatorImageFunction->EvaluateAtIndex( index );
    if( m_UseImageSpacing )
      {
      gradientMagnitude += vnl_math_sqr( temp / this->GetInputImage()->GetSpacing()[i] );
      }
    else
      {
      gradientMagnitude += vnl_math_sqr( temp );
      } 
    }

  gradientMagnitude = vcl_sqrt( gradientMagnitude );
  return gradientMagnitude;
}


/** Evaluate the function at the specifed point */
template <class TInputImage, class TOutput>
typename DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>::OutputType
DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>
::Evaluate(const PointType& point) const
{
  if( m_InterpolationMode == NearestNeighbourInterpolation )
    {
    IndexType index;
    this->ConvertPointToNearestIndex( point , index );
    return this->EvaluateAtIndex ( index );
    }
  else
    {
    ContinuousIndexType cindex;
#if ( ITK_VERSION_MAJOR < 3 ) || ( ITK_VERSION_MAJOR == 3 && ITK_VERSION_MINOR < 6 )
    this->ConvertPointToContinousIndex( point, cindex );
#else
    this->ConvertPointToContinuousIndex( point, cindex );
#endif
    return this->EvaluateAtContinuousIndex( cindex );
    }
}


/** Evaluate the function at specified ContinousIndex position.*/
template <class TInputImage, class TOutput>
typename DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>::OutputType
DiscreteGradientMagnitudeGaussianImageFunction<TInputImage,TOutput>
::EvaluateAtContinuousIndex(const ContinuousIndexType & cindex ) const
{
  if( m_InterpolationMode == NearestNeighbourInterpolation )
    {
    IndexType index;
    this->ConvertContinuousIndexToNearestIndex( cindex, index  );
    return this->EvaluateAtIndex( index );
    }
  else
    {
    unsigned int dim;  // index over dimension
    unsigned long neighbors = 1 << ImageDimension2;

    // Compute base index = closet index below point
    // Compute distance from point to base index
    IndexType baseIndex;
    double distance[ImageDimension2];

    for( dim = 0; dim < ImageDimension2; dim++ )
      {
      baseIndex[dim] = Math::Floor<IndexValueType>( cindex[dim] );
      distance[dim] = cindex[dim] - static_cast< double >( baseIndex[dim] );
      }

    // Interpolated value is the weighted sum of each of the surrounding
    // neighbors. The weight for each neighbor is the fraction overlap
    // of the neighbor pixel with respect to a pixel centered on point.
    TOutput value = NumericTraits<TOutput>::Zero;
    TOutput totalOverlap = NumericTraits<TOutput>::Zero;

    for( unsigned int counter = 0; counter < neighbors; counter++ )
      {
      double overlap = 1.0;          // fraction overlap
      unsigned int upper = counter;  // each bit indicates upper/lower neighbour
      IndexType neighIndex;

      // get neighbor index and overlap fraction
      for( dim = 0; dim < ImageDimension2; dim++ )
        {
        if ( upper & 1 )
          {
          neighIndex[dim] = baseIndex[dim] + 1;
          overlap *= distance[dim];
          }
        else
          {
          neighIndex[dim] = baseIndex[dim];
          overlap *= 1.0 - distance[dim];
          }
        upper >>= 1;
        }

      // get neighbor value only if overlap is not zero
      if( overlap )
        {
        value += overlap * static_cast<TOutput>( this->EvaluateAtIndex( neighIndex ) );
        totalOverlap += overlap;
        }

      if( totalOverlap == 1.0 )
        {
        // finished
        break;
        }
      }
    return ( static_cast<OutputType>( value ) );
    }
}

} // end namespace itk

#endif