[Insight-developers] precision truncation of 'delta' in ResampleImageFilter

Michael Stauffer mstauff at verizon.net
Mon Mar 21 16:28:20 EDT 2011


Hi,

I'm cleaning up ResampleImageFilter, removing redundant and unnecessary code. I have a question about lines 637-661, copied below. 'git blame' shows the code was originally from Dirk Padfield, and most recently edited by David Cole and Hans Johnson.

This code truncates values to a set precision to avoid accumulated rouding errors while repeatedly adding a small delta. The comments here suggest it should be truncating the 'delta' variable, but it's operating on 'inputIndex'. As best I can understand, it should be operating on 'delta' and not 'inputIndex'. As it stands, 'delta' doesn't get truncated to the desired precision at all. 'inputIndex' is initialized before this code in order to calculate delta, then it's set again in the subsequent while loop, to what looks like the same value in the first loop iteration, so this precision adjustment here actually doesn't do anything.

The code is repeated again later in the subsequent while loop, where it operates on 'inputIndex', which looks correct. 

Modules/Filtering/ImageGrid/include/itkResampleImageFilter.txx
Lines 637-661:

  // Delta is precise to many decimal points, but this precision
  // involves some error in the last bits.  This error can accumulate
  // as the delta values are added.
  // Sometimes, when the accumulated delta should be inside of the
  // image, it will be slightly
  // greater than the largest index in the image, like 255.00000000002
  // for a image of size 256.  This can cause an empty column to show up
  // at the right side of the image. If we instead
  // truncate this delta value to some precision, this solves the problem.
  // Therefore, the following routine uses a
  // precisionConstant that specifies the number of relevant bits,
  // and the value is truncated to this precision.
  for ( unsigned int i = 0; i < ImageDimension; ++i )
    {
    IndexValueType roundedInputIndex = (IndexValueType)( inputIndex[i] );
    if ( inputIndex[i] < 0.0 && inputIndex[i] != (double)roundedInputIndex )
      {
      --roundedInputIndex;
      }
    double inputIndexFrac = inputIndex[i] - roundedInputIndex;
    double newInputIndexFrac = (IndexValueType)( precisionConstant
                                       * inputIndexFrac )
                               / precisionConstant;
    inputIndex[i] = roundedInputIndex + newInputIndexFrac;
    }


More information about the Insight-developers mailing list