File: itkGenericMultiResolutionPyramidImageFilter.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 (371 lines) | stat: -rw-r--r-- 14,876 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
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
/*=========================================================================
 *
 *  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 itkGenericMultiResolutionPyramidImageFilter_h
#define itkGenericMultiResolutionPyramidImageFilter_h

#include "itkMultiResolutionPyramidImageFilter.h"
#include "itkSmoothingRecursiveGaussianImageFilter.h"

namespace itk
{
/** \class GenericMultiResolutionPyramidImageFilter
 * \brief Framework for creating images in a multi-resolution
 * pyramid
 *
 * GenericMultiResolutionPyramidImageFilter provides a generic framework to
 * to create a image pyramid according to a user defined multi-resolution
 * rescale and smoothing schedules.
 *
 * The multi-resolution rescale schedule is specified in terms of
 * shrink factors at each multi-resolution level for each dimension
 *
 * The rescale schedule is stored as an unsigned int matrix.
 * An element of the table can be access via the double bracket
 * notation: schedule[level][dimension]
 * For example:
 * 8 4 4
 * 4 4 2
 *
 * is a rescale schedule for two computation level. In the first (coarest)
 * level the image is reduce by a factor of 8 in the column dimension,
 * factor of 4 in the row dimension and factor of 4 in the slice dimension.
 * In the second level, the image is reduce by a factor of 4 in the column
 * dimension, 4 is the row dimension and 2 in the slice dimension.
 *
 * The method SetNumberOfLevels() set the number of
 * computation levels in the pyramid. This method will
 * allocate memory for the multi-resolution rescale schedule table.
 * This method generates defaults tables with the starting
 * shrink factor for all dimension set to 2^(NumberOfLevel - 1)
 * All factors are halved for all subsequent levels.
 * For example if the number of levels was set to 4, the default table is:
 * 8 8 8
 * 4 4 4
 * 2 2 2
 * 1 1 1
 *
 * The user can get a copy of the rescale schedule via GetRescaleSchedule()
 * They may make alteration and reset it using SetRescaleSchedule()
 *
 * To generate each output image, recursive Gaussian smoothing is performed
 * using a SmoothingRecursiveGaussianImageFilter.
 *
 * The user can make alteration on smoothing schedule via SetSmoothingSchedule()
 * For example, for 4 levels smoothing schedule would be:
 * 3 4 5
 * 2 2 2
 * 0 1 2
 * 0 0 0
 *
 * In the first level all sigma are set to the same value 2 across each axis.
 * Sigma is measured in the units of image spacing. Use different values along
 * each axis if you would like perform nonidentical smoothing (see level 1)
 * Although for the level 2 no smoothing will be performed because all sigma
 * values are equal zeros. For the last level 3 smoothing will be performed with
 * sigma 0 for x axis.
 *
 * The default smoothing schedule is derived from the rescale schedule, where
 * each element is computed as: 0.5 * rescale_factor * image_spacing.
 *
 * The user can get a copy of the schedule via GetSmoothingSchedule()
 *
 * The smoothed image is then downsampled using a ResampleImageFilter or
 * ShrinkImageFilter depending on SetUseShrinkImageFilter().
 *
 * When this filter is updated, NumberOfLevels outputs are produced.
 * The N'th output correspond to the N'th level of the pyramid.
 *
 * The user can influence whether or not rescale schedule or smoothing schedule
 * will be used via SetRescaleScheduleToUnity() and
 * SetSmoothingScheduleToZero() methods.
 *
 * The GenericMultiResolutionPyramidImageFilter provides direct control to
 * compute only single level of the pyramid via SetCurrentLevel() and
 * SetComputeOnlyForCurrentLevel() methods.
 *
 * \author Denis P. Shamonin and Marius Staring. Division of Image Processing,
 * Department of Radiology, Leiden, The Netherlands
 *
 * This implementation was taken from elastix (http://elastix.dev/).
 *
 * \note This work was funded by the Netherlands Organisation for
 * Scientific Research (NWO NRG-2010.02 and NWO 639.021.124).
 *
 * \sa SmoothingRecursiveGaussianImageFilter
 * \sa ResampleImageFilter
 * \sa ShrinkImageFilter
 *
 * \ingroup PyramidImageFilter MultiThreaded Streamed
 * \ingroup ITKRegistrationCommon
 */
template <class TInputImage, class TOutputImage, class TPrecisionType = double>
class ITK_TEMPLATE_EXPORT GenericMultiResolutionPyramidImageFilter
  : public MultiResolutionPyramidImageFilter<TInputImage, TOutputImage>
{
public:
  ITK_DISALLOW_COPY_AND_MOVE(GenericMultiResolutionPyramidImageFilter);

  /** Standard class typedefs. */
  using Self = GenericMultiResolutionPyramidImageFilter;
  using Superclass = MultiResolutionPyramidImageFilter<TInputImage, TOutputImage>;
  using SuperSuperclass = typename Superclass::Superclass;
  using Pointer = SmartPointer<Self>;
  using ConstPointer = SmartPointer<const Self>;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** Run-time type information (and related methods). */
  itkTypeMacro(GenericMultiResolutionPyramidImageFilter, MultiResolutionPyramidImageFilter);

  /** ImageDimension enumeration. */
  itkStaticConstMacro(ImageDimension, unsigned int, TInputImage::ImageDimension);
  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);

  /** Inherit types from Superclass. */
  using typename Superclass::ScheduleType;
  using typename Superclass::InputImageType;
  using typename Superclass::OutputImageType;
  using typename Superclass::InputImagePointer;
  using typename Superclass::OutputImagePointer;
  using typename Superclass::InputImageConstPointer;
  using SpacingType = typename Superclass::InputImageType::SpacingType;
  using PixelType = typename InputImageType::PixelType;
  using ScalarRealType = typename NumericTraits<PixelType>::ScalarRealType;

  /** SmoothingScheduleType typedef support. */
  using SmoothingScheduleType = vnl_matrix<ScalarRealType>;
  using RescaleScheduleType = ScheduleType;

  /** Define the type for the sigma array. */
  using SigmaArrayType = FixedArray<ScalarRealType, Self::ImageDimension>;
  using RescaleFactorArrayType = SigmaArrayType;

  /** Set a multi-resolution schedule. The input schedule must have only
   * ImageDimension number of columns and NumberOfLevels number of rows. For
   * each dimension, the shrink factor must be non-increasing with respect to
   * subsequent levels. This function will clamp shrink factors to satisfy
   * this condition. All shrink factors less than one will also be clamped
   * to the value of 1.
   */
  void
  SetSchedule(const ScheduleType & schedule) override;

  /** Set a multi-resolution rescale schedule. The input schedule must have only
   * ImageDimension number of columns and NumberOfLevels number of rows. For
   * each dimension, the shrink factor must be non-increasing with respect to
   * subsequent levels. This function will clamp shrink factors to satisfy
   * this condition. All shrink factors less than one will also be clamped
   * to the value of 1.
   */
  virtual void
  SetRescaleSchedule(const RescaleScheduleType & schedule);

  /** Set a multi-resolution rescale schedule with ones. */
  virtual void
  SetRescaleScheduleToUnity();

  /** Get the multi-resolution rescale schedule. */
  const RescaleScheduleType &
  GetRescaleSchedule() const
  {
    return this->m_Schedule;
  }


  /** Set a multi-resolution smoothing schedule. The input schedule must have only
   * ImageDimension number of columns and NumberOfLevels number of rows.
   * All sigmas less than 0 will also be clamped to the value of 0.
   */
  virtual void
  SetSmoothingSchedule(const SmoothingScheduleType & schedule);

  /** Set a multi-resolution rescale schedule with zeros. */
  virtual void
  SetSmoothingScheduleToZero();

  /** Get the multi-resolution smoothing schedule. */
  itkGetConstReferenceMacro(SmoothingSchedule, SmoothingScheduleType);

  /** Set the number of multi-resolution levels. The matrices containing the
   * schedule will be resized accordingly. The schedules are populated with
   * default values.
   */
  void
  SetNumberOfLevels(unsigned int num) override;

  /** Set the current multi-resolution levels. The current level is clamped to
   * a total number of levels.
   */
  virtual void
  SetCurrentLevel(unsigned int level);

  /** Get the current multi-resolution level. */
  itkGetConstReferenceMacro(CurrentLevel, unsigned int);

  /** Set a control on whether a current level will be used. */
  virtual void
  SetComputeOnlyForCurrentLevel(const bool _arg);

  itkGetConstMacro(ComputeOnlyForCurrentLevel, bool);
  itkBooleanMacro(ComputeOnlyForCurrentLevel);

#ifdef ITK_USE_CONCEPT_CHECKING
  /** Begin concept checking */
  itkConceptMacro(SameDimensionCheck, (Concept::SameDimension<ImageDimension, OutputImageDimension>));
  itkConceptMacro(OutputHasNumericTraitsCheck, (Concept::HasNumericTraits<typename TOutputImage::PixelType>));
  /** End concept checking */
#endif

protected:
  GenericMultiResolutionPyramidImageFilter();
  ~GenericMultiResolutionPyramidImageFilter() override = default;

  /** PrintSelf. */
  void
  PrintSelf(std::ostream & os, Indent indent) const override;

  /** GenericMultiResolutionPyramidImageFilter may produce images which are of
   * different resolution and different pixel spacing than its input image.
   * As such, GenericMultiResolutionPyramidImageFilter needs to provide an
   * implementation for GenerateOutputInformation() in order to inform the
   * pipeline execution model. The original documentation of this method is
   * below. \sa ProcessObject::GenerateOutputInformaton().
   */
  void
  GenerateOutputInformation() override;

  /** Given one output whose requested region has been set, this method sets
   * the requested region for the remaining output images. The original
   * documentation of this method is below. \sa
   * ProcessObject::GenerateOutputRequestedRegion().
   */
  void
  GenerateOutputRequestedRegion(DataObject * output) override;

  /** Overwrite the Superclass implementation: no padding required. */
  void
  GenerateInputRequestedRegion() override;

  /** Generate the output data. */
  void
  GenerateData() override;

  /** Release the output data when the current level is used. */
  void
  ReleaseOutputs();

  SmoothingScheduleType m_SmoothingSchedule{};
  unsigned int          m_CurrentLevel{};
  bool                  m_ComputeOnlyForCurrentLevel{};
  bool                  m_SmoothingScheduleDefined{};

private:
  /** Typedef for smoother. Smooth always happens first, then only from
   * InputImageType to OutputImageType is possible.
   */
  using SmootherType = SmoothingRecursiveGaussianImageFilter<InputImageType, OutputImageType>;

  /** Typedefs for shrinker or resample. If smoother has not been used, then
   * we have to use InputImageType to OutputImageType,
   * otherwise OutputImageType to OutputImageType.
   */
  using ImageToImageFilterSameTypes = ImageToImageFilter<OutputImageType, OutputImageType>;
  using ImageToImageFilterDifferentTypes = ImageToImageFilter<InputImageType, OutputImageType>;

  /** Smooth image at current level. Returns true if performed.
   * This method does not perform execution.
   */
  bool
  SetupSmoother(const unsigned int               level,
                typename SmootherType::Pointer & smoother,
                const InputImageConstPointer &   input);

  /** Shrink or Resample image at current level. Returns 1 or 2 if performed,
   * 0 otherwise. This method does not perform execution.
   */
  int
  SetupShrinkerOrResampler(const unsigned int                                   level,
                           typename SmootherType::Pointer &                     smoother,
                           const bool                                           sameType,
                           const InputImageConstPointer &                       input,
                           const OutputImagePointer &                           outputPtr,
                           typename ImageToImageFilterSameTypes::Pointer &      rescaleSameTypes,
                           typename ImageToImageFilterDifferentTypes::Pointer & rescaleDifferentTypes);

  /** Defines Shrink or Resample filters. */
  void
  DefineShrinkerOrResampler(const bool                                           sameType,
                            const RescaleFactorArrayType &                       shrinkFactors,
                            const OutputImagePointer &                           outputPtr,
                            typename ImageToImageFilterSameTypes::Pointer &      rescaleSameTypes,
                            typename ImageToImageFilterDifferentTypes::Pointer & rescaleDifferentTypes);

  /** Initialize m_SmoothingSchedule to default values for backward compatibility. */
  void
  SetSmoothingScheduleToDefault();

  /** Checks whether we have to compute anything based on
   * m_ComputeOnlyForCurrentLevel and m_CurrentLevel.
   */
  bool
  ComputeForCurrentLevel(const unsigned int level) const;

  /** Backward compatibility method to compute default sigma value. */
  double
  GetDefaultSigma(const unsigned int   level,
                  const unsigned int   dim,
                  const unsigned int * factors,
                  const SpacingType &  spacing) const;

  /** Get sigmas from m_SmoothingSchedule for the level. */
  void
  GetSigma(const unsigned int level, SigmaArrayType & sigmaArray) const;

  /** Get shrink factors from m_Schedule for the level. */
  void
  GetShrinkFactors(const unsigned int level, RescaleFactorArrayType & shrinkFactors) const;

  /** Returns true if all elements of sigmaArray are zeros,
   * otherwise return false.
   */
  bool
  AreSigmasAllZeros(const SigmaArrayType & sigmaArray) const;

  /** Returns true if all elements of rescaleFactorArray are ones,
   * otherwise return false.
   */
  bool
  AreRescaleFactorsAllOnes(const RescaleFactorArrayType & rescaleFactorArray) const;

  /** Returns true if smooth has been used in pipeline, otherwise return false. */
  bool
  IsSmoothingUsed() const;

  /** Returns true if rescale has been used in pipeline, otherwise return false. */
  bool
  IsRescaleUsed() const;
};

} // namespace itk

#ifndef ITK_MANUAL_INSTANTIATION
#  include "itkGenericMultiResolutionPyramidImageFilter.hxx"
#endif

#endif