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, thealign_cornersargument selects the matchingtorch.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
0under either flag, and a size-0 axis gives an empty grid.An integer
dtypeis 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 functiontorch.nn.functional.grid_sample(). Default:Truedevice (
Optional[device], optional) – the device on which the grid will be generated. Default:Nonedtype (
Optional[dtype], optional) – the data type of the generated grid. Default:Nonealign_corners (
bool, optional) – which normalization convention to use whennormalized_coordinatesisTrue.Truemaps pixel centers \([0, size-1]\) to \([-1, 1]\);Falseuses the half-pixel mapping \(x_{norm} = (2x + 1) / W - 1\), where \(\pm 1\) are the outer pixel edges. Ignored whennormalized_coordinatesisFalse. Default:True
- Return type:
- 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 totorch.nn.functional.grid_sample().A size-1 axis is
0and a size-0 axis gives an empty grid.Known defect: there is no
align_cornersargument, so the half-pixel grid thatgrid_sample(..., align_corners=False)expects cannot be requested (#4503).
- Parameters:
depth (
int) – the volume depth (theDaxis).height (
int) – the image height (rows).width (
int) – the image width (cols).normalized_coordinates (
bool, optional) – whether to normalize coordinates to \([-1, 1]\). Default:Truedevice (
Optional[device], optional) – the device on which the grid will be generated. Default:Nonedtype (
Optional[dtype], optional) – the data type of the generated grid. Default:None
- Return type:
- Returns:
grid tensor with shape \((1, D, H, W, 3)\).