kornia.geometry.grid#

kornia.geometry.grid.create_meshgrid(height, width, normalized_coordinates=True, device=None, dtype=None, align_corners=True)[source]#

Generate a coordinate grid for an image.

Convention:
  • Coordinates follow Coordinates and sizes. With normalized_coordinates=True, the align_corners argument selects the matching torch.nn.functional.grid_sample() convention; pass the same flag to both calls. The default is corner-aligned (align_corners=True).

  • A size-1 axis is 0 under either flag, and a size-0 axis gives an empty grid.

  • An integer dtype is kept for a pixel grid; a normalized grid is then torch’s default floating dtype.

Parameters:
  • height (int) – the image height (rows).

  • width (int) – the image width (cols).

  • normalized_coordinates (bool, optional) – whether to normalize coordinates in the range \([-1,1]\) in order to be consistent with the PyTorch function torch.nn.functional.grid_sample(). Default: True

  • device (Optional[device], optional) – the device on which the grid will be generated. Default: None

  • dtype (Optional[dtype], optional) – the data type of the generated grid. Default: None

  • align_corners (bool, optional) – which normalization convention to use when normalized_coordinates is True. True maps pixel centers \([0, size-1]\) to \([-1, 1]\); False uses the half-pixel mapping \(x_{norm} = (2x + 1) / W - 1\), where \(\pm 1\) are the outer pixel edges. Ignored when normalized_coordinates is False. Default: True

Return type:

Tensor

Returns:

grid tensor with shape \((1, H, W, 2)\).

Example

>>> create_meshgrid(2, 2)
tensor([[[[-1., -1.],
          [ 1., -1.]],

         [[-1.,  1.],
          [ 1.,  1.]]]])
>>> create_meshgrid(2, 2, align_corners=False)
tensor([[[[-0.5000, -0.5000],
          [ 0.5000, -0.5000]],

         [[-0.5000,  0.5000],
          [ 0.5000,  0.5000]]]])
>>> create_meshgrid(2, 2, normalized_coordinates=False)
tensor([[[[0., 0.],
          [1., 0.]],

         [[0., 1.],
          [1., 1.]]]])
kornia.geometry.grid.create_meshgrid3d(depth, height, width, normalized_coordinates=True, device=None, dtype=None)[source]#

Generate a coordinate grid for an image.

Convention:
  • Coordinates follow Coordinates and sizes. A normalized grid is always corner-aligned; reorder its last axis with grid[..., [1, 2, 0]] before passing it to torch.nn.functional.grid_sample().

  • A size-1 axis is 0 and a size-0 axis gives an empty grid.

  • Known defect: there is no align_corners argument, so the half-pixel grid that grid_sample(..., align_corners=False) expects cannot be requested (#4503).

Parameters:
  • depth (int) – the volume depth (the D axis).

  • height (int) – the image height (rows).

  • width (int) – the image width (cols).

  • normalized_coordinates (bool, optional) – whether to normalize coordinates to \([-1, 1]\). Default: True

  • device (Optional[device], optional) – the device on which the grid will be generated. Default: None

  • dtype (Optional[dtype], optional) – the data type of the generated grid. Default: None

Return type:

Tensor

Returns:

grid tensor with shape \((1, D, H, W, 3)\).