Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Merge ``homography_warp`` to ``warp_perspective`` (#1438)

Max Pakhomov1b91bc4d508e
diff --git a/docs/source/geometry.conversions.rst b/docs/source/geometry.conversions.rst
index 93fd76e12..e524f4fcd 100644
--- a/docs/source/geometry.conversions.rst
+++ b/docs/source/geometry.conversions.rst
@@ -10,6 +10,7 @@ Angles
.. autofunction:: deg2rad
.. autofunction:: pol2cart
.. autofunction:: cart2pol
+.. autofunction:: angle_to_rotation_matrix
Coordinates
-----------
@@ -22,6 +23,13 @@ Coordinates
.. autofunction:: denormalize_pixel_coordinates3d
.. autofunction:: normalize_pixel_coordinates3d
+Homography
+----------
+
+.. autofunction:: normalize_homography
+.. autofunction:: denormalize_homography
+.. autofunction:: normalize_homography3d
+
Quaternion
----------
diff --git a/docs/source/geometry.transform.rst b/docs/source/geometry.transform.rst
index 8c680eca4..b4e335ed6 100644
--- a/docs/source/geometry.transform.rst
+++ b/docs/source/geometry.transform.rst
@@ -49,8 +49,6 @@ Matrix transformations
.. autofunction:: invert_affine_transform
.. autofunction:: projection_from_Rt
.. autofunction:: get_tps_transform
-.. autofunction:: normalize_homography
-.. autofunction:: denormalize_homography
Crop operators
--------------
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 820d1ac04..44b9c7b9b 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -1,11 +1,12 @@
import enum
import warnings
-from typing import Tuple
+from typing import Optional, Tuple
import torch
import torch.nn.functional as F
from kornia.constants import pi
+from kornia.utils.helpers import _torch_inverse_cast
__all__ = [
"rad2deg",
@@ -30,6 +31,11 @@ __all__ = [
"denormalize_pixel_coordinates3d",
"normalize_pixel_coordinates3d",
"angle_to_rotation_matrix",
+ "normalize_homography",
+ "denormalize_homography",
+ "normalize_homography3d",
+ "normal_transform_pixel",
+ "normal_transform_pixel3d",
]
@@ -983,14 +989,10 @@ def angle_to_rotation_matrix(angle: torch.Tensor) -> torch.Tensor:
r"""Create a rotation matrix out of angles in degrees.
Args:
- angle: tensor of angles in degrees, any shape.
+ angle: tensor of angles in degrees, any shape :math:`(*)`.
Returns:
- tensor of *x2x2 rotation matrices.
-
- Shape:
- - Input: :math:`(*)`
- - Output: :math:`(*, 2, 2)`
+ tensor of rotation matrices with shape :math:`(*, 2, 2)`.
Example:
>>> input = torch.rand(1, 3) # Nx3
@@ -1000,3 +1002,174 @@ def angle_to_rotation_matrix(angle: torch.Tensor) -> torch.Tensor:
cos_a: torch.Tensor = torch.cos(ang_rad)
sin_a: torch.Tensor = torch.sin(ang_rad)
return torch.stack([cos_a, sin_a, -sin_a, cos_a], dim=-1).view(*angle.shape, 2, 2)
+
+
+def normalize_homography(
+ dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int], dsize_dst: Tuple[int, int]
+) -> torch.Tensor:
+ r"""Normalize a given homography in pixels to [-1, 1].
+
+ Args:
+ dst_pix_trans_src_pix: homography/ies from source to destination to be
+ normalized. :math:`(B, 3, 3)`
+ dsize_src: size of the source image (height, width).
+ dsize_dst: size of the destination image (height, width).
+
+ Returns:
+ the normalized homography of shape :math:`(B, 3, 3)`.
+ """
+ if not isinstance(dst_pix_trans_src_pix, torch.Tensor):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(dst_pix_trans_src_pix)}")
+
+ if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (3, 3)):
+ raise ValueError(f"Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {dst_pix_trans_src_pix.shape}")
+
+ # source and destination sizes
+ src_h, src_w = dsize_src
+ dst_h, dst_w = dsize_dst
+
+ # compute the transformation pixel/norm for src/dst
+ src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)
+
+ src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix)
+ dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)
+
+ # compute chain transformations
+ dst_norm_trans_src_norm: torch.Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
+ return dst_norm_trans_src_norm
+
+
+def normal_transform_pixel(
+ height: int,
+ width: int,
+ eps: float = 1e-14,
+ device: Optional[torch.device] = None,
+ dtype: Optional[torch.dtype] = None,
+) -> torch.Tensor:
+ r"""Compute the normalization matrix from image size in pixels to [-1, 1].
+
+ Args:
+ height image height.
+ width: image width.
+ eps: epsilon to prevent divide-by-zero errors
+
+ Returns:
+ normalized transform with shape :math:`(1, 3, 3)`.
+ """
+ tr_mat = torch.tensor([[1.0, 0.0, -1.0], [0.0, 1.0, -1.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype) # 3x3
+
+ # prevent divide by zero bugs
+ width_denom: float = eps if width == 1 else width - 1.0
+ height_denom: float = eps if height == 1 else height - 1.0
+
+ tr_mat[0, 0] = tr_mat[0, 0] * 2.0 / width_denom
+ tr_mat[1, 1] = tr_mat[1, 1] * 2.0 / height_denom
+
+ return tr_mat.unsqueeze(0) # 1x3x3
+
+
+def normal_transform_pixel3d(
+ depth: int,
+ height: int,
+ width: int,
+ eps: float = 1e-14,
+ device: Optional[torch.device] = None,
+ dtype: Optional[torch.dtype] = None,
+) -> torch.Tensor:
+ r"""Compute the normalization matrix from image size in pixels to [-1, 1].
+
+ Args:
+ depth: image depth.
+ height: image height.
+ width: image width.
+ eps: epsilon to prevent divide-by-zero errors
+
+ Returns:
+ normalized transform with shape :math:`(1, 4, 4)`.
+ """
+ tr_mat = torch.tensor(
+ [[1.0, 0.0, 0.0, -1.0], [0.0, 1.0, 0.0, -1.0], [0.0, 0.0, 1.0, -1.0], [0.0, 0.0, 0.0, 1.0]],
+ device=device,
+ dtype=dtype,
+ ) # 4x4
+
+ # prevent divide by zero bugs
+ width_denom: float = eps if width == 1 else width - 1.0
+ height_denom: float = eps if height == 1 else height - 1.0
+ depth_denom: float = eps if depth == 1 else depth - 1.0
+
+ tr_mat[0, 0] = tr_mat[0, 0] * 2.0 / width_denom
+ tr_mat[1, 1] = tr_mat[1, 1] * 2.0 / height_denom
+ tr_mat[2, 2] = tr_mat[2, 2] * 2.0 / depth_denom
+
+ return tr_mat.unsqueeze(0) # 1x4x4
+
+
+def denormalize_homography(
+ dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int], dsize_dst: Tuple[int, int]
+) -> torch.Tensor:
+ r"""De-normalize a given homography in pixels from [-1, 1] to actual height and width.
+
+ Args:
+ dst_pix_trans_src_pix: homography/ies from source to destination to be
+ denormalized. :math:`(B, 3, 3)`
+ dsize_src: size of the source image (height, width).
+ dsize_dst: size of the destination image (height, width).
+
+ Returns:
+ the denormalized homography of shape :math:`(B, 3, 3)`.
+ """
+ if not isinstance(dst_pix_trans_src_pix, torch.Tensor):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(dst_pix_trans_src_pix)}")
+
+ if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (3, 3)):
+ raise ValueError(f"Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {dst_pix_trans_src_pix.shape}")
+
+ # source and destination sizes
+ src_h, src_w = dsize_src
+ dst_h, dst_w = dsize_dst
+
+ # compute the transformation pixel/norm for src/dst
+ src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)
+
+ dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)
+ dst_denorm_trans_dst_pix = _torch_inverse_cast(dst_norm_trans_dst_pix)
+ # compute chain transformations
+ dst_norm_trans_src_norm: torch.Tensor = dst_denorm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_norm_trans_src_pix)
+ return dst_norm_trans_src_norm
+
+
+def normalize_homography3d(
+ dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int, int], dsize_dst: Tuple[int, int, int]
+) -> torch.Tensor:
+ r"""Normalize a given homography in pixels to [-1, 1].
+
+ Args:
+ dst_pix_trans_src_pix: homography/ies from source to destination to be
+ normalized. :math:`(B, 4, 4)`
+ dsize_src: size of the source image (depth, height, width).
+ dsize_src: size of the destination image (depth, height, width).
+
+ Returns:
+ the normalized homography.
+
+ Shape:
+ Output: :math:`(B, 4, 4)`
+ """
+ if not isinstance(dst_pix_trans_src_pix, torch.Tensor):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(dst_pix_trans_src_pix)}")
+
+ if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (4, 4)):
+ raise ValueError(f"Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {dst_pix_trans_src_pix.shape}")
+
+ # source and destination sizes
+ src_d, src_h, src_w = dsize_src
+ dst_d, dst_h, dst_w = dsize_dst
+ # compute the transformation pixel/norm for src/dst
+ src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel3d(src_d, src_h, src_w).to(dst_pix_trans_src_pix)
+
+ src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix)
+ dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel3d(dst_d, dst_h, dst_w).to(dst_pix_trans_src_pix)
+ # compute chain transformations
+ dst_norm_trans_src_norm: torch.Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
+ return dst_norm_trans_src_norm
diff --git a/kornia/geometry/transform/__init__.py b/kornia/geometry/transform/__init__.py
index f853e1842..7ed467a93 100644
--- a/kornia/geometry/transform/__init__.py
+++ b/kornia/geometry/transform/__init__.py
@@ -6,6 +6,5 @@ from .flips import *
from .homography_warper import *
from .image_registrator import *
from .imgwarp import *
-from .projwarp import *
from .pyramid import *
from .thin_plate_spline import *
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index 7390b438b..8fcad47d8 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -7,8 +7,7 @@ from kornia.filters import gaussian_blur2d
from kornia.utils import _extract_device_dtype
from kornia.utils.image import perform_keep_shape_image
-from .imgwarp import get_affine_matrix2d, get_rotation_matrix2d, warp_affine
-from .projwarp import get_projective_transform, warp_affine3d
+from .imgwarp import get_affine_matrix2d, get_projective_transform, get_rotation_matrix2d, warp_affine, warp_affine3d
__all__ = [
"affine",
diff --git a/kornia/geometry/transform/crop3d.py b/kornia/geometry/transform/crop3d.py
index 1a3ff7e32..3f499768e 100644
--- a/kornia/geometry/transform/crop3d.py
+++ b/kornia/geometry/transform/crop3d.py
@@ -4,7 +4,7 @@ import torch
from kornia.geometry.bbox import infer_bbox_shape3d, validate_bbox3d
-from .projwarp import get_perspective_transform3d, warp_affine3d
+from .imgwarp import get_perspective_transform3d, warp_affine3d
__all__ = [
"crop_and_resize3d",
diff --git a/kornia/geometry/transform/homography_warper.py b/kornia/geometry/transform/homography_warper.py
index 273d61346..50e070140 100644
--- a/kornia/geometry/transform/homography_warper.py
+++ b/kornia/geometry/transform/homography_warper.py
@@ -1,170 +1,18 @@
-from typing import Optional, Tuple
+from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
-from kornia.geometry.linalg import transform_points
-from kornia.testing import check_is_tensor
-from kornia.utils import create_meshgrid, create_meshgrid3d
-from kornia.utils.helpers import _torch_inverse_cast
+from kornia.utils import create_meshgrid
+
+from .imgwarp import homography_warp, warp_grid
__all__ = [
"HomographyWarper",
- "homography_warp",
- "homography_warp3d",
- "warp_grid",
- "warp_grid3d",
- "normalize_homography",
- "denormalize_homography",
- "normalize_homography3d",
- "normal_transform_pixel",
- "normal_transform_pixel3d",
]
-def warp_grid(grid: torch.Tensor, src_homo_dst: torch.Tensor) -> torch.Tensor:
- r"""Compute the grid to warp the coordinates grid by the homography/ies.
-
- Args:
- grid: Unwrapped grid of the shape :math:`(1, N, W, 2)`.
- src_homo_dst: Homography or homographies (stacked) to
- transform all points in the grid. Shape of the homography
- has to be :math:`(1, 3, 3)` or :math:`(N, 1, 3, 3)`.
-
- Returns:
- the transformed grid of shape :math:`(N, H, W, 2)`.
- """
- batch_size: int = src_homo_dst.size(0)
- _, height, width, _ = grid.size()
- # expand grid to match the input batch size
- grid = grid.expand(batch_size, -1, -1, -1) # NxHxWx2
- if len(src_homo_dst.shape) == 3: # local homography case
- src_homo_dst = src_homo_dst.view(batch_size, 1, 3, 3) # Nx1x3x3
- # perform the actual grid transformation,
- # the grid is copied to input device and casted to the same type
- flow: torch.Tensor = transform_points(src_homo_dst, grid.to(src_homo_dst)) # NxHxWx2
- return flow.view(batch_size, height, width, 2) # NxHxWx2
-
-
-def warp_grid3d(grid: torch.Tensor, src_homo_dst: torch.Tensor) -> torch.Tensor:
- r"""Compute the grid to warp the coordinates grid by the homography/ies.
-
- Args:
- grid: Unwrapped grid of the shape :math:`(1, D, H, W, 3)`.
- src_homo_dst: Homography or homographies (stacked) to
- transform all points in the grid. Shape of the homography
- has to be :math:`(1, 4, 4)` or :math:`(N, 1, 4, 4)`.
-
- Returns:
- the transformed grid of shape :math:`(N, H, W, 3)`.
- """
- batch_size: int = src_homo_dst.size(0)
- _, depth, height, width, _ = grid.size()
- # expand grid to match the input batch size
- grid = grid.expand(batch_size, -1, -1, -1, -1) # NxDxHxWx3
- if len(src_homo_dst.shape) == 3: # local homography case
- src_homo_dst = src_homo_dst.view(batch_size, 1, 4, 4) # Nx1x3x3
- # perform the actual grid transformation,
- # the grid is copied to input device and casted to the same type
- flow: torch.Tensor = transform_points(src_homo_dst, grid.to(src_homo_dst)) # NxDxHxWx3
- return flow.view(batch_size, depth, height, width, 3) # NxDxHxWx3
-
-
-# functional api
-def homography_warp(
- patch_src: torch.Tensor,
- src_homo_dst: torch.Tensor,
- dsize: Tuple[int, int],
- mode: str = 'bilinear',
- padding_mode: str = 'zeros',
- align_corners: bool = False,
- normalized_coordinates: bool = True,
-) -> torch.Tensor:
- r"""Warp image patches or tensors by normalized 2D homographies.
-
- See :class:`~kornia.geometry.warp.HomographyWarper` for details.
-
- Args:
- patch_src: The image or tensor to warp. Should be from source of shape :math:`(N, C, H, W)`.
- src_homo_dst: The homography or stack of homographies from destination to source of shape
- :math:`(N, 3, 3)`.
- dsize: The height and width of the image to warp.
- mode: interpolation mode to calculate output values ``'bilinear'`` | ``'nearest'``.
- padding_mode: padding mode for outside grid values ``'zeros'`` | ``'border'`` | ``'reflection'``.
- align_corners: interpolation flag.
- normalized_coordinates: Whether the homography assumes [-1, 1] normalized coordinates or not.
-
- Return:
- Patch sampled at locations from source to destination.
-
- Example:
- >>> input = torch.rand(1, 3, 32, 32)
- >>> homography = torch.eye(3).view(1, 3, 3)
- >>> output = homography_warp(input, homography, (32, 32))
- """
- if not src_homo_dst.device == patch_src.device:
- raise TypeError(
- "Patch and homography must be on the same device. \
- Got patch.device: {} src_H_dst.device: {}.".format(
- patch_src.device, src_homo_dst.device
- )
- )
-
- height, width = dsize
- grid = create_meshgrid(height, width, normalized_coordinates=normalized_coordinates)
- warped_grid = warp_grid(grid, src_homo_dst)
-
- return F.grid_sample(patch_src, warped_grid, mode=mode, padding_mode=padding_mode, align_corners=align_corners)
-
-
-def homography_warp3d(
- patch_src: torch.Tensor,
- src_homo_dst: torch.Tensor,
- dsize: Tuple[int, int, int],
- mode: str = 'bilinear',
- padding_mode: str = 'zeros',
- align_corners: bool = False,
- normalized_coordinates: bool = True,
-) -> torch.Tensor:
- r"""Warp image patches or tensors by normalized 3D homographies.
-
- Args:
- patch_src: The image or tensor to warp. Should be from source of shape :math:`(N, C, D, H, W)`.
- src_homo_dst: The homography or stack of homographies from destination to source of shape
- :math:`(N, 4, 4)`.
- dsize: The height and width of the image to warp.
- mode: interpolation mode to calculate output values ``'bilinear'`` | ``'nearest'``.
- padding_mode: padding mode for outside grid values ``'zeros'`` | ``'border'`` | ``'reflection'``.
- align_corners: interpolation flag.
- normalized_coordinates: Whether the homography assumes [-1, 1] normalized coordinates or not.
-
- Return:
- Patch sampled at locations from source to destination.
-
- Example:
- >>> input = torch.rand(1, 3, 32, 32)
- >>> homography = torch.eye(3).view(1, 3, 3)
- >>> output = homography_warp(input, homography, (32, 32))
- """
- if not src_homo_dst.device == patch_src.device:
- raise TypeError(
- "Patch and homography must be on the same device. \
- Got patch.device: {} src_H_dst.device: {}.".format(
- patch_src.device, src_homo_dst.device
- )
- )
-
- depth, height, width = dsize
- grid = create_meshgrid3d(
- depth, height, width, normalized_coordinates=normalized_coordinates, device=patch_src.device
- )
- warped_grid = warp_grid3d(grid, src_homo_dst)
-
- return F.grid_sample(patch_src, warped_grid, mode=mode, padding_mode=padding_mode, align_corners=align_corners)
-
-
-# layer api
class HomographyWarper(nn.Module):
r"""Warp tensors by homographies.
@@ -281,171 +129,3 @@ class HomographyWarper(nn.Module):
)
return warped_patch
-
-
-def normal_transform_pixel(
- height: int,
- width: int,
- eps: float = 1e-14,
- device: Optional[torch.device] = None,
- dtype: Optional[torch.dtype] = None,
-) -> torch.Tensor:
- r"""Compute the normalization matrix from image size in pixels to [-1, 1].
-
- Args:
- height image height.
- width: image width.
- eps: epsilon to prevent divide-by-zero errors
-
- Returns:
- normalized transform with shape :math:`(1, 3, 3)`.
- """
- tr_mat = torch.tensor([[1.0, 0.0, -1.0], [0.0, 1.0, -1.0], [0.0, 0.0, 1.0]], device=device, dtype=dtype) # 3x3
-
- # prevent divide by zero bugs
- width_denom: float = eps if width == 1 else width - 1.0
- height_denom: float = eps if height == 1 else height - 1.0
-
- tr_mat[0, 0] = tr_mat[0, 0] * 2.0 / width_denom
- tr_mat[1, 1] = tr_mat[1, 1] * 2.0 / height_denom
-
- return tr_mat.unsqueeze(0) # 1x3x3
-
-
-def normal_transform_pixel3d(
- depth: int,
- height: int,
- width: int,
- eps: float = 1e-14,
- device: Optional[torch.device] = None,
- dtype: Optional[torch.dtype] = None,
-) -> torch.Tensor:
- r"""Compute the normalization matrix from image size in pixels to [-1, 1].
-
- Args:
- depth: image depth.
- height: image height.
- width: image width.
- eps: epsilon to prevent divide-by-zero errors
-
- Returns:
- normalized transform with shape :math:`(1, 4, 4)`.
- """
- tr_mat = torch.tensor(
- [[1.0, 0.0, 0.0, -1.0], [0.0, 1.0, 0.0, -1.0], [0.0, 0.0, 1.0, -1.0], [0.0, 0.0, 0.0, 1.0]],
- device=device,
- dtype=dtype,
- ) # 4x4
-
- # prevent divide by zero bugs
- width_denom: float = eps if width == 1 else width - 1.0
- height_denom: float = eps if height == 1 else height - 1.0
- depth_denom: float = eps if depth == 1 else depth - 1.0
-
- tr_mat[0, 0] = tr_mat[0, 0] * 2.0 / width_denom
- tr_mat[1, 1] = tr_mat[1, 1] * 2.0 / height_denom
- tr_mat[2, 2] = tr_mat[2, 2] * 2.0 / depth_denom
-
- return tr_mat.unsqueeze(0) # 1x4x4
-
-
-def normalize_homography(
- dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int], dsize_dst: Tuple[int, int]
-) -> torch.Tensor:
- r"""Normalize a given homography in pixels to [-1, 1].
-
- Args:
- dst_pix_trans_src_pix: homography/ies from source to destination to be
- normalized. :math:`(B, 3, 3)`
- dsize_src: size of the source image (height, width).
- dsize_dst: size of the destination image (height, width).
-
- Returns:
- the normalized homography of shape :math:`(B, 3, 3)`.
- """
- check_is_tensor(dst_pix_trans_src_pix)
-
- if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (3, 3)):
- raise ValueError(f"Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {dst_pix_trans_src_pix.shape}")
-
- # source and destination sizes
- src_h, src_w = dsize_src
- dst_h, dst_w = dsize_dst
-
- # compute the transformation pixel/norm for src/dst
- src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)
-
- src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix)
- dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)
-
- # compute chain transformations
- dst_norm_trans_src_norm: torch.Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
- return dst_norm_trans_src_norm
-
-
-def denormalize_homography(
- dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int], dsize_dst: Tuple[int, int]
-) -> torch.Tensor:
- r"""De-normalize a given homography in pixels from [-1, 1] to actual height and width.
-
- Args:
- dst_pix_trans_src_pix: homography/ies from source to destination to be
- denormalized. :math:`(B, 3, 3)`
- dsize_src: size of the source image (height, width).
- dsize_dst: size of the destination image (height, width).
-
- Returns:
- the denormalized homography of shape :math:`(B, 3, 3)`.
- """
- check_is_tensor(dst_pix_trans_src_pix)
-
- if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (3, 3)):
- raise ValueError(f"Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {dst_pix_trans_src_pix.shape}")
-
- # source and destination sizes
- src_h, src_w = dsize_src
- dst_h, dst_w = dsize_dst
-
- # compute the transformation pixel/norm for src/dst
- src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w).to(dst_pix_trans_src_pix)
-
- dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w).to(dst_pix_trans_src_pix)
- dst_denorm_trans_dst_pix = _torch_inverse_cast(dst_norm_trans_dst_pix)
- # compute chain transformations
- dst_norm_trans_src_norm: torch.Tensor = dst_denorm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_norm_trans_src_pix)
- return dst_norm_trans_src_norm
-
-
-def normalize_homography3d(
- dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int, int], dsize_dst: Tuple[int, int, int]
-) -> torch.Tensor:
- r"""Normalize a given homography in pixels to [-1, 1].
-
- Args:
- dst_pix_trans_src_pix: homography/ies from source to destination to be
- normalized. :math:`(B, 4, 4)`
- dsize_src: size of the source image (depth, height, width).
- dsize_src: size of the destination image (depth, height, width).
-
- Returns:
- the normalized homography.
-
- Shape:
- Output: :math:`(B, 4, 4)`
- """
- check_is_tensor(dst_pix_trans_src_pix)
-
- if not (len(dst_pix_trans_src_pix.shape) == 3 or dst_pix_trans_src_pix.shape[-2:] == (4, 4)):
- raise ValueError(f"Input dst_pix_trans_src_pix must be a Bx3x3 tensor. Got {dst_pix_trans_src_pix.shape}")
-
- # source and destination sizes
- src_d, src_h, src_w = dsize_src
- dst_d, dst_h, dst_w = dsize_dst
- # compute the transformation pixel/norm for src/dst
- src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel3d(src_d, src_h, src_w).to(dst_pix_trans_src_pix)
-
- src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix)
- dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel3d(dst_d, dst_h, dst_w).to(dst_pix_trans_src_pix)
- # compute chain transformations
- dst_norm_trans_src_norm: torch.Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
- return dst_norm_trans_src_norm
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index aec29d2e0..61dea7b38 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -1,21 +1,22 @@
-from typing import Optional, Tuple
+from typing import List, Optional, Tuple
import torch
import torch.nn.functional as F
from kornia.geometry.conversions import (
+ angle_axis_to_rotation_matrix,
angle_to_rotation_matrix,
convert_affinematrix_to_homography,
convert_affinematrix_to_homography3d,
+ deg2rad,
+ normalize_homography,
+ normalize_homography3d,
normalize_pixel_coordinates,
)
from kornia.geometry.linalg import transform_points
-from kornia.utils import create_meshgrid, eye_like
+from kornia.utils import create_meshgrid, create_meshgrid3d, eye_like
from kornia.utils.helpers import _torch_inverse_cast, _torch_solve_cast
-from .homography_warper import normalize_homography
-from .projwarp import get_projective_transform
-
__all__ = [
"warp_perspective",
"warp_affine",
@@ -27,6 +28,15 @@ __all__ = [
"get_affine_matrix3d",
"get_shear_matrix2d",
"get_shear_matrix3d",
+ "warp_affine3d",
+ "get_projective_transform",
+ "projection_from_Rt",
+ "get_perspective_transform3d",
+ "warp_perspective3d",
+ "warp_grid",
+ "warp_grid3d",
+ "homography_warp",
+ "homography_warp3d",
]
@@ -220,6 +230,54 @@ def _fill_and_warp(
return F.grid_sample(src, grid, align_corners=align_corners, mode=mode, padding_mode="zeros") + inv_color_mask
+def warp_grid(grid: torch.Tensor, src_homo_dst: torch.Tensor) -> torch.Tensor:
+ r"""Compute the grid to warp the coordinates grid by the homography/ies.
+
+ Args:
+ grid: Unwrapped grid of the shape :math:`(1, N, W, 2)`.
+ src_homo_dst: Homography or homographies (stacked) to
+ transform all points in the grid. Shape of the homography
+ has to be :math:`(1, 3, 3)` or :math:`(N, 1, 3, 3)`.
+
+ Returns:
+ the transformed grid of shape :math:`(N, H, W, 2)`.
+ """
+ batch_size: int = src_homo_dst.size(0)
+ _, height, width, _ = grid.size()
+ # expand grid to match the input batch size
+ grid = grid.expand(batch_size, -1, -1, -1) # NxHxWx2
+ if len(src_homo_dst.shape) == 3: # local homography case
+ src_homo_dst = src_homo_dst.view(batch_size, 1, 3, 3) # Nx1x3x3
+ # perform the actual grid transformation,
+ # the grid is copied to input device and casted to the same type
+ flow: torch.Tensor = transform_points(src_homo_dst, grid.to(src_homo_dst)) # NxHxWx2
+ return flow.view(batch_size, height, width, 2) # NxHxWx2
+
+
+def warp_grid3d(grid: torch.Tensor, src_homo_dst: torch.Tensor) -> torch.Tensor:
+ r"""Compute the grid to warp the coordinates grid by the homography/ies.
+
+ Args:
+ grid: Unwrapped grid of the shape :math:`(1, D, H, W, 3)`.
+ src_homo_dst: Homography or homographies (stacked) to
+ transform all points in the grid. Shape of the homography
+ has to be :math:`(1, 4, 4)` or :math:`(N, 1, 4, 4)`.
+
+ Returns:
+ the transformed grid of shape :math:`(N, H, W, 3)`.
+ """
+ batch_size: int = src_homo_dst.size(0)
+ _, depth, height, width, _ = grid.size()
+ # expand grid to match the input batch size
+ grid = grid.expand(batch_size, -1, -1, -1, -1) # NxDxHxWx3
+ if len(src_homo_dst.shape) == 3: # local homography case
+ src_homo_dst = src_homo_dst.view(batch_size, 1, 4, 4) # Nx1x3x3
+ # perform the actual grid transformation,
+ # the grid is copied to input device and casted to the same type
+ flow: torch.Tensor = transform_points(src_homo_dst, grid.to(src_homo_dst)) # NxDxHxWx3
+ return flow.view(batch_size, depth, height, width, 3) # NxDxHxWx3
+
+
def get_perspective_transform(src, dst):
r"""Calculate a perspective transform from four pairs of the corresponding
points.
@@ -779,3 +837,524 @@ def _compute_shear_matrix_3d(sxy_tan, sxz_tan, syx_tan, syz_tan, szx_tan, szy_ta
m12 = sxy_tan * szx_tan + szy_tan * m11
m22 = sxz_tan * szx_tan + szy_tan * m21 + ones
return m00, m10, m20, m01, m11, m21, m02, m12, m22
+
+
+def warp_affine3d(
+ src: torch.Tensor,
+ M: torch.Tensor,
+ dsize: Tuple[int, int, int],
+ flags: str = 'bilinear',
+ padding_mode: str = 'zeros',
+ align_corners: bool = True,
+) -> torch.Tensor:
+ r"""Apply a projective transformation a to 3d tensor.
+
+ .. warning::
+ This API signature it is experimental and might suffer some changes in the future.
+
+ Args:
+ src : input tensor of shape :math:`(B, C, D, H, W)`.
+ M: projective transformation matrix of shape :math:`(B, 3, 4)`.
+ dsize: size of the output image (depth, height, width).
+ mode: interpolation mode to calculate output values
+ ``'bilinear'`` | ``'nearest'``.
+ padding_mode: padding mode for outside grid values
+ ``'zeros'`` | ``'border'`` | ``'reflection'``.
+ align_corners : mode for grid_generation.
+
+ Returns:
+ torch.Tensor: the warped 3d tensor with shape :math:`(B, C, D, H, W)`.
+
+ .. note::
+ This function is often used in conjunction with :func:`get_perspective_transform3d`.
+ """
+ if len(src.shape) != 5:
+ raise AssertionError(src.shape)
+ if not (len(M.shape) == 3 and M.shape[-2:] == (3, 4)):
+ raise AssertionError(M.shape)
+ if len(dsize) != 3:
+ raise AssertionError(dsize)
+ B, C, D, H, W = src.size()
+
+ size_src: Tuple[int, int, int] = (D, H, W)
+ size_out: Tuple[int, int, int] = dsize
+
+ M_4x4 = convert_affinematrix_to_homography3d(M) # Bx4x4
+
+ # we need to normalize the transformation since grid sample needs -1/1 coordinates
+ dst_norm_trans_src_norm: torch.Tensor = normalize_homography3d(M_4x4, size_src, size_out) # Bx4x4
+
+ src_norm_trans_dst_norm = _torch_inverse_cast(dst_norm_trans_src_norm)
+ P_norm: torch.Tensor = src_norm_trans_dst_norm[:, :3] # Bx3x4
+
+ # compute meshgrid and apply to input
+ dsize_out: List[int] = [B, C] + list(size_out)
+ grid = torch.nn.functional.affine_grid(P_norm, dsize_out, align_corners=align_corners)
+ return torch.nn.functional.grid_sample(
+ src, grid, align_corners=align_corners, mode=flags, padding_mode=padding_mode
+ )
+
+
+def projection_from_Rt(rmat: torch.Tensor, tvec: torch.Tensor) -> torch.Tensor:
+ r"""Compute the projection matrix from Rotation and translation.
+
+ .. warning::
+ This API signature it is experimental and might suffer some changes in the future.
+
+ Concatenates the batch of rotations and translations such that :math:`P = [R | t]`.
+
+ Args:
+ rmat: the rotation matrix with shape :math:`(*, 3, 3)`.
+ tvec: the translation vector with shape :math:`(*, 3, 1)`.
+
+ Returns:
+ the projection matrix with shape :math:`(*, 3, 4)`.
+
+ """
+ if not (len(rmat.shape) >= 2 and rmat.shape[-2:] == (3, 3)):
+ raise AssertionError(rmat.shape)
+ if not (len(tvec.shape) >= 2 and tvec.shape[-2:] == (3, 1)):
+ raise AssertionError(tvec.shape)
+
+ return torch.cat([rmat, tvec], dim=-1) # Bx3x4
+
+
+def get_projective_transform(center: torch.Tensor, angles: torch.Tensor, scales: torch.Tensor) -> torch.Tensor:
+ r"""Calculate the projection matrix for a 3D rotation.
+
+ .. warning::
+ This API signature it is experimental and might suffer some changes in the future.
+
+ The function computes the projection matrix given the center and angles per axis.
+
+ Args:
+ center: center of the rotation (x,y,z) in the source with shape :math:`(B, 3)`.
+ angles: angle axis vector containing the rotation angles in degrees in the form
+ of (rx, ry, rz) with shape :math:`(B, 3)`. Internally it calls Rodrigues to compute
+ the rotation matrix from axis-angle.
+ scales: scale factor for x-y-z-directions with shape :math:`(B, 3)`.
+
+ Returns:
+ the projection matrix of 3D rotation with shape :math:`(B, 3, 4)`.
+
+ .. note::
+ This function is often used in conjunction with :func:`warp_affine3d`.
+ """
+ if not (len(center.shape) == 2 and center.shape[-1] == 3):
+ raise AssertionError(center.shape)
+ if not (len(angles.shape) == 2 and angles.shape[-1] == 3):
+ raise AssertionError(angles.shape)
+ if center.device != angles.device:
+ raise AssertionError(center.device, angles.device)
+ if center.dtype != angles.dtype:
+ raise AssertionError(center.dtype, angles.dtype)
+
+ # create rotation matrix
+ angle_axis_rad: torch.Tensor = deg2rad(angles)
+ rmat: torch.Tensor = angle_axis_to_rotation_matrix(angle_axis_rad) # Bx3x3
+ scaling_matrix: torch.Tensor = eye_like(3, rmat)
+ scaling_matrix = scaling_matrix * scales.unsqueeze(dim=1)
+ rmat = rmat @ scaling_matrix.to(rmat)
+
+ # define matrix to move forth and back to origin
+ from_origin_mat = torch.eye(4)[None].repeat(rmat.shape[0], 1, 1).type_as(center) # Bx4x4
+ from_origin_mat[..., :3, -1] += center
+
+ to_origin_mat = from_origin_mat.clone()
+ to_origin_mat = _torch_inverse_cast(from_origin_mat)
+
+ # append translation with zeros
+ proj_mat = projection_from_Rt(rmat, torch.zeros_like(center)[..., None]) # Bx3x4
+
+ # chain 4x4 transforms
+ proj_mat = convert_affinematrix_to_homography3d(proj_mat) # Bx4x4
+ proj_mat = from_origin_mat @ proj_mat @ to_origin_mat
+
+ return proj_mat[..., :3, :] # Bx3x4
+
+
+def get_perspective_transform3d(src: torch.Tensor, dst: torch.Tensor) -> torch.Tensor:
+ r"""Calculate a 3d perspective transform from four pairs of the corresponding points.
+
+ The function calculates the matrix of a perspective transform so that:
+
+ .. math ::
+
+ \begin{bmatrix}
+ t_{i}x_{i}^{'} \\
+ t_{i}y_{i}^{'} \\
+ t_{i}z_{i}^{'} \\
+ t_{i} \\
+ \end{bmatrix}
+ =
+ \textbf{map_matrix} \cdot
+ \begin{bmatrix}
+ x_{i} \\
+ y_{i} \\
+ z_{i} \\
+ 1 \\
+ \end{bmatrix}
+
+ where
+
+ .. math ::
+ dst(i) = (x_{i}^{'},y_{i}^{'},z_{i}^{'}), src(i) = (x_{i}, y_{i}, z_{i}), i = 0,1,2,5,7
+
+ Concrete math is as below:
+
+ .. math ::
+
+ \[ u_i =\frac{c_{00} * x_i + c_{01} * y_i + c_{02} * z_i + c_{03}}
+ {c_{30} * x_i + c_{31} * y_i + c_{32} * z_i + c_{33}} \]
+ \[ v_i =\frac{c_{10} * x_i + c_{11} * y_i + c_{12} * z_i + c_{13}}
+ {c_{30} * x_i + c_{31} * y_i + c_{32} * z_i + c_{33}} \]
+ \[ w_i =\frac{c_{20} * x_i + c_{21} * y_i + c_{22} * z_i + c_{23}}
+ {c_{30} * x_i + c_{31} * y_i + c_{32} * z_i + c_{33}} \]
+
+ .. math ::
+
+ \begin{pmatrix}
+ x_0 & y_0 & z_0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_0*u_0 & -y_0*u_0 & -z_0 * u_0 \\
+ x_1 & y_1 & z_1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_1*u_1 & -y_1*u_1 & -z_1 * u_1 \\
+ x_2 & y_2 & z_2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_2*u_2 & -y_2*u_2 & -z_2 * u_2 \\
+ x_5 & y_5 & z_5 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_5*u_5 & -y_5*u_5 & -z_5 * u_5 \\
+ x_7 & y_7 & z_7 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_7*u_7 & -y_7*u_7 & -z_7 * u_7 \\
+ 0 & 0 & 0 & 0 & x_0 & y_0 & z_0 & 1 & 0 & 0 & 0 & 0 & -x_0*v_0 & -y_0*v_0 & -z_0 * v_0 \\
+ 0 & 0 & 0 & 0 & x_1 & y_1 & z_1 & 1 & 0 & 0 & 0 & 0 & -x_1*v_1 & -y_1*v_1 & -z_1 * v_1 \\
+ 0 & 0 & 0 & 0 & x_2 & y_2 & z_2 & 1 & 0 & 0 & 0 & 0 & -x_2*v_2 & -y_2*v_2 & -z_2 * v_2 \\
+ 0 & 0 & 0 & 0 & x_5 & y_5 & z_5 & 1 & 0 & 0 & 0 & 0 & -x_5*v_5 & -y_5*v_5 & -z_5 * v_5 \\
+ 0 & 0 & 0 & 0 & x_7 & y_7 & z_7 & 1 & 0 & 0 & 0 & 0 & -x_7*v_7 & -y_7*v_7 & -z_7 * v_7 \\
+ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_0 & y_0 & z_0 & 1 & -x_0*w_0 & -y_0*w_0 & -z_0 * w_0 \\
+ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_1 & y_1 & z_1 & 1 & -x_1*w_1 & -y_1*w_1 & -z_1 * w_1 \\
+ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_2 & y_2 & z_2 & 1 & -x_2*w_2 & -y_2*w_2 & -z_2 * w_2 \\
+ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_5 & y_5 & z_5 & 1 & -x_5*w_5 & -y_5*w_5 & -z_5 * w_5 \\
+ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_7 & y_7 & z_7 & 1 & -x_7*w_7 & -y_7*w_7 & -z_7 * w_7 \\
+ \end{pmatrix}
+
+ Args:
+ src: coordinates of quadrangle vertices in the source image with shape :math:`(B, 8, 3)`.
+ dst: coordinates of the corresponding quadrangle vertices in
+ the destination image with shape :math:`(B, 8, 3)`.
+
+ Returns:
+ the perspective transformation with shape :math:`(B, 4, 4)`.
+
+ .. note::
+ This function is often used in conjunction with :func:`warp_perspective3d`.
+ """
+ if not isinstance(src, (torch.Tensor)):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(src)}")
+
+ if not isinstance(dst, (torch.Tensor)):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(dst)}")
+
+ if not src.shape[-2:] == (8, 3):
+ raise ValueError(f"Inputs must be a Bx8x3 tensor. Got {src.shape}")
+
+ if not src.shape == dst.shape:
+ raise ValueError(f"Inputs must have the same shape. Got {dst.shape}")
+
+ if not (src.shape[0] == dst.shape[0]):
+ raise ValueError(f"Inputs must have same batch size dimension. Expect {src.shape} but got {dst.shape}")
+
+ if not (src.device == dst.device and src.dtype == dst.dtype):
+ raise AssertionError(
+ f"Expect `src` and `dst` to be in the same device (Got {src.dtype}, {dst.dtype}) "
+ f"with the same dtype (Got {src.dtype}, {dst.dtype})."
+ )
+
+ # we build matrix A by using only 4 point correspondence. The linear
+ # system is solved with the least square method, so here
+ # we could even pass more correspondence
+ p = []
+
+ # 000, 100, 110, 101, 011
+ for i in [0, 1, 2, 5, 7]:
+ p.append(_build_perspective_param3d(src[:, i], dst[:, i], 'x'))
+ p.append(_build_perspective_param3d(src[:, i], dst[:, i], 'y'))
+ p.append(_build_perspective_param3d(src[:, i], dst[:, i], 'z'))
+
+ # A is Bx15x15
+ A = torch.stack(p, dim=1)
+
+ # b is a Bx15x1
+ b = torch.stack(
+ [
+ dst[:, 0:1, 0],
+ dst[:, 0:1, 1],
+ dst[:, 0:1, 2],
+ dst[:, 1:2, 0],
+ dst[:, 1:2, 1],
+ dst[:, 1:2, 2],
+ dst[:, 2:3, 0],
+ dst[:, 2:3, 1],
+ dst[:, 2:3, 2],
+ # dst[:, 3:4, 0], dst[:, 3:4, 1], dst[:, 3:4, 2],
+ # dst[:, 4:5, 0], dst[:, 4:5, 1], dst[:, 4:5, 2],
+ dst[:, 5:6, 0],
+ dst[:, 5:6, 1],
+ dst[:, 5:6, 2],
+ # dst[:, 6:7, 0], dst[:, 6:7, 1], dst[:, 6:7, 2],
+ dst[:, 7:8, 0],
+ dst[:, 7:8, 1],
+ dst[:, 7:8, 2],
+ ],
+ dim=1,
+ )
+
+ # solve the system Ax = b
+ X, _ = _torch_solve_cast(b, A)
+
+ # create variable to return
+ batch_size = src.shape[0]
+ M = torch.ones(batch_size, 16, device=src.device, dtype=src.dtype)
+ M[..., :15] = torch.squeeze(X, dim=-1)
+ return M.view(-1, 4, 4) # Bx4x4
+
+
+def _build_perspective_param3d(p: torch.Tensor, q: torch.Tensor, axis: str) -> torch.Tensor:
+ ones = torch.ones_like(p)[..., 0:1]
+ zeros = torch.zeros_like(p)[..., 0:1]
+
+ if axis == 'x':
+ return torch.cat(
+ [
+ p[:, 0:1],
+ p[:, 1:2],
+ p[:, 2:3],
+ ones,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ -p[:, 0:1] * q[:, 0:1],
+ -p[:, 1:2] * q[:, 0:1],
+ -p[:, 2:3] * q[:, 0:1],
+ ],
+ dim=1,
+ )
+
+ if axis == 'y':
+ return torch.cat(
+ [
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ p[:, 0:1],
+ p[:, 1:2],
+ p[:, 2:3],
+ ones,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ -p[:, 0:1] * q[:, 1:2],
+ -p[:, 1:2] * q[:, 1:2],
+ -p[:, 2:3] * q[:, 1:2],
+ ],
+ dim=1,
+ )
+
+ if axis == 'z':
+ return torch.cat(
+ [
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ zeros,
+ p[:, 0:1],
+ p[:, 1:2],
+ p[:, 2:3],
+ ones,
+ -p[:, 0:1] * q[:, 2:3],
+ -p[:, 1:2] * q[:, 2:3],
+ -p[:, 2:3] * q[:, 2:3],
+ ],
+ dim=1,
+ )
+
+ raise NotImplementedError(f"perspective params for axis `{axis}` is not implemented.")
+
+
+def warp_perspective3d(
+ src: torch.Tensor,
+ M: torch.Tensor,
+ dsize: Tuple[int, int, int],
+ flags: str = 'bilinear',
+ border_mode: str = 'zeros',
+ align_corners: bool = False,
+) -> torch.Tensor:
+ r"""Apply a perspective transformation to an image.
+
+ The function warp_perspective transforms the source image using
+ the specified matrix:
+
+ .. math::
+ \text{dst} (x, y) = \text{src} \left(
+ \frac{M_{11} x + M_{12} y + M_{13}}{M_{31} x + M_{32} y + M_{33}} ,
+ \frac{M_{21} x + M_{22} y + M_{23}}{M_{31} x + M_{32} y + M_{33}}
+ \right )
+
+ Args:
+ src: input image with shape :math:`(B, C, D, H, W)`.
+ M: transformation matrix with shape :math:`(B, 4, 4)`.
+ dsize: size of the output image (height, width).
+ flags: interpolation mode to calculate output values
+ ``'bilinear'`` | ``'nearest'``.
+ border_mode: padding mode for outside grid values
+ ``'zeros'`` | ``'border'`` | ``'reflection'``.
+ align_corners: interpolation flag.
+
+ Returns:
+ the warped input image :math:`(B, C, D, H, W)`.
+
+ .. note::
+ This function is often used in conjunction with :func:`get_perspective_transform3d`.
+ """
+ if not isinstance(src, torch.Tensor):
+ raise TypeError(f"Input src type is not a torch.Tensor. Got {type(src)}")
+
+ if not isinstance(M, torch.Tensor):
+ raise TypeError(f"Input M type is not a torch.Tensor. Got {type(M)}")
+
+ if not len(src.shape) == 5:
+ raise ValueError(f"Input src must be a BxCxDxHxW tensor. Got {src.shape}")
+
+ if not (len(M.shape) == 3 or M.shape[-2:] == (4, 4)):
+ raise ValueError(f"Input M must be a Bx4x4 tensor. Got {M.shape}")
+
+ # launches the warper
+ d, h, w = src.shape[-3:]
+ return _transform_warp_impl3d(src, M, (d, h, w), dsize, flags, border_mode, align_corners)
+
+
+def homography_warp(
+ patch_src: torch.Tensor,
+ src_homo_dst: torch.Tensor,
+ dsize: Tuple[int, int],
+ mode: str = 'bilinear',
+ padding_mode: str = 'zeros',
+ align_corners: bool = False,
+ normalized_coordinates: bool = True,
+ normalized_homography: bool = True,
+) -> torch.Tensor:
+ r"""Warp image patches or tensors by normalized 2D homographies.
+
+ See :class:`~kornia.geometry.warp.HomographyWarper` for details.
+
+ Args:
+ patch_src: The image or tensor to warp. Should be from source of shape :math:`(N, C, H, W)`.
+ src_homo_dst: The homography or stack of homographies from destination to source of shape :math:`(N, 3, 3)`.
+ dsize:
+ if homography normalized: The height and width of the image to warp.
+ if homography not normalized: size of the output image (height, width).
+ mode: interpolation mode to calculate output values ``'bilinear'`` | ``'nearest'``.
+ padding_mode: padding mode for outside grid values ``'zeros'`` | ``'border'`` | ``'reflection'``.
+ align_corners: interpolation flag.
+ normalized_coordinates: Whether the homography assumes [-1, 1] normalized coordinates or not.
+ normalized_homography: show is homography normalized.
+
+ Return:
+ Patch sampled at locations from source to destination.
+
+ Example:
+ >>> input = torch.rand(1, 3, 32, 32)
+ >>> homography = torch.eye(3).view(1, 3, 3)
+ >>> output = homography_warp(input, homography, (32, 32))
+
+ Example
+ >>> img = torch.rand(1, 4, 5, 6)
+ >>> H = torch.eye(3)[None]
+ >>> out = homography_warp(img, H, (4, 2), align_corners=True, normalized_homography=False)
+ >>> print(out.shape)
+ torch.Size([1, 4, 4, 2])
+
+ """
+ if not src_homo_dst.device == patch_src.device:
+ raise TypeError(
+ "Patch and homography must be on the same device. \
+ Got patch.device: {} src_H_dst.device: {}.".format(
+ patch_src.device, src_homo_dst.device
+ )
+ )
+ if normalized_homography:
+ height, width = dsize
+ grid = create_meshgrid(height, width, normalized_coordinates=normalized_coordinates,
+ device=patch_src.device, dtype=patch_src.dtype)
+ warped_grid = warp_grid(grid, src_homo_dst)
+
+ return F.grid_sample(patch_src, warped_grid, mode=mode, padding_mode=padding_mode, align_corners=align_corners)
+ return warp_perspective(
+ patch_src, src_homo_dst, dsize, mode='bilinear', padding_mode=padding_mode, align_corners=True
+ )
+
+
+def _transform_warp_impl3d(
+ src: torch.Tensor,
+ dst_pix_trans_src_pix: torch.Tensor,
+ dsize_src: Tuple[int, int, int],
+ dsize_dst: Tuple[int, int, int],
+ grid_mode: str,
+ padding_mode: str,
+ align_corners: bool,
+) -> torch.Tensor:
+ """Compute the transform in normalized coordinates and perform the warping."""
+ dst_norm_trans_src_norm: torch.Tensor = normalize_homography3d(dst_pix_trans_src_pix, dsize_src, dsize_dst)
+
+ src_norm_trans_dst_norm = torch.inverse(dst_norm_trans_src_norm)
+ return homography_warp3d(src, src_norm_trans_dst_norm, dsize_dst, grid_mode, padding_mode, align_corners, True)
+
+
+def homography_warp3d(
+ patch_src: torch.Tensor,
+ src_homo_dst: torch.Tensor,
+ dsize: Tuple[int, int, int],
+ mode: str = 'bilinear',
+ padding_mode: str = 'zeros',
+ align_corners: bool = False,
+ normalized_coordinates: bool = True,
+) -> torch.Tensor:
+ r"""Warp image patches or tensors by normalized 3D homographies.
+
+ Args:
+ patch_src: The image or tensor to warp. Should be from source of shape :math:`(N, C, D, H, W)`.
+ src_homo_dst: The homography or stack of homographies from destination to source of shape
+ :math:`(N, 4, 4)`.
+ dsize: The height and width of the image to warp.
+ mode: interpolation mode to calculate output values ``'bilinear'`` | ``'nearest'``.
+ padding_mode: padding mode for outside grid values ``'zeros'`` | ``'border'`` | ``'reflection'``.
+ align_corners: interpolation flag.
+ normalized_coordinates: Whether the homography assumes [-1, 1] normalized coordinates or not.
+
+ Return:
+ Patch sampled at locations from source to destination.
+
+ Example:
+ >>> input = torch.rand(1, 3, 32, 32)
+ >>> homography = torch.eye(3).view(1, 3, 3)
+ >>> output = homography_warp(input, homography, (32, 32))
+ """
+ if not src_homo_dst.device == patch_src.device:
+ raise TypeError(
+ "Patch and homography must be on the same device. \
+ Got patch.device: {} src_H_dst.device: {}.".format(
+ patch_src.device, src_homo_dst.device
+ )
+ )
+
+ depth, height, width = dsize
+ grid = create_meshgrid3d(
+ depth, height, width, normalized_coordinates=normalized_coordinates, device=patch_src.device
+ )
+ warped_grid = warp_grid3d(grid, src_homo_dst)
+
+ return F.grid_sample(patch_src, warped_grid, mode=mode, padding_mode=padding_mode, align_corners=align_corners)
diff --git a/kornia/geometry/transform/projwarp.py b/kornia/geometry/transform/projwarp.py
deleted file mode 100644
index cc51c1a51..000000000
--- a/kornia/geometry/transform/projwarp.py
+++ /dev/null
@@ -1,430 +0,0 @@
-"""Module to perform projective transformations to tensors."""
-from typing import List, Tuple
-
-import torch
-
-from kornia.geometry.conversions import angle_axis_to_rotation_matrix, convert_affinematrix_to_homography3d, deg2rad
-from kornia.testing import check_is_tensor
-from kornia.utils import eye_like
-from kornia.utils.helpers import _torch_inverse_cast, _torch_solve_cast
-
-from .homography_warper import homography_warp3d, normalize_homography3d
-
-__all__ = [
- "warp_affine3d",
- "get_projective_transform",
- "projection_from_Rt",
- "get_perspective_transform3d",
- "warp_perspective3d",
-]
-
-
-def warp_affine3d(
- src: torch.Tensor,
- M: torch.Tensor,
- dsize: Tuple[int, int, int],
- flags: str = 'bilinear',
- padding_mode: str = 'zeros',
- align_corners: bool = True,
-) -> torch.Tensor:
- r"""Apply a projective transformation a to 3d tensor.
-
- .. warning::
- This API signature it is experimental and might suffer some changes in the future.
-
- Args:
- src : input tensor of shape :math:`(B, C, D, H, W)`.
- M: projective transformation matrix of shape :math:`(B, 3, 4)`.
- dsize: size of the output image (depth, height, width).
- mode: interpolation mode to calculate output values
- ``'bilinear'`` | ``'nearest'``.
- padding_mode: padding mode for outside grid values
- ``'zeros'`` | ``'border'`` | ``'reflection'``.
- align_corners : mode for grid_generation.
-
- Returns:
- torch.Tensor: the warped 3d tensor with shape :math:`(B, C, D, H, W)`.
-
- .. note::
- This function is often used in conjunction with :func:`get_perspective_transform3d`.
- """
- if len(src.shape) != 5:
- raise AssertionError(src.shape)
- if not (len(M.shape) == 3 and M.shape[-2:] == (3, 4)):
- raise AssertionError(M.shape)
- if len(dsize) != 3:
- raise AssertionError(dsize)
- B, C, D, H, W = src.size()
-
- size_src: Tuple[int, int, int] = (D, H, W)
- size_out: Tuple[int, int, int] = dsize
-
- M_4x4 = convert_affinematrix_to_homography3d(M) # Bx4x4
-
- # we need to normalize the transformation since grid sample needs -1/1 coordinates
- dst_norm_trans_src_norm: torch.Tensor = normalize_homography3d(M_4x4, size_src, size_out) # Bx4x4
-
- src_norm_trans_dst_norm = _torch_inverse_cast(dst_norm_trans_src_norm)
- P_norm: torch.Tensor = src_norm_trans_dst_norm[:, :3] # Bx3x4
-
- # compute meshgrid and apply to input
- dsize_out: List[int] = [B, C] + list(size_out)
- grid = torch.nn.functional.affine_grid(P_norm, dsize_out, align_corners=align_corners)
- return torch.nn.functional.grid_sample(
- src, grid, align_corners=align_corners, mode=flags, padding_mode=padding_mode
- )
-
-
-def projection_from_Rt(rmat: torch.Tensor, tvec: torch.Tensor) -> torch.Tensor:
- r"""Compute the projection matrix from Rotation and translation.
-
- .. warning::
- This API signature it is experimental and might suffer some changes in the future.
-
- Concatenates the batch of rotations and translations such that :math:`P = [R | t]`.
-
- Args:
- rmat: the rotation matrix with shape :math:`(*, 3, 3)`.
- tvec: the translation vector with shape :math:`(*, 3, 1)`.
-
- Returns:
- the projection matrix with shape :math:`(*, 3, 4)`.
-
- """
- if not (len(rmat.shape) >= 2 and rmat.shape[-2:] == (3, 3)):
- raise AssertionError(rmat.shape)
- if not (len(tvec.shape) >= 2 and tvec.shape[-2:] == (3, 1)):
- raise AssertionError(tvec.shape)
-
- return torch.cat([rmat, tvec], dim=-1) # Bx3x4
-
-
-def get_projective_transform(center: torch.Tensor, angles: torch.Tensor, scales: torch.Tensor) -> torch.Tensor:
- r"""Calculate the projection matrix for a 3D rotation.
-
- .. warning::
- This API signature it is experimental and might suffer some changes in the future.
-
- The function computes the projection matrix given the center and angles per axis.
-
- Args:
- center: center of the rotation (x,y,z) in the source with shape :math:`(B, 3)`.
- angles: angle axis vector containing the rotation angles in degrees in the form
- of (rx, ry, rz) with shape :math:`(B, 3)`. Internally it calls Rodrigues to compute
- the rotation matrix from axis-angle.
- scales: scale factor for x-y-z-directions with shape :math:`(B, 3)`.
-
- Returns:
- the projection matrix of 3D rotation with shape :math:`(B, 3, 4)`.
-
- .. note::
- This function is often used in conjunction with :func:`warp_affine3d`.
- """
- if not (len(center.shape) == 2 and center.shape[-1] == 3):
- raise AssertionError(center.shape)
- if not (len(angles.shape) == 2 and angles.shape[-1] == 3):
- raise AssertionError(angles.shape)
- if center.device != angles.device:
- raise AssertionError(center.device, angles.device)
- if center.dtype != angles.dtype:
- raise AssertionError(center.dtype, angles.dtype)
-
- # create rotation matrix
- angle_axis_rad: torch.Tensor = deg2rad(angles)
- rmat: torch.Tensor = angle_axis_to_rotation_matrix(angle_axis_rad) # Bx3x3
- scaling_matrix: torch.Tensor = eye_like(3, rmat)
- scaling_matrix = scaling_matrix * scales.unsqueeze(dim=1)
- rmat = rmat @ scaling_matrix.to(rmat)
-
- # define matrix to move forth and back to origin
- from_origin_mat = torch.eye(4)[None].repeat(rmat.shape[0], 1, 1).type_as(center) # Bx4x4
- from_origin_mat[..., :3, -1] += center
-
- to_origin_mat = from_origin_mat.clone()
- to_origin_mat = _torch_inverse_cast(from_origin_mat)
-
- # append translation with zeros
- proj_mat = projection_from_Rt(rmat, torch.zeros_like(center)[..., None]) # Bx3x4
-
- # chain 4x4 transforms
- proj_mat = convert_affinematrix_to_homography3d(proj_mat) # Bx4x4
- proj_mat = from_origin_mat @ proj_mat @ to_origin_mat
-
- return proj_mat[..., :3, :] # Bx3x4
-
-
-def get_perspective_transform3d(src: torch.Tensor, dst: torch.Tensor) -> torch.Tensor:
- r"""Calculate a 3d perspective transform from four pairs of the corresponding points.
-
- The function calculates the matrix of a perspective transform so that:
-
- .. math ::
-
- \begin{bmatrix}
- t_{i}x_{i}^{'} \\
- t_{i}y_{i}^{'} \\
- t_{i}z_{i}^{'} \\
- t_{i} \\
- \end{bmatrix}
- =
- \textbf{map_matrix} \cdot
- \begin{bmatrix}
- x_{i} \\
- y_{i} \\
- z_{i} \\
- 1 \\
- \end{bmatrix}
-
- where
-
- .. math ::
- dst(i) = (x_{i}^{'},y_{i}^{'},z_{i}^{'}), src(i) = (x_{i}, y_{i}, z_{i}), i = 0,1,2,5,7
-
- Concrete math is as below:
-
- .. math ::
-
- \[ u_i =\frac{c_{00} * x_i + c_{01} * y_i + c_{02} * z_i + c_{03}}
- {c_{30} * x_i + c_{31} * y_i + c_{32} * z_i + c_{33}} \]
- \[ v_i =\frac{c_{10} * x_i + c_{11} * y_i + c_{12} * z_i + c_{13}}
- {c_{30} * x_i + c_{31} * y_i + c_{32} * z_i + c_{33}} \]
- \[ w_i =\frac{c_{20} * x_i + c_{21} * y_i + c_{22} * z_i + c_{23}}
- {c_{30} * x_i + c_{31} * y_i + c_{32} * z_i + c_{33}} \]
-
- .. math ::
-
- \begin{pmatrix}
- x_0 & y_0 & z_0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_0*u_0 & -y_0*u_0 & -z_0 * u_0 \\
- x_1 & y_1 & z_1 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_1*u_1 & -y_1*u_1 & -z_1 * u_1 \\
- x_2 & y_2 & z_2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_2*u_2 & -y_2*u_2 & -z_2 * u_2 \\
- x_5 & y_5 & z_5 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_5*u_5 & -y_5*u_5 & -z_5 * u_5 \\
- x_7 & y_7 & z_7 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -x_7*u_7 & -y_7*u_7 & -z_7 * u_7 \\
- 0 & 0 & 0 & 0 & x_0 & y_0 & z_0 & 1 & 0 & 0 & 0 & 0 & -x_0*v_0 & -y_0*v_0 & -z_0 * v_0 \\
- 0 & 0 & 0 & 0 & x_1 & y_1 & z_1 & 1 & 0 & 0 & 0 & 0 & -x_1*v_1 & -y_1*v_1 & -z_1 * v_1 \\
- 0 & 0 & 0 & 0 & x_2 & y_2 & z_2 & 1 & 0 & 0 & 0 & 0 & -x_2*v_2 & -y_2*v_2 & -z_2 * v_2 \\
- 0 & 0 & 0 & 0 & x_5 & y_5 & z_5 & 1 & 0 & 0 & 0 & 0 & -x_5*v_5 & -y_5*v_5 & -z_5 * v_5 \\
- 0 & 0 & 0 & 0 & x_7 & y_7 & z_7 & 1 & 0 & 0 & 0 & 0 & -x_7*v_7 & -y_7*v_7 & -z_7 * v_7 \\
- 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_0 & y_0 & z_0 & 1 & -x_0*w_0 & -y_0*w_0 & -z_0 * w_0 \\
- 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_1 & y_1 & z_1 & 1 & -x_1*w_1 & -y_1*w_1 & -z_1 * w_1 \\
- 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_2 & y_2 & z_2 & 1 & -x_2*w_2 & -y_2*w_2 & -z_2 * w_2 \\
- 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_5 & y_5 & z_5 & 1 & -x_5*w_5 & -y_5*w_5 & -z_5 * w_5 \\
- 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & x_7 & y_7 & z_7 & 1 & -x_7*w_7 & -y_7*w_7 & -z_7 * w_7 \\
- \end{pmatrix}
-
- Args:
- src: coordinates of quadrangle vertices in the source image with shape :math:`(B, 8, 3)`.
- dst: coordinates of the corresponding quadrangle vertices in
- the destination image with shape :math:`(B, 8, 3)`.
-
- Returns:
- the perspective transformation with shape :math:`(B, 4, 4)`.
-
- .. note::
- This function is often used in conjunction with :func:`warp_perspective3d`.
- """
- if not isinstance(src, (torch.Tensor)):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(src)}")
-
- if not isinstance(dst, (torch.Tensor)):
- raise TypeError(f"Input type is not a torch.Tensor. Got {type(dst)}")
-
- if not src.shape[-2:] == (8, 3):
- raise ValueError(f"Inputs must be a Bx8x3 tensor. Got {src.shape}")
-
- if not src.shape == dst.shape:
- raise ValueError(f"Inputs must have the same shape. Got {dst.shape}")
-
- if not (src.shape[0] == dst.shape[0]):
- raise ValueError(f"Inputs must have same batch size dimension. Expect {src.shape} but got {dst.shape}")
-
- if not (src.device == dst.device and src.dtype == dst.dtype):
- raise AssertionError(
- f"Expect `src` and `dst` to be in the same device (Got {src.dtype}, {dst.dtype}) "
- f"with the same dtype (Got {src.dtype}, {dst.dtype})."
- )
-
- # we build matrix A by using only 4 point correspondence. The linear
- # system is solved with the least square method, so here
- # we could even pass more correspondence
- p = []
-
- # 000, 100, 110, 101, 011
- for i in [0, 1, 2, 5, 7]:
- p.append(_build_perspective_param3d(src[:, i], dst[:, i], 'x'))
- p.append(_build_perspective_param3d(src[:, i], dst[:, i], 'y'))
- p.append(_build_perspective_param3d(src[:, i], dst[:, i], 'z'))
-
- # A is Bx15x15
- A = torch.stack(p, dim=1)
-
- # b is a Bx15x1
- b = torch.stack(
- [
- dst[:, 0:1, 0],
- dst[:, 0:1, 1],
- dst[:, 0:1, 2],
- dst[:, 1:2, 0],
- dst[:, 1:2, 1],
- dst[:, 1:2, 2],
- dst[:, 2:3, 0],
- dst[:, 2:3, 1],
- dst[:, 2:3, 2],
- # dst[:, 3:4, 0], dst[:, 3:4, 1], dst[:, 3:4, 2],
- # dst[:, 4:5, 0], dst[:, 4:5, 1], dst[:, 4:5, 2],
- dst[:, 5:6, 0],
- dst[:, 5:6, 1],
- dst[:, 5:6, 2],
- # dst[:, 6:7, 0], dst[:, 6:7, 1], dst[:, 6:7, 2],
- dst[:, 7:8, 0],
- dst[:, 7:8, 1],
- dst[:, 7:8, 2],
- ],
- dim=1,
- )
-
- # solve the system Ax = b
- X, _ = _torch_solve_cast(b, A)
-
- # create variable to return
- batch_size = src.shape[0]
- M = torch.ones(batch_size, 16, device=src.device, dtype=src.dtype)
- M[..., :15] = torch.squeeze(X, dim=-1)
- return M.view(-1, 4, 4) # Bx4x4
-
-
-def _build_perspective_param3d(p: torch.Tensor, q: torch.Tensor, axis: str) -> torch.Tensor:
- ones = torch.ones_like(p)[..., 0:1]
- zeros = torch.zeros_like(p)[..., 0:1]
-
- if axis == 'x':
- return torch.cat(
- [
- p[:, 0:1],
- p[:, 1:2],
- p[:, 2:3],
- ones,
- zeros,
- zeros,
- zeros,
- zeros,
- zeros,
- zeros,
- zeros,
- zeros,
- -p[:, 0:1] * q[:, 0:1],
- -p[:, 1:2] * q[:, 0:1],
- -p[:, 2:3] * q[:, 0:1],
- ],
- dim=1,
- )
-
- if axis == 'y':
- return torch.cat(
- [
- zeros,
- zeros,
- zeros,
- zeros,
- p[:, 0:1],
- p[:, 1:2],
- p[:, 2:3],
- ones,
- zeros,
- zeros,
- zeros,
- zeros,
- -p[:, 0:1] * q[:, 1:2],
- -p[:, 1:2] * q[:, 1:2],
- -p[:, 2:3] * q[:, 1:2],
- ],
- dim=1,
- )
-
- if axis == 'z':
- return torch.cat(
- [
- zeros,
- zeros,
- zeros,
- zeros,
- zeros,
- zeros,
- zeros,
- zeros,
- p[:, 0:1],
- p[:, 1:2],
- p[:, 2:3],
- ones,
- -p[:, 0:1] * q[:, 2:3],
- -p[:, 1:2] * q[:, 2:3],
- -p[:, 2:3] * q[:, 2:3],
- ],
- dim=1,
- )
-
- raise NotImplementedError(f"perspective params for axis `{axis}` is not implemented.")
-
-
-def warp_perspective3d(
- src: torch.Tensor,
- M: torch.Tensor,
- dsize: Tuple[int, int, int],
- flags: str = 'bilinear',
- border_mode: str = 'zeros',
- align_corners: bool = False,
-) -> torch.Tensor:
- r"""Apply a perspective transformation to an image.
-
- The function warp_perspective transforms the source image using
- the specified matrix:
-
- .. math::
- \text{dst} (x, y) = \text{src} \left(
- \frac{M_{11} x + M_{12} y + M_{13}}{M_{31} x + M_{32} y + M_{33}} ,
- \frac{M_{21} x + M_{22} y + M_{23}}{M_{31} x + M_{32} y + M_{33}}
- \right )
-
- Args:
- src: input image with shape :math:`(B, C, D, H, W)`.
- M: transformation matrix with shape :math:`(B, 4, 4)`.
- dsize: size of the output image (height, width).
- flags: interpolation mode to calculate output values
- ``'bilinear'`` | ``'nearest'``.
- border_mode: padding mode for outside grid values
- ``'zeros'`` | ``'border'`` | ``'reflection'``.
- align_corners: interpolation flag.
-
- Returns:
- the warped input image :math:`(B, C, D, H, W)`.
-
- .. note::
- This function is often used in conjunction with :func:`get_perspective_transform3d`.
- """
- check_is_tensor(src)
- check_is_tensor(M)
-
- if not len(src.shape) == 5:
- raise ValueError(f"Input src must be a BxCxDxHxW tensor. Got {src.shape}")
-
- if not (len(M.shape) == 3 or M.shape[-2:] == (4, 4)):
- raise ValueError(f"Input M must be a Bx4x4 tensor. Got {M.shape}")
-
- # launches the warper
- d, h, w = src.shape[-3:]
- return transform_warp_impl3d(src, M, (d, h, w), dsize, flags, border_mode, align_corners)
-
-
-def transform_warp_impl3d(
- src: torch.Tensor,
- dst_pix_trans_src_pix: torch.Tensor,
- dsize_src: Tuple[int, int, int],
- dsize_dst: Tuple[int, int, int],
- grid_mode: str,
- padding_mode: str,
- align_corners: bool,
-) -> torch.Tensor:
- """Compute the transform in normalized coordinates and perform the warping."""
- dst_norm_trans_src_norm: torch.Tensor = normalize_homography3d(dst_pix_trans_src_pix, dsize_src, dsize_dst)
-
- src_norm_trans_dst_norm = torch.inverse(dst_norm_trans_src_norm)
- return homography_warp3d(src, src_norm_trans_dst_norm, dsize_dst, grid_mode, padding_mode, align_corners, True)
diff --git a/test/geometry/transform/test_homography_warper.py b/test/geometry/transform/test_homography_warper.py
index 2e338d715..e0cdf6f1b 100644
--- a/test/geometry/transform/test_homography_warper.py
+++ b/test/geometry/transform/test_homography_warper.py
@@ -33,14 +33,14 @@ class TestHomographyWarper:
res = torch.tensor([[[0.5, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
assert (
- kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype) == res
+ kornia.geometry.conversions.normal_transform_pixel(height, width, device=device, dtype=dtype) == res
).all()
- norm_homo = kornia.geometry.transform.normalize_homography(dst_homo_src, (height, width), (height, width))
+ norm_homo = kornia.geometry.conversions.normalize_homography(dst_homo_src, (height, width), (height, width))
assert (norm_homo == dst_homo_src).all()
# change output scale
- norm_homo = kornia.geometry.transform.normalize_homography(
+ norm_homo = kornia.geometry.conversions.normalize_homography(
dst_homo_src, (height, width), (height * 2, width // 2)
)
res = torch.tensor(
@@ -56,14 +56,14 @@ class TestHomographyWarper:
res = torch.tensor([[[0.5, 0.0, -1.0], [0.0, 2.0, -1.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
assert (
- kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype) == res
+ kornia.geometry.conversions.normal_transform_pixel(height, width, device=device, dtype=dtype) == res
).all()
- denorm_homo = kornia.geometry.transform.denormalize_homography(dst_homo_src, (height, width), (height, width))
+ denorm_homo = kornia.geometry.conversions.denormalize_homography(dst_homo_src, (height, width), (height, width))
assert (denorm_homo == dst_homo_src).all()
# change output scale
- denorm_homo = kornia.geometry.transform.denormalize_homography(
+ denorm_homo = kornia.geometry.conversions.denormalize_homography(
dst_homo_src, (height, width), (height * 2, width // 2)
)
res = torch.tensor([[[0.25, 0.0, 0.0], [0.0, 3.0, 0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype).repeat(
@@ -82,7 +82,7 @@ class TestHomographyWarper:
dst_homo_src[..., 1, 2] = 2.0
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
- norm_homo = kornia.geometry.transform.normalize_homography(dst_homo_src, (height, width), (height, width))
+ norm_homo = kornia.geometry.conversions.normalize_homography(dst_homo_src, (height, width), (height, width))
res = torch.tensor([[[0.5, 0.0, 0.0], [0.0, 2.0, 5.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
assert (norm_homo == res).all()
@@ -97,7 +97,7 @@ class TestHomographyWarper:
dst_homo_src[..., 1, 2] = 2.0
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
- denorm_homo = kornia.geometry.transform.denormalize_homography(dst_homo_src, (height, width), (height, width))
+ denorm_homo = kornia.geometry.conversions.denormalize_homography(dst_homo_src, (height, width), (height, width))
res = torch.tensor([[[0.5, 0.0, 3.0], [0.0, 2.0, 0.5], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
assert (denorm_homo == res).all()
@@ -112,11 +112,15 @@ class TestHomographyWarper:
dst_homo_src[..., 1, 2] = 2.0
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
- denorm_homo = kornia.geometry.transform.denormalize_homography(dst_homo_src, (height, width), (height, width))
- norm_denorm_homo = kornia.geometry.transform.normalize_homography(denorm_homo, (height, width), (height, width))
+ denorm_homo = kornia.geometry.conversions.denormalize_homography(
+ dst_homo_src, (height, width), (height, width))
+ norm_denorm_homo = kornia.geometry.conversions.normalize_homography(
+ denorm_homo, (height, width), (height, width))
assert (dst_homo_src == norm_denorm_homo).all()
- norm_homo = kornia.geometry.transform.normalize_homography(dst_homo_src, (height, width), (height, width))
- denorm_norm_homo = kornia.geometry.transform.denormalize_homography(norm_homo, (height, width), (height, width))
+ norm_homo = kornia.geometry.conversions.normalize_homography(
+ dst_homo_src, (height, width), (height, width))
+ denorm_norm_homo = kornia.geometry.conversions.denormalize_homography(
+ norm_homo, (height, width), (height, width))
assert (dst_homo_src == denorm_norm_homo).all()
@pytest.mark.parametrize("offset", [1, 3, 7])
@@ -215,7 +219,7 @@ class TestHomographyWarper:
# same transform precomputing the grid
warper.precompute_warp_grid(_torch_inverse_cast(dst_homo_src_i))
patch_dst_to_src_precomputed = warper(patch_dst)
- assert (patch_dst_to_src_precomputed == patch_dst_to_src).all()
+ assert_close(patch_dst_to_src_precomputed, patch_dst_to_src, atol=1e-4, rtol=1e-4)
# projected should be equal as initial
error = utils.compute_patch_error(patch_src, patch_dst_to_src, height, width)
@@ -302,41 +306,42 @@ class TestHomographyNormalTransform:
@pytest.mark.parametrize("height,width,expected", [(2, 5, expected_2d_0), (1, 5, expected_2d_1)])
def test_transform2d(self, height, width, expected, device, dtype):
- output = kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype)
+ output = kornia.geometry.conversions.normal_transform_pixel(height, width, device=device, dtype=dtype)
assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("height", [1, 2, 5])
@pytest.mark.parametrize("width", [1, 2, 5])
def test_divide_by_zero2d(self, height, width, device, dtype):
- output = kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype)
+ output = kornia.geometry.conversions.normal_transform_pixel(height, width, device=device, dtype=dtype)
assert torch.isinf(output).sum().item() == 0
def test_transform2d_apply(self, device, dtype):
height, width = 2, 5
input = torch.tensor([[0.0, 0.0], [width - 1, height - 1]], device=device, dtype=dtype)
expected = torch.tensor([[-1.0, -1.0], [1.0, 1.0]], device=device, dtype=dtype)
- transform = kornia.geometry.transform.normal_transform_pixel(height, width, device=device, dtype=dtype)
+ transform = kornia.geometry.conversions.normal_transform_pixel(height, width, device=device, dtype=dtype)
output = kornia.geometry.linalg.transform_points(transform, input)
assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("height,width,depth,expected", [(2, 6, 4, expected_3d_0), (1, 6, 4, expected_3d_1)])
def test_transform3d(self, height, width, depth, expected, device, dtype):
- output = kornia.geometry.transform.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
+ output = kornia.geometry.conversions.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("height", [1, 2, 5])
@pytest.mark.parametrize("width", [1, 2, 5])
@pytest.mark.parametrize("depth", [1, 2, 5])
def test_divide_by_zero3d(self, height, width, depth, device, dtype):
- output = kornia.geometry.transform.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
+ output = kornia.geometry.conversions.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
assert torch.isinf(output).sum().item() == 0
def test_transform3d_apply(self, device, dtype):
depth, height, width = 3, 2, 5
input = torch.tensor([[0.0, 0.0, 0.0], [width - 1, height - 1, depth - 1]], device=device, dtype=dtype)
expected = torch.tensor([[-1.0, -1.0, -1.0], [1.0, 1.0, 1.0]], device=device, dtype=dtype)
- transform = kornia.geometry.transform.normal_transform_pixel3d(depth, height, width, device=device, dtype=dtype)
+ transform = kornia.geometry.conversions.normal_transform_pixel3d(
+ depth, height, width, device=device, dtype=dtype)
output = kornia.geometry.linalg.transform_points(transform, input)
assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
@@ -356,23 +361,23 @@ class TestHomographyWarper3D:
device=device,
dtype=dtype,
)
- norm = kornia.geometry.transform.normal_transform_pixel3d(input_shape[0], input_shape[1], input_shape[2]).to(
+ norm = kornia.geometry.conversions.normal_transform_pixel3d(input_shape[0], input_shape[1], input_shape[2]).to(
device=device, dtype=dtype
)
assert_close(norm, res, rtol=1e-4, atol=1e-4)
- norm_homo = kornia.geometry.transform.normalize_homography3d(dst_homo_src, input_shape, input_shape).to(
+ norm_homo = kornia.geometry.conversions.normalize_homography3d(dst_homo_src, input_shape, input_shape).to(
device=device, dtype=dtype
)
assert_close(norm_homo, dst_homo_src, rtol=1e-4, atol=1e-4)
- norm_homo = kornia.geometry.transform.normalize_homography3d(dst_homo_src, input_shape, input_shape).to(
+ norm_homo = kornia.geometry.conversions.normalize_homography3d(dst_homo_src, input_shape, input_shape).to(
device=device, dtype=dtype
)
assert_close(norm_homo, dst_homo_src, rtol=1e-4, atol=1e-4)
# change output scale
- norm_homo = kornia.geometry.transform.normalize_homography3d(
+ norm_homo = kornia.geometry.conversions.normalize_homography3d(
dst_homo_src, input_shape, (input_shape[0] // 2, input_shape[1] * 2, input_shape[2] // 2)
).to(device=device, dtype=dtype)
res = torch.tensor(
@@ -394,7 +399,7 @@ class TestHomographyWarper3D:
dst_homo_src[..., 2, 3] = 3.0
dst_homo_src = dst_homo_src.expand(batch_size, -1, -1)
- norm_homo = kornia.geometry.transform.normalize_homography3d(dst_homo_src, (2, 2, 5), (2, 2, 5))
+ norm_homo = kornia.geometry.conversions.normalize_homography3d(dst_homo_src, (2, 2, 5), (2, 2, 5))
res = torch.tensor(
[[[0.5, 0.0, 0.0, 0.0], [0.0, 0.5, 0.0, 3.5], [0.0, 0.0, 2.0, 7.0], [0.0, 0.0, 0.0, 1.0]]],
device=device,
diff --git a/test/geometry/transform/test_image_registrator.py b/test/geometry/transform/test_image_registrator.py
index 678689818..4cc15d163 100644
--- a/test/geometry/transform/test_image_registrator.py
+++ b/test/geometry/transform/test_image_registrator.py
@@ -4,7 +4,8 @@ import torch
import kornia
import kornia.testing as utils # test utils
from kornia.geometry import transform_points
-from kornia.geometry.transform import denormalize_homography, ImageRegistrator
+from kornia.geometry.conversions import denormalize_homography
+from kornia.geometry.transform import ImageRegistrator
from kornia.testing import assert_close
diff --git a/test/geometry/transform/test_imgwarp.py b/test/geometry/transform/test_imgwarp.py
index 49c5050c0..df9fe5474 100644
--- a/test/geometry/transform/test_imgwarp.py
+++ b/test/geometry/transform/test_imgwarp.py
@@ -241,6 +241,21 @@ class TestWarpPerspective:
img_a = kornia.geometry.warp_perspective(img_b, homo_ab, (h, w))
assert_close(img_a, expected, atol=1e-4, rtol=1e-4)
+ def test_translation_normalized(self, device, dtype):
+ offset = 1.0
+ h, w = 3, 4
+
+ img_b = torch.arange(float(h * w), device=device, dtype=dtype).view(1, 1, h, w)
+ homo_ab = kornia.eye_like(3, img_b)
+ homo_ab[..., :2, -1] += offset
+
+ expected = torch.zeros_like(img_b)
+ expected[..., 1:, 1:] = img_b[..., :2, :3]
+
+ # Same as opencv: cv2.warpPerspective(kornia.tensor_to_image(img_b), homo_ab[0].numpy(), (w, h))
+ img_a = kornia.geometry.transform.homography_warp(img_b, homo_ab, (h, w), normalized_homography=False)
+ assert_close(img_a, expected, atol=1e-4, rtol=1e-4)
+
def test_rotation_inverse(self, device, dtype):
h, w = 4, 4
img_b = torch.rand(1, 1, h, w, device=device, dtype=dtype)
diff --git a/test/geometry/transform/test_projwarp.py b/test/geometry/transform/test_imgwarp3d.py
similarity index 99%
rename from test/geometry/transform/test_projwarp.py
rename to test/geometry/transform/test_imgwarp3d.py
index 5b86c913c..fce049441 100644
--- a/test/geometry/transform/test_projwarp.py
+++ b/test/geometry/transform/test_imgwarp3d.py
@@ -3,7 +3,7 @@ import torch
from torch.autograd import gradcheck
import kornia
-import kornia.geometry.transform.projwarp as proj
+import kornia.geometry.transform as proj
import kornia.testing as utils # test utils
from kornia.testing import assert_close
diff --git a/test/utils/test_grid.py b/test/utils/test_grid.py
index 0423f403e..efa7302fc 100644
--- a/test/utils/test_grid.py
+++ b/test/utils/test_grid.py
@@ -42,7 +42,7 @@ def test_normalize_pixel_grid(device, dtype):
grid_pix = torch.unsqueeze(grid_pix, dim=0)
# grid from pixel space to normalized
- norm_trans_pix = kornia.geometry.transform.normal_transform_pixel(
+ norm_trans_pix = kornia.geometry.conversions.normal_transform_pixel(
height, width, device=device, dtype=dtype
) # 1x3x3
pix_trans_norm = torch.inverse(norm_trans_pix) # 1x3x3