Resample an image to match given metadata. The affine matrix will be aligned, and the size of the output image will match. This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic ` for more information.
| 257 | |
| 258 | |
| 259 | class ResampleToMatch(SpatialResample): |
| 260 | """ |
| 261 | Resample an image to match given metadata. The affine matrix will be aligned, |
| 262 | and the size of the output image will match. |
| 263 | |
| 264 | This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>` |
| 265 | for more information. |
| 266 | """ |
| 267 | |
| 268 | def __call__( # type: ignore |
| 269 | self, |
| 270 | img: torch.Tensor, |
| 271 | img_dst: torch.Tensor, |
| 272 | mode: str | int | None = None, |
| 273 | padding_mode: str | None = None, |
| 274 | align_corners: bool | None = None, |
| 275 | dtype: DtypeLike = None, |
| 276 | lazy: bool | None = None, |
| 277 | ) -> torch.Tensor: |
| 278 | """ |
| 279 | Args: |
| 280 | img: input image to be resampled to match ``img_dst``. It currently supports channel-first arrays with |
| 281 | at most three spatial dimensions. |
| 282 | mode: {``"bilinear"``, ``"nearest"``} or spline interpolation order 0-5 (integers). |
| 283 | Interpolation mode to calculate output values. Defaults to ``"bilinear"``. |
| 284 | See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html |
| 285 | When it's an integer, the numpy (cpu tensor)/cupy (cuda tensor) backends will be used |
| 286 | and the value represents the order of the spline interpolation. |
| 287 | See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html |
| 288 | padding_mode: {``"zeros"``, ``"border"``, ``"reflection"``} |
| 289 | Padding mode for outside grid values. Defaults to ``"border"``. |
| 290 | See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html |
| 291 | When `mode` is an integer, using numpy/cupy backends, this argument accepts |
| 292 | {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}. |
| 293 | See also: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.map_coordinates.html |
| 294 | align_corners: Geometrically, we consider the pixels of the input as squares rather than points. |
| 295 | Defaults to ``None``, effectively using the value of `self.align_corners`. |
| 296 | See also: https://pytorch.org/docs/stable/generated/torch.nn.functional.grid_sample.html |
| 297 | dtype: data type for resampling computation. Defaults to ``self.dtype`` or |
| 298 | ``np.float64`` (for best precision). If ``None``, use the data type of input data. |
| 299 | To be compatible with other modules, the output data type is always `float32`. |
| 300 | lazy: a flag to indicate whether this transform should execute lazily or not |
| 301 | during this call. Setting this to False or True overrides the ``lazy`` flag set |
| 302 | during initialization for this call. Defaults to None. |
| 303 | |
| 304 | Raises: |
| 305 | ValueError: When the affine matrix of the source image is not invertible. |
| 306 | Returns: |
| 307 | Resampled input tensor or MetaTensor. |
| 308 | """ |
| 309 | if img_dst is None: |
| 310 | raise RuntimeError("`img_dst` is missing.") |
| 311 | dst_affine = img_dst.peek_pending_affine() if isinstance(img_dst, MetaTensor) else torch.eye(4) |
| 312 | lazy_ = self.lazy if lazy is None else lazy |
| 313 | img = super().__call__( |
| 314 | img=img, |
| 315 | dst_affine=dst_affine, |
| 316 | spatial_size=img_dst.peek_pending_shape() if isinstance(img_dst, MetaTensor) else img_dst.shape[1:], |
no outgoing calls
searching dependent graphs…