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 normalize_homography3d(dst_pix_trans_src_pix: torch.Tensor, dsize_src: Tuple[int, int, int], dsize_dst: Tuple[int, int, int]) -> torch.Tensor: __doc__ = HMW.normalize_homography3d.__doc__ warnings.warn( "`normalize_homography3d` is deprecated and will be removed > 0.6.0. " "Please use `kornia.geometry.transform.normalize_homography3d instead.`", DeprecationWarning, stacklevel=2) return HMW.normalize_homography3d(dst_pix_trans_src_pix, dsize_src, dsize_dst)
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"""Applies a projective transformation a to 3d tensor. .. warning:: This API signature it is experimental and might suffer some changes in the future. Args: src (torch.Tensor): input tensor of shape :math:`(B, C, D, H, W)`. M (torch.Tensor): projective transformation matrix of shape :math:`(B, 3, 4)`. dsize (Tuple[int, int, int]): size of the output image (depth, height, width). mode (str): interpolation mode to calculate output values 'bilinear' | 'nearest'. Default: 'bilinear'. padding_mode (str): padding mode for outside grid values 'zeros' | 'border' | 'reflection'. Default: 'zeros'. align_corners (bool): mode for grid_generation. Default: True. Returns: torch.Tensor: the warped 3d tensor with shape :math:`(B, C, D, H, W)`. """ assert len(src.shape) == 5, src.shape assert len(M.shape) == 3 and M.shape[-2:] == (3, 4), M.shape assert len(dsize) == 3, 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(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 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 cooridnates 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 warp_affine3d( src: torch.Tensor, M: torch.Tensor, dsize: Tuple[int, int, int], flags: str = 'bilinear', padding_mode: str = 'zeros', align_corners: Optional[bool] = None, ) -> torch.Tensor: r"""Applies 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 conjuntion with :func:`get_perspective_transform3d`. """ assert len(src.shape) == 5, src.shape assert len(M.shape) == 3 and M.shape[-2:] == (3, 4), M.shape assert len(dsize) == 3, dsize B, C, D, H, W = src.size() # TODO: remove the statement below in kornia v0.6 if align_corners is None: message: str = ( "The align_corners default value has been changed. By default now is set True " "in order to match cv2.warpAffine. In case you want to keep your previous " "behaviour set it to False. This warning will disappear in kornia > v0.6." ) warnings.warn(message) # set default value for align corners align_corners = True 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)