Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add mode parameter to elastic_transform_2d (#883)

Constantin Pape77d16ea29613
diff --git a/kornia/geometry/transform/elastic_transform.py b/kornia/geometry/transform/elastic_transform.py
index 32b6dca4e..da97eeeeb 100644
--- a/kornia/geometry/transform/elastic_transform.py
+++ b/kornia/geometry/transform/elastic_transform.py
@@ -15,7 +15,8 @@ def elastic_transform2d(image: torch.Tensor,
kernel_size: Tuple[int, int] = (3, 3),
sigma: Tuple[float, float] = (4., 4.),
alpha: Tuple[float, float] = (32., 32.),
- align_corners: bool = False) -> torch.Tensor:
+ align_corners: bool = False,
+ mode: str = 'bilinear') -> torch.Tensor:
r"""Applies elastic transform of images as described in :cite:`Simard2003BestPF`.
Args:
@@ -29,6 +30,7 @@ def elastic_transform2d(image: torch.Tensor,
alpha (Tuple[float, float]): The scaling factor that controls the intensity of the deformation
in the y and x directions, respectively. Default: 32.
align_corners (bool): Interpolation flag used by `grid_sample`. Default: False.
+ mode (str): Interpolation mode used by `grid_sample`. Either 'bilinear' or 'nearest'. Default: 'bilinear'.
.. note:
`sigma` and `alpha` can also be a `torch.Tensor`. However, you could not torchscript
@@ -85,6 +87,6 @@ def elastic_transform2d(image: torch.Tensor,
b, c, h, w = image.shape
grid = kornia.utils.create_meshgrid(h, w, device=image.device).to(image.dtype)
warped = F.grid_sample(
- image, (grid + disp).clamp(-1, 1), align_corners=align_corners)
+ image, (grid + disp).clamp(-1, 1), align_corners=align_corners, mode=mode)
return warped