File: itkBSplineResampleImageFunctionTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (101 lines) | stat: -rw-r--r-- 3,860 bytes parent folder | download
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
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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.
 *
 *=========================================================================*/
/*=========================================================================
 *
 *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
 *
 *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
 *
 *  For complete copyright, license and disclaimer of warranty information
 *  please refer to the NOTICE file at the top of the ITK source tree.
 *
 *=========================================================================*/
#include "itkSimpleFilterWatcher.h"
#include "vnl/vnl_sample.h"
#include "makeRandomImageBsplineInterpolator.h"

/** Note:  This is the same test used for the itkBSplineDecompositionFilter
 *        It is duplicated here because it exercises the itkBSplineResampleImageFunctionTest
 *        and demonstrates its use.
 */

int
itkBSplineResampleImageFunctionTest(int, char *[])
{
  constexpr unsigned int ImageDimension = 2;
  using PixelType = float;
  using ImageType = itk::Image<PixelType, ImageDimension>;
  using BSplineInterpolatorFunctionType = itk::BSplineInterpolateImageFunction<ImageType, double, double>;

  constexpr unsigned int                   SplineOrder = 3;
  BSplineInterpolatorFunctionType::Pointer interpolator =
    makeRandomImageInterpolator<BSplineInterpolatorFunctionType>(SplineOrder);
  ImageType::ConstPointer randImage = interpolator->GetInputImage();

  using FilterType = itk::BSplineDecompositionImageFilter<ImageType, ImageType>;
  auto                     filter = FilterType::New();
  itk::SimpleFilterWatcher watcher(filter, "filter");

  filter->SetSplineOrder(interpolator->GetSplineOrder());
  filter->SetInput(randImage);
  filter->Update();

  filter->Print(std::cout);

  /** Set up a BSplineResampleImageFunction. */
  using ResampleFunctionType = itk::BSplineResampleImageFunction<ImageType, double>;
  auto resample = ResampleFunctionType::New();

  resample->SetSplineOrder(interpolator->GetSplineOrder());
  resample->SetInputImage(filter->GetOutput());

  /** Compare 10 values at random points. */

  ImageType::IndexType Last;
  Last.Fill(0);
  Last[0] = randImage->GetLargestPossibleRegion().GetSize()[0] - 1;
  ImageType::PointType LastPhysicalLocation;
  randImage->TransformIndexToPhysicalPoint(Last, LastPhysicalLocation);

  const double minValue = randImage->GetOrigin()[0];
  const double maxValue = LastPhysicalLocation[0];

  for (unsigned int k = 0; k < 10; ++k)
  {
    ResampleFunctionType::PointType point;
    for (unsigned int j = 0; j < ImageDimension; ++j)
    {
      point[j] = vnl_sample_uniform(minValue, maxValue);
    }

    const double f = resample->Evaluate(point);
    const double g = interpolator->Evaluate(point);

    if (itk::Math::abs(f - g) > 1e-5)
    {
      std::cout << "Resample and Interpolated point are different." << std::endl;
      std::cout << " point: " << point << std::endl;
      std::cout << " resample: " << resample->Evaluate(point) << std::endl;
      std::cout << " interpolator: " << interpolator->Evaluate(point) << std::endl;
      std::cout << " Test failed. " << std::endl;
      return EXIT_FAILURE;
    }
  }

  return EXIT_SUCCESS;
}