File: elxRandomCoordinateSampler.hxx

package info (click to toggle)
elastix 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,480 kB
  • sloc: cpp: 68,403; lisp: 4,118; python: 1,013; xml: 182; sh: 177; makefile: 33
file content (114 lines) | stat: -rw-r--r-- 4,544 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
102
103
104
105
106
107
108
109
110
111
112
113
114
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  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 elxRandomCoordinateSampler_hxx
#define elxRandomCoordinateSampler_hxx

#include "elxRandomCoordinateSampler.h"
#include <itkDeref.h>
#include "itkLinearInterpolateImageFunction.h"

namespace elastix
{

/**
 * ******************* BeforeEachResolution ******************
 */

template <class TElastix>
void
RandomCoordinateSampler<TElastix>::BeforeEachResolution()
{
  const Configuration & configuration = itk::Deref(Superclass2::GetConfiguration());

  const unsigned int level = this->m_Registration->GetAsITKBaseType()->GetCurrentLevel();

  /** Set the NumberOfSpatialSamples. */
  unsigned long numberOfSpatialSamples = 5000;
  configuration.ReadParameter(numberOfSpatialSamples, "NumberOfSpatialSamples", this->GetComponentLabel(), level, 0);
  this->SetNumberOfSamples(numberOfSpatialSamples);

  /** Set up the fixed image interpolator and set the SplineOrder, default value = 1. */
  unsigned int splineOrder = 1;
  configuration.ReadParameter(splineOrder, "FixedImageBSplineInterpolationOrder", this->GetComponentLabel(), level, 0);
  if (splineOrder == 1)
  {
    using LinearInterpolatorType = itk::LinearInterpolateImageFunction<InputImageType, CoordRepType>;
    auto fixedImageLinearInterpolator = LinearInterpolatorType::New();
    this->SetInterpolator(fixedImageLinearInterpolator);
  }
  else
  {
    auto fixedImageBSplineInterpolator = DefaultInterpolatorType::New();
    fixedImageBSplineInterpolator->SetSplineOrder(splineOrder);
    this->SetInterpolator(fixedImageBSplineInterpolator);
  }

  /** Set the UseRandomSampleRegion bool. */
  bool useRandomSampleRegion = false;
  configuration.ReadParameter(useRandomSampleRegion, "UseRandomSampleRegion", this->GetComponentLabel(), level, 0);
  this->SetUseRandomSampleRegion(useRandomSampleRegion);

  /** Set the SampleRegionSize. */
  if (useRandomSampleRegion)
  {
    InputImageSpacingType sampleRegionSize;
    InputImageSpacingType fixedImageSpacing = this->GetElastix()->GetFixedImage()->GetSpacing();
    InputImageSizeType    fixedImageSize = this->GetElastix()->GetFixedImage()->GetLargestPossibleRegion().GetSize();

    /** Estimate default:
     * sampleRegionSize[i] = min ( fixedImageSizeInMM[i], max_i ( fixedImageSizeInMM[i]/3 ) )
     */
    double maxthird = 0.0;
    for (unsigned int i = 0; i < InputImageDimension; ++i)
    {
      sampleRegionSize[i] = (fixedImageSize[i] - 1) * fixedImageSpacing[i];
      maxthird = std::max(maxthird, sampleRegionSize[i] / 3.0);
    }
    for (unsigned int i = 0; i < InputImageDimension; ++i)
    {
      sampleRegionSize[i] = std::min(maxthird, sampleRegionSize[i]);
    }

    /** Read and check user's choice. */
    for (unsigned int i = 0; i < InputImageDimension; ++i)
    {
      configuration.ReadParameter(
        sampleRegionSize[i], "SampleRegionSize", this->GetComponentLabel(), level * InputImageDimension + i, 0);
    }
    this->SetSampleRegionSize(sampleRegionSize);

    for (unsigned int i = 0; i < InputImageDimension; ++i)
    {
      if (sampleRegionSize[i] > (fixedImageSize[i] - 1) * fixedImageSpacing[i])
      {
        itkExceptionMacro("ERROR: in your parameter file you selected\n"
                          << "  SampleRegionSize[ " << i << " ] = " << sampleRegionSize[i]
                          << " mm,\n  while the fixed image size at dim = " << i << " is " << fixedImageSize[i]
                          << " voxels or " << fixedImageSize[i] * fixedImageSpacing[i] << " mm.\n"
                          << "  Please select a smaller SampleRegionSize!\n"
                          << "  It is recommended to be not larger than 1/3 of the image size in mm.");
      }
    }
  }

} // end BeforeEachResolution()


} // end namespace elastix

#endif // end #ifndef elxRandomCoordinateSampler_hxx