This file is indexed.

/usr/include/ITK-4.5/itkMIRegistrationFunction.hxx is in libinsighttoolkit4-dev 4.5.0-3.

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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*=========================================================================
 *
 *  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 __itkMIRegistrationFunction_hxx
#define __itkMIRegistrationFunction_hxx

#include "itkMIRegistrationFunction.h"
#include "itkImageRandomIteratorWithIndex.h"
#include "itkMacro.h"
#include "vnl/vnl_math.h"
#include "itkNeighborhoodIterator.h"

#include "vnl/vnl_matrix.h"

namespace itk
{
/**
 * Default constructor
 */
template< typename TFixedImage, typename TMovingImage, typename TDisplacementField >
MIRegistrationFunction< TFixedImage, TMovingImage, TDisplacementField >
::MIRegistrationFunction()
{
  RadiusType   r;
  unsigned int j;

  m_NumberOfSamples = 1;
  m_NumberOfBins = 4;

  for ( j = 0; j < ImageDimension; j++ )
    {
    r[j] = 2;
    m_NumberOfSamples *= ( r[j] * 2 + 1 );
    }
  this->SetRadius(r);

  m_MetricTotal = 0.0;

  m_TimeStep = 1.0;
  m_Minnorm = 1.0;
  m_DenominatorThreshold = 1e-9;
  m_IntensityDifferenceThreshold = 0.001;
  this->SetMovingImage(NULL);
  this->SetFixedImage(NULL);
  m_FixedImageGradientCalculator = GradientCalculatorType::New();

  m_DoInverse = true;
  m_DoInverse = false;

  if ( m_DoInverse )
    {
    m_MovingImageGradientCalculator = GradientCalculatorType::New();
    }
  typename DefaultInterpolatorType::Pointer interp =
    DefaultInterpolatorType::New();

  m_MovingImageInterpolator = static_cast< InterpolatorType * >(
    interp.GetPointer() );
}

/*
 * Standard "PrintSelf" method.
 */
template< typename TFixedImage, typename TMovingImage, typename TDisplacementField >
void
MIRegistrationFunction< TFixedImage, TMovingImage, TDisplacementField >
::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);
/*
  os << indent << "MovingImageIterpolator: ";
  os << m_MovingImageInterpolator.GetPointer() << std::endl;
  os << indent << "FixedImageGradientCalculator: ";
  os << m_FixedImageGradientCalculator.GetPointer() << std::endl;
  os << indent << "DenominatorThreshold: ";
  os << m_DenominatorThreshold << std::endl;
  os << indent << "IntensityDifferenceThreshold: ";
  os << m_IntensityDifferenceThreshold << std::endl;
*/
}

/*
 * Set the function state values before each iteration
 */
template< typename TFixedImage, typename TMovingImage, typename TDisplacementField >
void
MIRegistrationFunction< TFixedImage, TMovingImage, TDisplacementField >
::InitializeIteration()
{
  if ( !this->m_MovingImage || !this->m_FixedImage || !m_MovingImageInterpolator )
    {
    itkExceptionMacro(<< "MovingImage, FixedImage and/or Interpolator not set");
    }

  // setup gradient calculator
  m_FixedImageGradientCalculator->SetInputImage(this->m_FixedImage);

  if ( m_DoInverse )
    {
    // setup gradient calculator
    m_MovingImageGradientCalculator->SetInputImage(this->m_MovingImage);
    }
  // setup moving image interpolator
  m_MovingImageInterpolator->SetInputImage(this->m_MovingImage);

  m_MetricTotal = 0.0;
}

/**
 * Compute update at a non boundary neighbourhood
 */
template< typename TFixedImage, typename TMovingImage, typename TDisplacementField >
typename MIRegistrationFunction< TFixedImage, TMovingImage, TDisplacementField >
::PixelType
MIRegistrationFunction< TFixedImage, TMovingImage, TDisplacementField >
::ComputeUpdate( const NeighborhoodType & it, void *itkNotUsed(globalData),
                 const FloatOffsetType & itkNotUsed(offset) )
{
  // we compute the derivative of MI w.r.t. the infinitesimal
  // displacement, following viola and wells.

  // 1)  collect samples from  M (Moving) and F (Fixed)
  // 2)  compute minimum and maximum values of M and F
  // 3)  discretized M and F into N bins
  // 4)  estimate joint probability P(M,F) and P(F)
  // 5)  derivatives is given as :
  //
  //  $$ \nabla MI = \frac{1}{N} \sum_i \sum_j (F_i-F_j)
  //    ( W(F_i,F_j) \frac{1}{\sigma_v} -
  //                W((F_i,M_i),(F_j,M_j)) \frac{1}{\sigma_vv} ) \nabla F
  //
  // NOTE : must estimate sigma for each pdf

  typedef vnl_matrix< double >               matrixType;
  typedef std::vector< double >              sampleContainerType;
  typedef std::vector< CovariantVectorType > gradContainerType;
  typedef std::vector< double >              gradMagContainerType;
  typedef std::vector< unsigned int >        inImageIndexContainerType;

  PixelType    update;
  PixelType    derivative;
  unsigned int j;

  const IndexType oindex = it.GetIndex();

  unsigned int indct;

  for ( indct = 0; indct < ImageDimension; indct++ )
    {
    update[indct] = 0.0;
    derivative[indct] = 0.0;
    }

  float thresh2 = 1.0 / 255.; //  FIX ME : FOR PET LUNG ONLY !!
  float thresh1 = 1.0 / 255.;
  if ( this->m_MovingImage->GetPixel(oindex) <= thresh1
       && this->m_FixedImage->GetPixel(oindex) <= thresh2 ) { return update; }

  typename FixedImageType::SizeType hradius = this->GetRadius();

  FixedImageType *img = const_cast< FixedImageType * >( this->m_FixedImage.GetPointer() );
  typename FixedImageType::SizeType imagesize = img->GetLargestPossibleRegion().GetSize();

  bool inimage;

// now collect the samples
  sampleContainerType       fixedSamplesA;
  sampleContainerType       movingSamplesA;
  sampleContainerType       fixedSamplesB;
  sampleContainerType       movingSamplesB;
  inImageIndexContainerType inImageIndicesA;
  gradContainerType         fixedGradientsA;
  gradMagContainerType      fixedGradMagsA;
  inImageIndexContainerType inImageIndicesB;
  gradContainerType         fixedGradientsB;
  gradMagContainerType      fixedGradMagsB;

  unsigned int samplestep = 2; //m_Radius[0];

  double minf = 1.e9, minm = 1.e9, maxf = 0.0, maxm = 0.0;
  double movingMean = 0.0;
  double fixedMean = 0.0;
  double fixedValue = 0, movingValue = 0;

  unsigned int sampct = 0;

  ConstNeighborhoodIterator< DisplacementFieldType >
  asamIt( hradius,
          this->GetDisplacementField(),
          this->GetDisplacementField()->GetRequestedRegion() );
  asamIt.SetLocation(oindex);
  unsigned int hoodlen = asamIt.Size();

// first get the density-related sample
  for ( indct = 0; indct < hoodlen; indct = indct + samplestep )
    {
    IndexType index = asamIt.GetIndex(indct);
    inimage = true;
    for ( unsigned int dd = 0; dd < ImageDimension; dd++ )
      {
      if ( index[dd] < 0 || index[dd] >
           static_cast< typename IndexType::IndexValueType >( imagesize[dd] - 1 ) ) { inimage = false; }
      }
    if ( inimage )
      {
      fixedValue = 0.;
      movingValue = 0.0;
      CovariantVectorType fixedGradient;

      // Get fixed image related information
      fixedValue = (double)this->m_FixedImage->GetPixel(index);

      fixedGradient = m_FixedImageGradientCalculator->EvaluateAtIndex(index);

      // Get moving image related information
      typedef typename DisplacementFieldType::PixelType DeformationPixelType;
      const DeformationPixelType itvec = this->GetDisplacementField()->GetPixel(index);

      PointType mappedPoint;
      this->GetFixedImage()->TransformIndexToPhysicalPoint(index, mappedPoint);
      for ( j = 0; j < ImageDimension; j++ )
        {
        mappedPoint[j] += itvec[j];
        }
      if ( m_MovingImageInterpolator->IsInsideBuffer(mappedPoint) )
        {
        movingValue = m_MovingImageInterpolator->Evaluate(mappedPoint);
        }
      else
        {
        movingValue = 0.0;
        }

      if ( fixedValue > maxf ) { maxf = fixedValue; }
      else if ( fixedValue < minf )
        {
        minf = fixedValue;
        }
      if ( movingValue > maxm ) { maxm = movingValue; }
      else if ( movingValue < minm )
        {
        minm = movingValue;
        }

      fixedMean += fixedValue;
      movingMean += movingValue;

      fixedSamplesA.insert(fixedSamplesA.begin(), (double)fixedValue);
      fixedGradientsA.insert(fixedGradientsA.begin(), fixedGradient);
      movingSamplesA.insert(movingSamplesA.begin(), (double)movingValue);

//        fixedSamplesB.insert(fixedSamplesB.begin(),(double)fixedValue);
//        fixedGradientsB.insert(fixedGradientsB.begin(),fixedGradient);
//        movingSamplesB.insert(movingSamplesB.begin(),(double)movingValue);

      sampct++;
      }
    }

// BEGIN RANDOM A SAMPLES
  bool getrandasamples = true;
  if ( getrandasamples )
    {
    typename FixedImageType::RegionType region = img->GetLargestPossibleRegion();

    ImageRandomIteratorWithIndex< FixedImageType > randasamit(img, region);
    unsigned int                                   numberOfSamples = 20;
    randasamit.SetNumberOfSamples(numberOfSamples);
//  numberOfSamples=100;

    indct = 0;

    randasamit.GoToBegin();
    while ( !randasamit.IsAtEnd() &&  indct < numberOfSamples )
      {
      IndexType index = randasamit.GetIndex();
      inimage = true;

      float d = 0.0;
      for ( unsigned int dd = 0; dd < ImageDimension; dd++ )
        {
        if ( index[dd] < 0 || index[dd] >
             static_cast< typename IndexType::IndexValueType >( imagesize[dd] - 1 ) ) { inimage = false; }
        d += ( index[dd] - oindex[dd] ) * ( index[dd] - oindex[dd] );
        }

      if ( inimage )
        {
        fixedValue = 0.;
        movingValue = 0.0;
        CovariantVectorType fixedGradient;
        double              fgm = 0;
        // Get fixed image related information
        fixedValue = (double)this->m_FixedImage->GetPixel(index);
        fixedGradient = m_FixedImageGradientCalculator->EvaluateAtIndex(index);

        for ( j = 0; j < ImageDimension; j++ )
          {
          fgm += fixedGradient[j] * fixedGradient[j];
          }
        // Get moving image related information
        typedef typename DisplacementFieldType::PixelType DeformationPixelType;
        const DeformationPixelType itvec = this->GetDisplacementField()->GetPixel(index);
        PointType                  mappedPoint;
        this->GetFixedImage()->TransformIndexToPhysicalPoint(index, mappedPoint);
        for ( j = 0; j < ImageDimension; j++ )
          {
          mappedPoint[j] += itvec[j];
          }
        if ( m_MovingImageInterpolator->IsInsideBuffer(mappedPoint) )
          {
          movingValue = m_MovingImageInterpolator->Evaluate(mappedPoint);
          }
        else
          {
          movingValue = 0.0;
          }

        //      if ( (fixedValue > 0 || movingValue > 0 || fgm > 0) ||
        // !filtersamples)

        if ( fixedValue > 0 || movingValue > 0 || fgm > 0 )
          {
          fixedMean += fixedValue;
          movingMean += movingValue;

          fixedSamplesA.insert(fixedSamplesA.begin(), (double)fixedValue);
          fixedGradientsA.insert(fixedGradientsA.begin(), fixedGradient);
          movingSamplesA.insert(movingSamplesA.begin(), (double)movingValue);
          sampct++;
          indct++;
          }
        }
      ++randasamit;
      }
    }
  // END RANDOM A SAMPLES

  const DisplacementFieldType *const field = this->GetDisplacementField();

  for ( j = 0; j < ImageDimension; j++ )
    {
    hradius[j] = 0;
    }
  ConstNeighborhoodIterator< DisplacementFieldType >
  hoodIt( hradius, field, field->GetRequestedRegion() );
  hoodIt.SetLocation(oindex);

  // then get the entropy ( and MI derivative ) related sample
  for ( indct = 0; indct < hoodIt.Size(); indct = indct + 1 )
    {
    const IndexType index = hoodIt.GetIndex(indct);
    inimage = true;
    float d = 0.0;
    for ( unsigned int dd = 0; dd < ImageDimension; dd++ )
      {
      if ( index[dd] < 0 || index[dd] >
           static_cast< typename IndexType::IndexValueType >( imagesize[dd] - 1 ) ) { inimage = false; }
      d += ( index[dd] - oindex[dd] ) * ( index[dd] - oindex[dd] );
      }
    if ( inimage  && vcl_sqrt(d) <= 1.0 )
      {
      fixedValue = 0.;
      movingValue = 0.0;
      CovariantVectorType fixedGradient;

      // Get fixed image related information
      fixedValue = (double)this->m_FixedImage->GetPixel(index);
      fixedGradient = m_FixedImageGradientCalculator->EvaluateAtIndex(index);

      // Get moving image related information
      // Get moving image related information
      const typename DisplacementFieldType::PixelType hooditvec = this->m_DisplacementField->GetPixel(index);
      PointType mappedPoint;
      this->GetFixedImage()->TransformIndexToPhysicalPoint(index, mappedPoint);
      for ( j = 0; j < ImageDimension; j++ )
        {
        mappedPoint[j] += hooditvec[j];
        }
      if ( m_MovingImageInterpolator->IsInsideBuffer(mappedPoint) )
        {
        movingValue = m_MovingImageInterpolator->Evaluate(mappedPoint);
        }
      else
        {
        movingValue = 0.0;
        }

      fixedSamplesB.insert(fixedSamplesB.begin(), (double)fixedValue);
      fixedGradientsB.insert(fixedGradientsB.begin(), fixedGradient);
      movingSamplesB.insert(movingSamplesB.begin(), (double)movingValue);
      }
    }

  double fsigma = 0.0;
  double msigma = 0.0;
  double jointsigma = 0.0;

  const double numsamplesB = (double)fixedSamplesB.size();
  const double numsamplesA = (double)fixedSamplesA.size();
  double       nsamp = numsamplesB;
//  if (maxf == minf && maxm == minm) return update;
//    else std::cout << " b samps " << fixedSamplesB.size()
//    << " a samps " <<  fixedSamplesA.size() <<
//    oindex  << hoodIt.Size() << it.Size() << std::endl;

  fixedMean /= (double)sampct;
  movingMean /= (double)sampct;

  bool mattes = false;

  for ( indct = 0; indct < (unsigned int)numsamplesA; indct++ )
    {
// Get fixed image related information
    fixedValue = fixedSamplesA[indct];
    movingValue = movingSamplesA[indct];

    fsigma += ( fixedValue - fixedMean ) * ( fixedValue - fixedMean );
    msigma += ( movingValue - movingMean ) * ( movingValue - movingMean );
    jointsigma += fsigma + msigma;

    if ( mattes )
      {
      fixedSamplesA[indct] = fixedSamplesA[indct] - minf;
      movingSamplesA[indct] = movingSamplesA[indct] - minm;
      if ( indct < numsamplesB )
        {
        fixedSamplesB[indct] = fixedSamplesB[indct] - minf;
        movingSamplesB[indct] = movingSamplesB[indct] - minm;
        }
      }
    }

  fsigma = vcl_sqrt(fsigma / numsamplesA);
  float  sigmaw = 0.8;
  double m_FixedImageStandardDeviation = fsigma * sigmaw;
  msigma = vcl_sqrt(msigma / numsamplesA);
  double m_MovingImageStandardDeviation = msigma * sigmaw;
  jointsigma = vcl_sqrt(jointsigma / numsamplesA);

  if ( fsigma < 1.e-7 || msigma < 1.e-7 ) { return update; }

  double       m_MinProbability = 0.0001;
  double       dLogSumFixed = 0., dLogSumMoving = 0., dLogSumJoint = 0.0;
  unsigned int bsamples;
  unsigned int asamples;

  // the B samples estimate the entropy
  for ( bsamples = 0; bsamples < (unsigned int)numsamplesB; bsamples++ )
    {
    double dDenominatorMoving = m_MinProbability;
    double dDenominatorJoint = m_MinProbability;
    double dDenominatorFixed = m_MinProbability;
    double dSumFixed = m_MinProbability;

    // this loop estimates the density
    for ( asamples = 0; asamples < (unsigned int)numsamplesA; asamples++ )
      {
      double valueFixed = ( fixedSamplesB[bsamples] - fixedSamplesA[asamples] )
                          / m_FixedImageStandardDeviation;
      valueFixed = vcl_exp(-0.5 * valueFixed * valueFixed);

      double valueMoving = ( movingSamplesB[bsamples] - movingSamplesA[asamples] )
                           / m_MovingImageStandardDeviation;
      valueMoving = vcl_exp(-0.5 * valueMoving * valueMoving);

      dDenominatorMoving += valueMoving;
      dDenominatorFixed += valueFixed;
      dSumFixed += valueFixed;

// everything above here can be pre-computed only once and stored,
//  assuming const v.f. in small n-hood
      dDenominatorJoint += valueMoving * valueFixed;
      } // end of sample A loop

    dLogSumFixed -= vcl_log(dSumFixed);
    dLogSumMoving -= vcl_log(dDenominatorMoving);
    dLogSumJoint -= vcl_log(dDenominatorJoint);

    // this loop estimates the density
    for ( asamples = 0; asamples < (unsigned int)numsamplesA; asamples++ )
      {
      double valueFixed = ( fixedSamplesB[bsamples] - fixedSamplesA[asamples] )
                          / m_FixedImageStandardDeviation;
      valueFixed = vcl_exp(-0.5 * valueFixed * valueFixed);

      double valueMoving = ( movingSamplesB[bsamples] - movingSamplesA[asamples] )
                           / m_MovingImageStandardDeviation;
      valueMoving = vcl_exp(-0.5 * valueMoving * valueMoving);
      const double weightFixed = valueFixed / dDenominatorFixed;
// dDenominatorJoint and weightJoint are what need to be computed each time
      const double weightJoint = valueMoving * valueFixed / dDenominatorJoint;

// begin where we may switch fixed and moving
      double weight = ( weightFixed - weightJoint );
      weight *= ( fixedSamplesB[bsamples] - fixedSamplesA[asamples] );
// end where we may switch fixed and moving

// this can also be stored away
      for ( unsigned int i = 0; i < ImageDimension; i++ )
        {
        derivative[i] += ( fixedGradientsB[bsamples][i] - fixedGradientsA[asamples][i] ) * weight;
        }
      } // end of sample A loop
    }   // end of sample B loop

  const double threshold = -0.1 *nsamp *vcl_log(m_MinProbability);
  if ( dLogSumMoving > threshold || dLogSumFixed > threshold
       || dLogSumJoint > threshold  )
    {
    // at least half the samples in B did not occur within
    // the Parzen window width of samples in A
    return update;
    }

  double value = 0.0;
  value  = dLogSumFixed + dLogSumMoving - dLogSumJoint;
  value /= nsamp;
  value += vcl_log(nsamp);

  m_MetricTotal += value;
  this->m_Energy += value;

  derivative /= nsamp;
  derivative /= vnl_math_sqr(m_FixedImageStandardDeviation);

  double updatenorm = 0.0;
  for ( unsigned int tt = 0; tt < ImageDimension; tt++ )
    {
    updatenorm += derivative[tt] * derivative[tt];
    }
  updatenorm = vcl_sqrt(updatenorm);

  if ( updatenorm > 1.e-20 && this->GetNormalizeGradient() )
    {
    derivative = derivative / updatenorm;
    }

  return derivative * this->GetGradientStep();
}
} // end namespace itk

#endif