Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Kornia Kornia Coordinate Grid

From Leeroopedia


Knowledge Sources
Domains Vision, Geometry
Last Updated 2026-02-09 15:00 GMT

Overview

Provides functions to generate 2D and 3D coordinate meshgrids compatible with PyTorch spatial transformers and grid sampling operations.

Description

The grid.py module in the Kornia geometry library provides two functions: create_meshgrid for generating 2D coordinate grids and create_meshgrid3d for generating 3D coordinate grids. These functions produce (x, y) or (x, y, z) coordinate tensors that can operate in either normalized ([-1, 1]) or pixel coordinate space. The normalized mode is designed for direct compatibility with torch.nn.functional.grid_sample. The 2D function returns a tensor of shape (1, H, W, 2) and the 3D function returns (1, D, H, W, 3). Both functions support configurable device and dtype.

Usage

Import these functions when you need to create coordinate grids for spatial transformer networks, image warping, depth unprojection, or any operation that requires a pixel-to-coordinate mapping across an image or volume.

Code Reference

Source Location

Signature

def create_meshgrid(
    height: int,
    width: int,
    normalized_coordinates: bool = True,
    device: Optional[torch.device] = None,
    dtype: Optional[torch.dtype] = None,
) -> torch.Tensor

def create_meshgrid3d(
    depth: int,
    height: int,
    width: int,
    normalized_coordinates: bool = True,
    device: Optional[torch.device] = None,
    dtype: Optional[torch.dtype] = None,
) -> torch.Tensor

Import

from kornia.geometry.grid import create_meshgrid, create_meshgrid3d

I/O Contract

Inputs (create_meshgrid)

Name Type Required Description
height int Yes Image height (number of rows)
width int Yes Image width (number of columns)
normalized_coordinates bool No If True, coordinates are in [-1, 1] range for grid_sample compatibility. Default: True
device torch.device No Device to place the grid on. Default: None (CPU)
dtype torch.dtype No Data type of the grid tensor. Default: None

Outputs (create_meshgrid)

Name Type Description
grid torch.Tensor Coordinate grid of shape (1, H, W, 2) with (x, y) coordinates

Inputs (create_meshgrid3d)

Name Type Required Description
depth int Yes Volume depth (number of channels/slices)
height int Yes Image height (number of rows)
width int Yes Image width (number of columns)
normalized_coordinates bool No If True, coordinates are in [-1, 1] range. Default: True
device torch.device No Device to place the grid on. Default: None
dtype torch.dtype No Data type of the grid tensor. Default: None

Outputs (create_meshgrid3d)

Name Type Description
grid torch.Tensor Coordinate grid of shape (1, D, H, W, 3) with (x, y, z) coordinates

Usage Examples

import torch
from kornia.geometry.grid import create_meshgrid, create_meshgrid3d

# Create a 2D normalized meshgrid (for grid_sample)
grid_norm = create_meshgrid(2, 2)
# tensor([[[[-1., -1.],
#           [ 1., -1.]],
#          [[-1.,  1.],
#           [ 1.,  1.]]]])

# Create a 2D pixel-coordinate meshgrid
grid_pixel = create_meshgrid(2, 2, normalized_coordinates=False)
# tensor([[[[0., 0.],
#           [1., 0.]],
#          [[0., 1.],
#           [1., 1.]]]])

# Create a 3D meshgrid for volumetric data
grid_3d = create_meshgrid3d(depth=4, height=8, width=8)
# grid_3d.shape: (1, 4, 8, 8, 3)

# Use with grid_sample for image warping
image = torch.rand(1, 3, 64, 64)
grid = create_meshgrid(64, 64, normalized_coordinates=True)
warped = torch.nn.functional.grid_sample(image, grid, align_corners=True)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment