BSplineInterpolateImageFunction.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_BSplineInterpolateImageFunction_H
21 #define MIRTK_BSplineInterpolateImageFunction_H
22 
23 #include "mirtk/InterpolateImageFunction.h"
24 #include "mirtk/MirrorExtrapolateImageFunction.h"
25 
26 
27 namespace mirtk {
28 
29 
30 /**
31  * B-spline interpolation of generic image
32  *
33  * This class defines and implements the B-spline interpolation of images.
34  * Currently supports B-splines of degree 2 to degree 5. By default splines
35  * of degree 3 (cubic) are used. For more details see:
36  *
37  * M. Unser, "Splines: A Perfect Fit for Signal and Image Processing," IEEE
38  * Signal Processing Magazine, vol. 16, no. 6, pp. 22-38, November 1999.
39  *
40  */
41 template <class TImage>
43 : public GenericInterpolateImageFunction<TImage>
44 {
45  mirtkGenericInterpolatorMacro(
47  Interpolation_BSpline
48  );
49 
50  // ---------------------------------------------------------------------------
51  // Types
52 
53 public:
54 
58 
59  // ---------------------------------------------------------------------------
60  // Attributes
61 
62  /// Degree of B-spline
63  mirtkPublicAttributeMacro(int, SplineDegree);
64 
65  /// Input image contains spline coefficients
66  mirtkAttributeMacro(bool, UseInputCoefficients);
67 
68  /// Image of spline coefficients
69  mirtkAttributeMacro(CoefficientImage, Coefficient);
70 
71  /// Infinite discrete coefficient image obtained by extrapolation
72  mirtkComponentMacro(CoefficientExtrapolator, InfiniteCoefficient);
73 
74 public:
75 
76  // ---------------------------------------------------------------------------
77  // Construction/Destruction
78 
79  /// Constructor
81 
82  /// Destructor
84 
85  /// Initialize interpolation function
86  virtual void Initialize(bool = false);
87 
88  /// Update spline coefficients
89  virtual void Update();
90 
91  // ---------------------------------------------------------------------------
92  // Domain checks
93 
94  /// Returns interval of discrete image indices whose values are needed for
95  /// interpolation of the image value at a given continuous coordinate
96  virtual void BoundingInterval(double, int &, int &) const;
97 
98  // ---------------------------------------------------------------------------
99  // Evaluation
100 
101  /// Get value of given 2D image at arbitrary location (in pixels)
102  ///
103  /// This function is used to interpolate the image value at arbitrary
104  /// locations when no extrapolator was set.
105  VoxelType Get2D(double, double, double = 0, double = 0) const;
106 
107  /// Get value of given 2D image at arbitrary location (in pixels)
108  ///
109  /// This function is used to only interpolate foreground image values.
110  /// If fully outside the foreground region, the _DefaultValue is returned.
111  VoxelType GetWithPadding2D(double, double, double = 0, double = 0) const;
112 
113  /// Get value of given 2D image at arbitrary location (in pixels)
114  ///
115  /// If the location is inside the finite domain of the image, an actual image
116  /// instance can be passed as first argument directly such as an instance of
117  /// GenericImage. Otherwise, an image function which extends the finite
118  /// image domain to an infinite lattice is needed, i.e., an instance of a
119  /// subclass of ExtrapolateImageFunction.
120  template <class TOtherImage> typename TOtherImage::VoxelType
121  Get2D(const TOtherImage *, double, double, double = 0, double = 0) const;
122 
123  /// Get value of given 2D image at arbitrary location (in pixels)
124  ///
125  /// This function is used to only interpolate foreground image values.
126  /// If fully outside the foreground region, the _DefaultValue is returned.
127  ///
128  /// If the location is inside the finite domain of the image, an actual image
129  /// instance can be passed as first argument directly such as an instance of
130  /// GenericImage. Otherwise, an image function which extends the finite
131  /// image domain to an infinite lattice is needed, i.e., an instance of a
132  /// subclass of ExtrapolateImageFunction.
133  template <class TOtherImage, class TCoefficient> typename TCoefficient::VoxelType
134  GetWithPadding2D(const TOtherImage *, const TCoefficient *,
135  double, double, double = 0, double = 0) const;
136 
137  /// Get value of given 3D image at arbitrary location (in pixels)
138  ///
139  /// This function is used to interpolate the image value at arbitrary
140  /// locations when no extrapolator was set.
141  VoxelType Get3D(double, double, double = 0, double = 0) const;
142 
143  /// Get value of given 3D image at arbitrary location (in pixels)
144  ///
145  /// This function is used to only interpolate foreground image values.
146  /// If fully outside the foreground region, the _DefaultValue is returned.
147  VoxelType GetWithPadding3D(double, double, double = 0, double = 0) const;
148 
149  /// Get value of given 3D image at arbitrary location (in pixels)
150  ///
151  /// If the location is inside the finite domain of the image, an actual image
152  /// instance can be passed as first argument directly such as an instance of
153  /// GenericImage. Otherwise, an image function which extends the finite
154  /// image domain to an infinite lattice is needed, i.e., an instance of a
155  /// subclass of ExtrapolateImageFunction.
156  template <class TOtherImage> typename TOtherImage::VoxelType
157  Get3D(const TOtherImage *, double, double, double = 0, double = 0) const;
158 
159  /// Get value of given 3D image at arbitrary location (in pixels)
160  ///
161  /// This function is used to only interpolate foreground image values.
162  /// If fully outside the foreground region, the _DefaultValue is returned.
163  ///
164  /// If the location is inside the finite domain of the image, an actual image
165  /// instance can be passed as first argument directly such as an instance of
166  /// GenericImage. Otherwise, an image function which extends the finite
167  /// image domain to an infinite lattice is needed, i.e., an instance of a
168  /// subclass of ExtrapolateImageFunction.
169  template <class TOtherImage, class TCoefficient> typename TCoefficient::VoxelType
170  GetWithPadding3D(const TOtherImage *, const TCoefficient *,
171  double, double, double = 0, double = 0) const;
172 
173  /// Get value of given 4D image at arbitrary location (in pixels)
174  ///
175  /// This function is used to interpolate the image value at arbitrary
176  /// locations when no extrapolator was set.
177  VoxelType Get4D(double, double, double = 0, double = 0) const;
178 
179  /// Get value of given 4D image at arbitrary location (in pixels)
180  ///
181  /// This function is used to only interpolate foreground image values.
182  /// If fully outside the foreground region, the _DefaultValue is returned.
183  VoxelType GetWithPadding4D(double, double, double = 0, double = 0) const;
184 
185  /// Get value of given 4D image at arbitrary location (in pixels)
186  ///
187  /// If the location is inside the finite domain of the image, an actual image
188  /// instance can be passed as first argument directly such as an instance of
189  /// GenericImage. Otherwise, an image function which extends the finite
190  /// image domain to an infinite lattice is needed, i.e., an instance of a
191  /// subclass of ExtrapolateImageFunction.
192  template <class TOtherImage> typename TOtherImage::VoxelType
193  Get4D(const TOtherImage *, double, double, double = 0, double = 0) const;
194 
195  /// Get value of given 4D image at arbitrary location (in pixels)
196  ///
197  /// This function is used to only interpolate foreground image values.
198  /// If fully outside the foreground region, the _DefaultValue is returned.
199  ///
200  /// If the location is inside the finite domain of the image, an actual image
201  /// instance can be passed as first argument directly such as an instance of
202  /// GenericImage. Otherwise, an image function which extends the finite
203  /// image domain to an infinite lattice is needed, i.e., an instance of a
204  /// subclass of ExtrapolateImageFunction.
205  template <class TOtherImage, class TCoefficient> typename TCoefficient::VoxelType
206  GetWithPadding4D(const TOtherImage *, const TCoefficient *,
207  double, double, double = 0, double = 0) const;
208 
209  /// Get value of given image at arbitrary location (in pixels)
210  ///
211  /// This function is used to interpolate the image value at arbitrary
212  /// locations when no extrapolator was set.
213  VoxelType Get(double, double, double = 0, double = 0) const;
214 
215  /// Get value of given image at arbitrary location (in pixels)
216  ///
217  /// This function is used to only interpolate foreground image values.
218  /// If fully outside the foreground region, the _DefaultValue is returned.
219  VoxelType GetWithPadding(double, double, double = 0, double = 0) const;
220 
221  /// Get value of given image at arbitrary location (in pixels)
222  ///
223  /// If the location is inside the finite domain of the image, an actual image
224  /// instance can be passed as first argument directly such as an instance of
225  /// GenericImage. Otherwise, an image function which extends the finite
226  /// image domain to an infinite lattice is needed, i.e., an instance of a
227  /// subclass of ExtrapolateImageFunction.
228  template <class TOtherImage> typename TOtherImage::VoxelType
229  Get(const TOtherImage *, double, double, double = 0, double = 0) const;
230 
231  /// Get value of given image at arbitrary location (in pixels)
232  ///
233  /// This function is used to only interpolate foreground image values.
234  /// If fully outside the foreground region, the _DefaultValue is returned.
235  ///
236  /// If the location is inside the finite domain of the image, an actual image
237  /// instance can be passed as first argument directly such as an instance of
238  /// GenericImage. Otherwise, an image function which extends the finite
239  /// image domain to an infinite lattice is needed, i.e., an instance of a
240  /// subclass of ExtrapolateImageFunction.
241  template <class TOtherImage, class TCoefficient> typename TCoefficient::VoxelType
242  GetWithPadding(const TOtherImage *, const TCoefficient *,
243  double, double, double = 0, double = 0) const;
244 
245  /// Evaluate generic image without handling boundary conditions
246  ///
247  /// This version is faster than EvaluateOutside, but is only defined inside
248  /// the domain for which all image values required for interpolation are
249  /// defined and thus require no extrapolation of the finite image.
250  virtual VoxelType GetInside(double, double, double = 0, double = 0) const;
251 
252  /// Evaluate generic image at an arbitrary location (in pixels)
253  virtual VoxelType GetOutside(double, double, double = 0, double = 0) const;
254 
255  /// Evaluate generic image without handling boundary conditions
256  ///
257  /// This function is used to only interpolate foreground image values.
258  /// If fully outside the foreground region, the _DefaultValue is returned.
259  ///
260  /// This version is faster than GetWithPaddingOutside, but is only defined
261  /// inside the domain for which all image values required for interpolation
262  /// are defined and thus require no extrapolation of the finite image.
263  virtual VoxelType GetWithPaddingInside(double, double, double = 0, double = 0) const;
264 
265  /// Evaluate generic image at an arbitrary location (in pixels)
266  ///
267  /// This function is used to only interpolate foreground image values.
268  /// If fully outside the foreground region, the _DefaultValue is returned.
269  virtual VoxelType GetWithPaddingOutside(double, double, double = 0, double = 0) const;
270 
271 };
272 
273 /**
274  * B-spline interpolation of any scalar image
275  */
276 
278 : public GenericBSplineInterpolateImageFunction<BaseImage>
279 {
280  mirtkObjectMacro(BSplineInterpolateImageFunction);
281 
282 public:
283 
284  /// Constructor
286  :
288  {}
289 
290 };
291 
292 
293 } // namespace mirtk
294 
295 #endif // MIRTK_BSplineInterpolateImageFunction_H
mirtkPublicAttributeMacro(int, SplineDegree)
Degree of B-spline.
VoxelType Get3D(double, double, double=0, double=0) const
VoxelType Get2D(double, double, double=0, double=0) const
VoxelType Get4D(double, double, double=0, double=0) const
Definition: IOConfig.h:41
VoxelType Get(double, double, double=0, double=0) const
virtual VoxelType GetWithPaddingOutside(double, double, double=0, double=0) const
mirtkComponentMacro(CoefficientExtrapolator, InfiniteCoefficient)
Infinite discrete coefficient image obtained by extrapolation.
VoxelType GetWithPadding4D(double, double, double=0, double=0) const
VoxelType GetWithPadding2D(double, double, double=0, double=0) const
virtual VoxelType GetOutside(double, double, double=0, double=0) const
Evaluate generic image at an arbitrary location (in pixels)
virtual VoxelType GetInside(double, double, double=0, double=0) const
virtual void BoundingInterval(double, int &, int &) const
VoxelType GetWithPadding(double, double, double=0, double=0) const
VoxelType GetWithPadding3D(double, double, double=0, double=0) const
mirtkAttributeMacro(bool, UseInputCoefficients)
Input image contains spline coefficients.
virtual VoxelType GetWithPaddingInside(double, double, double=0, double=0) const