File: elxGenericPyramidHelper.h

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 (207 lines) | stat: -rw-r--r-- 8,728 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
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
/*=========================================================================
 *
 *  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 elxGenericPyramidHelper_h
#define elxGenericPyramidHelper_h

#include <string>      // For is_same.
#include <type_traits> // For is_same.

#include "elxConfiguration.h"
#include <itkDeref.h>

namespace elastix
{
template <class TElastix>
class FixedGenericPyramid;

template <class TElastix>
class MovingGenericPyramid;

/** Internal helper class for FixedGenericPyramid and MovingGenericPyramid. */
class GenericPyramidHelper
{
public:
  /** Sets the schedule of a fixed or moving generic pyramid. */
  template <typename TPyramid>
  static void
  SetSchedule(TPyramid & pyramid)
  {
    using ElastixType = typename TPyramid::ElastixType;

    constexpr bool isFixed = std::is_same_v<TPyramid, FixedGenericPyramid<ElastixType>>;
    constexpr bool isMoving = std::is_same_v<TPyramid, MovingGenericPyramid<ElastixType>>;

    static_assert(isMoving != isFixed, "TPyramid must be either FixedGenericPyramid or MovingGenericPyramid!");

    const std::string      parameterPrefix = isFixed ? "Fixed" : "Moving";
    constexpr const char * pyramidAdjective = isFixed ? "fixed" : "moving";

    // It is assumed for more than ten years already that the configuration is not null, looking at
    // "src/Components/FixedImagePyramids/FixedGenericPyramid/elxFixedGenericPyramid.hxx" revision
    // f84ac0d1094ebdb13e456b1b8cf1f6f9bfcd0a38 "ENH: First checkin of a generic pyramid...", Marius Staring, 2012-02-02

    const Configuration & configuration = itk::Deref(pyramid.GetConfiguration());

    /** Get the ImageDimension. */
    const unsigned int ImageDimension = TPyramid::ImageDimension;

    /** Read numberOfResolutions. */
    unsigned int numberOfResolutions = 3;
    configuration.ReadParameter(numberOfResolutions, "NumberOfResolutions", 0, true);
    if (numberOfResolutions == 0)
    {
      itkGenericExceptionMacro("The NumberOfResolutions parameter must have a non-zero value!");
    }

    /** Create a default schedule. Set the numberOfLevels first. */
    pyramid.GetAsITKBaseType()->SetNumberOfLevels(numberOfResolutions);
    typename TPyramid::RescaleScheduleType   rescaleSchedule = pyramid.GetRescaleSchedule();
    typename TPyramid::SmoothingScheduleType smoothingSchedule = pyramid.GetSmoothingSchedule();

    /** Set the schedule for rescaling.
     * The following parameter file fields can be used:
     * - ImagePyramidRescaleSchedule
     * - ImagePyramidSchedule
     * - Fixed/MovingImagePyramidRescaleSchedule
     * - Fixed/MovingImagePyramidSchedule
     */
    bool foundRescale = true;
    for (unsigned int i = 0; i < numberOfResolutions; ++i)
    {
      for (unsigned int j = 0; j < ImageDimension; ++j)
      {
        bool               ijfound = false;
        const unsigned int entrynr = i * ImageDimension + j;
        ijfound |= configuration.ReadParameter(rescaleSchedule[i][j], "ImagePyramidRescaleSchedule", entrynr, false);
        ijfound |= configuration.ReadParameter(rescaleSchedule[i][j], "ImagePyramidSchedule", entrynr, false);
        ijfound |= configuration.ReadParameter(
          rescaleSchedule[i][j], parameterPrefix + "ImagePyramidRescaleSchedule", entrynr, false);
        ijfound |=
          configuration.ReadParameter(rescaleSchedule[i][j], parameterPrefix + "ImagePyramidSchedule", entrynr, false);

        /** Remember if for at least one schedule element no value could be found. */
        foundRescale &= ijfound;

      } // end for ImageDimension
    }   // end for numberOfResolutions

    if (!foundRescale && pyramid.GetConfiguration()->GetPrintErrorMessages())
    {
      log::warn(std::ostringstream{} << "WARNING: the " << pyramidAdjective
                                     << " pyramid rescale schedule is not fully specified!\n"
                                     << "  A default pyramid rescale schedule is used.");
    }
    else
    {
      /** Set the rescale schedule into this class. */
      pyramid.SetRescaleSchedule(rescaleSchedule);

      const auto newSchedule = pyramid.GetRescaleSchedule();

      if (newSchedule != rescaleSchedule)
      {
        log::warn(
          std::ostringstream{} << "WARNING: the " << pyramidAdjective
                               << " pyramid rescale schedule is adjusted!\n  Input schedule from configuration:\n"
                               << rescaleSchedule << "\n  Adjusted schedule:\n"
                               << newSchedule);
      }
    }

    /** Set the schedule for smoothing (sigmas).
     * The following parameter file fields can be used:
     * - ImagePyramidSmoothingSchedule
     * - Fixed/MovingImagePyramidSmoothingSchedule
     */
    bool foundSmoothing = true;
    for (unsigned int i = 0; i < numberOfResolutions; ++i)
    {
      for (unsigned int j = 0; j < ImageDimension; ++j)
      {
        bool               ijfound = false;
        const unsigned int entrynr = i * ImageDimension + j;
        ijfound |=
          configuration.ReadParameter(smoothingSchedule[i][j], "ImagePyramidSmoothingSchedule", entrynr, false);
        ijfound |= configuration.ReadParameter(
          smoothingSchedule[i][j], parameterPrefix + "ImagePyramidSmoothingSchedule", entrynr, false);

        /** Remember if for at least one schedule element no value could be found. */
        foundSmoothing &= ijfound;

      } // end for ImageDimension
    }   // end for numberOfResolutions

    if (!foundSmoothing && pyramid.GetConfiguration()->GetPrintErrorMessages())
    {
      log::warn(std::ostringstream{} << "WARNING: the " << pyramidAdjective
                                     << " pyramid smoothing schedule is not fully specified!\n"
                                     << "  A default pyramid smoothing schedule is used.");
    }
    else
    {
      /** Set the rescale schedule into this class. */
      pyramid.SetSmoothingSchedule(smoothingSchedule);

      const auto newSchedule = pyramid.GetSmoothingSchedule();

      if (newSchedule != smoothingSchedule)
      {
        log::warn(
          std::ostringstream{} << "WARNING: the " << pyramidAdjective
                               << " pyramid smoothing schedule is adjusted!\n  Input schedule from configuration:\n"
                               << smoothingSchedule << "\n  Adjusted schedule:\n"
                               << newSchedule);
      }
    }

    // configuration.CountNumberOfParameterEntries( "ImagePyramidRescaleSchedule" );

    /** Use or skip rescaling within the pyramid. */
    /** Currently not used since the same can be obtained by just specifying
     * all ones in the rescale schedule. */
    /*bool useRescaleSchedule = true;
    configuration.ReadParameter( useRescaleSchedule,
          "UseImagePyramidRescaleSchedule", 0, false );
    pyramid.SetUseMultiResolutionRescaleSchedule( useRescaleSchedule );*/

    /** Use or skip smoothing within the pyramid. */
    /** Currently not used since the same can be obtained by just specifying
     * all zeros in the smoothing schedule. */
    /*bool useSmoothingSchedule = true;
    configuration.ReadParameter( useSmoothingSchedule,
          "UseImagePyramidSmoothingSchedule", 0, false );
    pyramid.SetUseMultiResolutionSmoothingSchedule( useSmoothingSchedule );*/

    /** Use or the resampler or the shrinker for rescaling within the pyramid. */
    bool useShrinkImageFilter = false;
    configuration.ReadParameter(useShrinkImageFilter, "ImagePyramidUseShrinkImageFilter", 0, false);
    pyramid.SetUseShrinkImageFilter(useShrinkImageFilter);

    /** Decide whether or not to compute the pyramid images only for the current
     * resolution. Setting the option to true saves memory, since only one level
     * of the pyramid gets allocated per resolution.
     */
    bool computeThisResolution = false;
    configuration.ReadParameter(computeThisResolution, "ComputePyramidImagesPerResolution", 0, false);
    pyramid.SetComputeOnlyForCurrentLevel(computeThisResolution);
  }
};

} // namespace elastix

#endif