kornia.geometry.warp

class HomographyWarper(height: int, width: int, mode: str = 'bilinear', padding_mode: str = 'zeros', normalized_coordinates: bool = True)[source]

Warps image patches or tensors by homographies.

\[X_{dst} = H_{src}^{\{dst\}} * X_{src}\]
Parameters
  • height (int) – The height of the image to warp.

  • width (int) – The width of the image to warp.

  • 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’.

  • normalized_coordinates (bool) – wether to use a grid with normalized coordinates.

forward(patch_src: torch.Tensor, dst_homo_src: torch.Tensor) → torch.Tensor[source]

Warps an image or tensor from source into reference frame.

Parameters
  • patch_src (torch.Tensor) – The image or tensor to warp. Should be from source.

  • dst_homo_src (torch.Tensor) – The homography or stack of homographies from source to destination. The homography assumes normalized coordinates [-1, 1].

Returns

Patch sampled at locations from source to destination.

Return type

torch.Tensor

Shape:
  • Input: \((N, C, H, W)\) and \((N, 3, 3)\)

  • Output: \((N, C, H, W)\)

Example

>>> input = torch.rand(1, 3, 32, 32)
>>> homography = torch.eye(3).view(1, 3, 3)
>>> warper = kornia.HomographyWarper(32, 32)
>>> output = warper(input, homography)  # NxCxHxW
warp_grid(dst_homo_src: torch.Tensor) → torch.Tensor[source]

Computes the grid to warp the coordinates grid by an homography.

Parameters

dst_homo_src (torch.Tensor) – Homography or homographies (stacked) to transform all points in the grid. Shape of the homography has to be \((N, 3, 3)\).

Returns

the transformed grid of shape \((N, H, W, 2)\).

Return type

torch.Tensor

class DepthWarper(pinhole_dst: kornia.geometry.camera.pinhole.PinholeCamera, height: int, width: int, mode: str = 'bilinear', padding_mode: str = 'zeros')[source]

Warps a patch by depth.

\[ \begin{align}\begin{aligned}P_{src}^{\{dst\}} = K_{dst} * T_{src}^{\{dst\}}\\\begin{split}I_{src} = \\omega(I_{dst}, P_{src}^{\{dst\}}, D_{src})\end{split}\end{aligned}\end{align} \]
Parameters
  • pinholes_dst (PinholeCamera) – the pinhole models for the destination frame.

  • height (int) – the height of the image to warp.

  • width (int) – the width of the image to warp.

  • 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’.

compute_projection_matrix(pinhole_src: kornia.geometry.camera.pinhole.PinholeCamera) → kornia.geometry.warp.depth_warper.DepthWarper[source]

Computes the projection matrix from the source to destinaion frame.

compute_subpixel_step() → torch.Tensor[source]

This computes the required inverse depth step to achieve sub pixel accurate sampling of the depth cost volume, per camera.

Szeliski, Richard, and Daniel Scharstein. “Symmetric sub-pixel stereo matching.” European Conference on Computer Vision. Springer Berlin Heidelberg, 2002.

forward(depth_src: torch.Tensor, patch_dst: torch.Tensor) → torch.Tensor[source]

Warps a tensor from destination frame to reference given the depth in the reference frame.

Parameters
  • depth_src (torch.Tensor) – the depth in the reference frame. The tensor must have a shape \((B, 1, H, W)\).

  • patch_dst (torch.Tensor) – the patch in the destination frame. The tensor must have a shape \((B, C, H, W)\).

Returns

the warped patch from destination frame to reference.

Return type

torch.Tensor

Shape:
  • Output: \((N, C, H, W)\) where C = number of channels.

Example

>>> # pinholes camera models
>>> pinhole_dst = kornia.PinholeCamera(...)
>>> pinhole_src = kornia.PinholeCamera(...)
>>> # create the depth warper, compute the projection matrix
>>> warper = kornia.DepthWarper(pinhole_dst, height, width)
>>> warper.compute_projection_matrix(pinhole_src)
>>> # warp the destionation frame to reference by depth
>>> depth_src = torch.ones(1, 1, 32, 32)  # Nx1xHxW
>>> image_dst = torch.rand(1, 3, 32, 32)  # NxCxHxW
>>> image_src = warper(depth_src, image_dst)  # NxCxHxW
warp_grid(depth_src: torch.Tensor) → torch.Tensor[source]

Computes a grid for warping a given the depth from the reference pinhole camera.

The function compute_projection_matrix has to be called beforehand in order to have precomputed the relative projection matrices encoding the relative pose and the intrinsics between the reference and a non reference camera.

homography_warp(patch_src: torch.Tensor, dst_homo_src: torch.Tensor, dsize: Tuple[int, int], mode: str = 'bilinear', padding_mode: str = 'zeros') → torch.Tensor[source]

Function that warps image patchs or tensors by homographies.

See HomographyWarper for details.

Parameters
  • patch_src (torch.Tensor) – The image or tensor to warp. Should be from source of shape \((N, C, H, W)\).

  • dst_homo_src (torch.Tensor) – The homography or stack of homographies from source to destination of shape \((N, 3, 3)\).

  • dsize (Tuple[int, int]) – The height and width of the image to warp.

  • 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’.

Returns

Patch sampled at locations from source to destination.

Return type

torch.Tensor

Example

>>> input = torch.rand(1, 3, 32, 32)
>>> homography = torch.eye(3).view(1, 3, 3)
>>> output = kornia.homography_warp(input, homography, (32, 32))
depth_warp(pinhole_dst: kornia.geometry.camera.pinhole.PinholeCamera, pinhole_src: kornia.geometry.camera.pinhole.PinholeCamera, depth_src: torch.Tensor, patch_dst: torch.Tensor, height: int, width: int)[source]

Function that warps a tensor from destination frame to reference given the depth in the reference frame.

See DepthWarper for details.

Example

>>> # pinholes camera models
>>> pinhole_dst = kornia.PinholeCamera(...)
>>> pinhole_src = kornia.PinholeCamera(...)
>>> # warp the destionation frame to reference by depth
>>> depth_src = torch.ones(1, 1, 32, 32)  # Nx1xHxW
>>> image_dst = torch.rand(1, 3, 32, 32)  # NxCxHxW
>>> image_src = kornia.depth_warp(pinhole_dst, pinhole_src,
>>>     depth_src, image_dst, height, width)  # NxCxHxW