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 Geometry Conversions

From Leeroopedia


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

Overview

Provides a comprehensive library of differentiable geometric conversion functions for angles, coordinate systems, rotation representations, quaternions, homographies, pixel normalization, and camera frame transformations.

Description

The conversions.py module in the Kornia geometry library is the central hub for converting between different geometric representations using PyTorch tensors. It includes functions for angle unit conversion (radians/degrees), coordinate system conversion (polar/cartesian, homogeneous/Euclidean), rotation representation conversion (axis-angle, quaternion, rotation matrix, Euler angles), homography normalization and denormalization, pixel coordinate normalization (for use with torch.nn.functional.grid_sample), camera intrinsics-based point normalization, camera frame conversions (graphics/vision conventions, world-to-cam/cam-to-world, ARKit-to-Colmap), and matrix conversions (4x4 extrinsics to/from R+t, affine to homography, skew-symmetric matrices). All functions are differentiable and support batched operations.

Deprecation Notice: The following legacy aliases are deprecated since v0.7.0 and will be removed in a future release. Use the modern names instead:

  • angle_axis_to_rotation_matrix → use axis_angle_to_rotation_matrix
  • rotation_matrix_to_angle_axis → use rotation_matrix_to_axis_angle
  • quaternion_to_angle_axis → use quaternion_to_axis_angle
  • angle_axis_to_quaternion → use axis_angle_to_quaternion

Usage

Import these functions whenever you need to convert between geometric representations in a differentiable manner -- for example, converting quaternions to rotation matrices for neural network-based pose estimation, normalizing pixel coordinates for spatial transformers, or converting between camera coordinate conventions.

Code Reference

Source Location

Signature

# Angle conversions
def rad2deg(tensor: torch.Tensor) -> torch.Tensor
def deg2rad(tensor: torch.Tensor) -> torch.Tensor

# Coordinate conversions
def pol2cart(rho: torch.Tensor, phi: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]
def cart2pol(x: torch.Tensor, y: torch.Tensor, eps: float = 1e-8) -> tuple[torch.Tensor, torch.Tensor]
def convert_points_from_homogeneous(points: torch.Tensor, eps: float = 1e-8) -> torch.Tensor
def convert_points_to_homogeneous(points: torch.Tensor) -> torch.Tensor

# Rotation representation conversions
def axis_angle_to_rotation_matrix(axis_angle: torch.Tensor) -> torch.Tensor
def rotation_matrix_to_axis_angle(rotation_matrix: torch.Tensor) -> torch.Tensor
def rotation_matrix_to_quaternion(rotation_matrix: torch.Tensor, eps: float = 1e-8) -> torch.Tensor
def quaternion_to_rotation_matrix(quaternion: torch.Tensor) -> torch.Tensor
def quaternion_to_axis_angle(quaternion: torch.Tensor) -> torch.Tensor
def axis_angle_to_quaternion(axis_angle: torch.Tensor) -> torch.Tensor
def normalize_quaternion(quaternion: torch.Tensor, eps: float = 1e-12) -> torch.Tensor
def quaternion_log_to_exp(quaternion: torch.Tensor, eps: float = 1e-8) -> torch.Tensor
def quaternion_exp_to_log(quaternion: torch.Tensor, eps: float = 1e-8) -> torch.Tensor

# Euler angle conversions
def euler_from_quaternion(w, x, y, z) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]
def quaternion_from_euler(roll, pitch, yaw) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]

# Pixel coordinate normalization
def normalize_pixel_coordinates(pixel_coordinates: torch.Tensor, height: int, width: int, eps: float = 1e-8) -> torch.Tensor
def denormalize_pixel_coordinates(pixel_coordinates: torch.Tensor, height: int, width: int, eps: float = 1e-8) -> torch.Tensor

# Homography normalization
def normalize_homography(dst_pix_trans_src_pix: torch.Tensor, dsize_src: tuple, dsize_dst: tuple) -> torch.Tensor
def denormalize_homography(dst_pix_trans_src_pix: torch.Tensor, dsize_src: tuple, dsize_dst: tuple) -> torch.Tensor

# Affine matrix conversions
def convert_affinematrix_to_homography(A: torch.Tensor) -> torch.Tensor
def convert_affinematrix_to_homography3d(A: torch.Tensor) -> torch.Tensor

# 2D rotation
def angle_to_rotation_matrix(angle: torch.Tensor) -> torch.Tensor

# Camera frame conversions
def Rt_to_matrix4x4(R: torch.Tensor, t: torch.Tensor) -> torch.Tensor
def matrix4x4_to_Rt(extrinsics: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]
def camtoworld_graphics_to_vision_4x4(extrinsics_graphics: torch.Tensor) -> torch.Tensor
def camtoworld_vision_to_graphics_4x4(extrinsics_vision: torch.Tensor) -> torch.Tensor
def camtoworld_to_worldtocam_Rt(R: torch.Tensor, t: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]
def worldtocam_to_camtoworld_Rt(R: torch.Tensor, t: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]
def ARKitQTVecs_to_ColmapQTVecs(qvec: torch.Tensor, tvec: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]

# Skew-symmetric matrix
def vector_to_skew_symmetric_matrix(vec: torch.Tensor) -> torch.Tensor

# Point normalization with intrinsics
def normalize_points_with_intrinsics(point_2d: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tensor
def denormalize_points_with_intrinsics(point_2d_norm: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tensor

Import

from kornia.geometry.conversions import (
    rad2deg, deg2rad, pol2cart, cart2pol,
    convert_points_from_homogeneous, convert_points_to_homogeneous,
    axis_angle_to_rotation_matrix, rotation_matrix_to_quaternion,
    quaternion_to_rotation_matrix, normalize_pixel_coordinates,
    Rt_to_matrix4x4, matrix4x4_to_Rt,
)

I/O Contract

Inputs (axis_angle_to_rotation_matrix)

Name Type Required Description
axis_angle torch.Tensor Yes Axis-angle rotation vectors of shape (N, 3) in radians

Outputs (axis_angle_to_rotation_matrix)

Name Type Description
rotation_matrix torch.Tensor Rotation matrices of shape (N, 3, 3)

Inputs (rotation_matrix_to_quaternion)

Name Type Required Description
rotation_matrix torch.Tensor Yes Rotation matrices of shape (*, 3, 3)
eps float No Small value to avoid zero division. Default: 1e-8

Outputs (rotation_matrix_to_quaternion)

Name Type Description
quaternion torch.Tensor Quaternion in (w, x, y, z) format with shape (*, 4)

Inputs (normalize_pixel_coordinates)

Name Type Required Description
pixel_coordinates torch.Tensor Yes Grid of pixel coordinates, shape (*, 2)
height int Yes Maximum height in y-axis
width int Yes Maximum width in x-axis
eps float No Safe division epsilon. Default: 1e-8

Outputs (normalize_pixel_coordinates)

Name Type Description
normalized torch.Tensor Normalized coordinates in [-1, 1] range, shape (*, 2)

Usage Examples

import torch
from kornia.geometry.conversions import (
    rad2deg, deg2rad,
    axis_angle_to_rotation_matrix,
    rotation_matrix_to_quaternion,
    quaternion_to_rotation_matrix,
    normalize_pixel_coordinates,
    Rt_to_matrix4x4,
)

# Angle conversions
angle_rad = torch.tensor(3.1416)
angle_deg = rad2deg(angle_rad)  # tensor(180.)

# Axis-angle to rotation matrix
axis_angle = torch.tensor([[1.5708, 0., 0.]])
R = axis_angle_to_rotation_matrix(axis_angle)  # (1, 3, 3)

# Rotation matrix to quaternion
q = rotation_matrix_to_quaternion(R)  # (1, 4) in (w, x, y, z) format

# Quaternion back to rotation matrix
R_back = quaternion_to_rotation_matrix(q)  # (1, 3, 3)

# Normalize pixel coordinates for grid_sample
coords = torch.tensor([[50., 100.]])
norm_coords = normalize_pixel_coordinates(coords, height=100, width=50)

# Compose R and t into 4x4 matrix
R_eye = torch.eye(3).unsqueeze(0)
t = torch.ones(1, 3, 1)
extrinsics = Rt_to_matrix4x4(R_eye, t)  # (1, 4, 4)

Related Pages

Page Connections

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