kornia.geometry.conversions¶
-
rad2deg
(tensor: torch.Tensor) → torch.Tensor[source]¶ Function that converts angles from radians to degrees.
- Parameters
tensor (torch.Tensor) – Tensor of arbitrary shape.
- Returns
Tensor with same shape as input.
- Return type
Example
>>> input = kornia.pi * torch.rand(1, 3, 3) >>> output = kornia.rad2deg(input)
-
deg2rad
(tensor: torch.Tensor) → torch.Tensor[source]¶ Function that converts angles from degrees to radians.
- Parameters
tensor (torch.Tensor) – Tensor of arbitrary shape.
- Returns
tensor with same shape as input.
- Return type
Examples:
>>> input = 360. * torch.rand(1, 3, 3) >>> output = kornia.deg2rad(input)
-
convert_points_from_homogeneous
(points: torch.Tensor, eps: float = 1e-08) → torch.Tensor[source]¶ Function that converts points from homogeneous to Euclidean space.
Examples:
>>> input = torch.rand(2, 4, 3) # BxNx3 >>> output = kornia.convert_points_from_homogeneous(input) # BxNx2
-
convert_points_to_homogeneous
(points: torch.Tensor) → torch.Tensor[source]¶ Function that converts points from Euclidean to homogeneous space.
Examples:
>>> input = torch.rand(2, 4, 3) # BxNx3 >>> output = kornia.convert_points_to_homogeneous(input) # BxNx4
-
rotation_matrix_to_angle_axis
(rotation_matrix: torch.Tensor) → torch.Tensor[source]¶ Convert 3x3 rotation matrix to Rodrigues vector.
- Parameters
rotation_matrix (torch.Tensor) – rotation matrix.
- Returns
Rodrigues vector transformation.
- Return type
- Shape:
Input: \((N, 3, 3)\)
Output: \((N, 3)\)
Example
>>> input = torch.rand(2, 3, 3) # Nx3x3 >>> output = kornia.rotation_matrix_to_angle_axis(input) # Nx3
-
rotation_matrix_to_quaternion
(rotation_matrix: torch.Tensor, eps: float = 1e-08) → torch.Tensor[source]¶ Convert 3x3 rotation matrix to 4d quaternion vector. The quaternion vector has components in (x, y, z, w) format.
- Parameters
rotation_matrix (torch.Tensor) – the rotation matrix to convert.
eps (float) – small value to avoid zero division. Default: 1e-8.
- Returns
the rotation in quaternion.
- Return type
- Shape:
Input: \((*, 3, 3)\)
Output: \((*, 4)\)
Example
>>> input = torch.rand(4, 3, 3) # Nx3x3 >>> output = kornia.rotation_matrix_to_quaternion(input) # Nx4
-
quaternion_to_angle_axis
(quaternion: torch.Tensor) → torch.Tensor[source]¶ Convert quaternion vector to angle axis of rotation. The quaternion should be in (x, y, z, w) format.
Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h
- Parameters
quaternion (torch.Tensor) – tensor with quaternions.
- Returns
tensor with angle axis of rotation.
- Return type
- Shape:
Input: \((*, 4)\) where * means, any number of dimensions
Output: \((*, 3)\)
Example
>>> quaternion = torch.rand(2, 4) # Nx4 >>> angle_axis = kornia.quaternion_to_angle_axis(quaternion) # Nx3
-
quaternion_to_rotation_matrix
(quaternion: torch.Tensor) → torch.Tensor[source]¶ Converts a quaternion to a rotation matrix. The quaternion should be in (x, y, z, w) format.
- Parameters
quaternion (torch.Tensor) – a tensor containing a quaternion to be converted. The tensor can be of shape \((*, 4)\).
- Returns
the rotation matrix of shape \((*, 3, 3)\).
- Return type
Example
>>> quaternion = torch.tensor([0., 0., 1., 0.]) >>> kornia.quaternion_to_rotation_matrix(quaternion) tensor([[[-1., 0., 0.], [ 0., -1., 0.], [ 0., 0., 1.]]])
-
quaternion_log_to_exp
(quaternion: torch.Tensor, eps: float = 1e-08) → torch.Tensor[source]¶ Applies exponential map to log quaternion. The quaternion should be in (x, y, z, w) format.
- Parameters
quaternion (torch.Tensor) – a tensor containing a quaternion to be converted. The tensor can be of shape \((*, 3)\).
- Returns
the quaternion exponential map of shape \((*, 4)\).
- Return type
Example
>>> quaternion = torch.tensor([0., 0., 0.]) >>> kornia.quaternion_log_to_exp(quaternion) tensor([0., 0., 0., 1.])
-
quaternion_exp_to_log
(quaternion: torch.Tensor, eps: float = 1e-08) → torch.Tensor[source]¶ Applies the log map to a quaternion. The quaternion should be in (x, y, z, w) format.
- Parameters
quaternion (torch.Tensor) – a tensor containing a quaternion to be converted. The tensor can be of shape \((*, 4)\).
- Returns
the quaternion log map of shape \((*, 3)\).
- Return type
Example
>>> quaternion = torch.tensor([0., 0., 0., 1.]) >>> kornia.quaternion_exp_to_log(quaternion) tensor([0., 0., 0.])
-
angle_axis_to_quaternion
(angle_axis: torch.Tensor) → torch.Tensor[source]¶ Convert an angle axis to a quaternion. The quaternion vector has components in (x, y, z, w) format.
Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h
- Parameters
angle_axis (torch.Tensor) – tensor with angle axis.
- Returns
tensor with quaternion.
- Return type
- Shape:
Input: \((*, 3)\) where * means, any number of dimensions
Output: \((*, 4)\)
Example
>>> angle_axis = torch.rand(2, 4) # Nx4 >>> quaternion = kornia.angle_axis_to_quaternion(angle_axis) # Nx3
-
angle_axis_to_rotation_matrix
(angle_axis: torch.Tensor) → torch.Tensor[source]¶ Convert 3d vector of axis-angle rotation to 3x3 rotation matrix
- Parameters
angle_axis (torch.Tensor) – tensor of 3d vector of axis-angle rotations.
- Returns
tensor of 3x3 rotation matrices.
- Return type
- Shape:
Input: \((N, 3)\)
Output: \((N, 3, 3)\)
Example
>>> input = torch.rand(1, 3) # Nx3 >>> output = kornia.angle_axis_to_rotation_matrix(input) # Nx3x3
-
denormalize_pixel_coordinates
(pixel_coordinates: torch.Tensor, height: int, width: int, eps: float = 1e-08) → torch.Tensor[source]¶ Denormalize pixel coordinates.
The input is assumed to be -1 if on extreme left, 1 if on extreme right (x = w-1).
- Parameters
pixel_coordinates (torch.Tensor) – the normalized grid coordinates. Shape can be \((*, 2)\).
width (int) – the maximum width in the x-axis.
height (int) – the maximum height in the y-axis.
eps (float) – safe division by zero. (default 1e-8).
- Returns
the denormalized pixel coordinates.
- Return type
-
normalize_pixel_coordinates
(pixel_coordinates: torch.Tensor, height: int, width: int, eps: float = 1e-08) → torch.Tensor[source]¶ Normalize pixel coordinates between -1 and 1.
Normalized, -1 if on extreme left, 1 if on extreme right (x = w-1).
- Parameters
pixel_coordinates (torch.Tensor) – the grid with pixel coordinates. Shape can be \((*, 2)\).
width (int) – the maximum width in the x-axis.
height (int) – the maximum height in the y-axis.
eps (float) – safe division by zero. (default 1e-8).
- Returns
the normalized pixel coordinates.
- Return type
-
denormalize_pixel_coordinates3d
(pixel_coordinates: torch.Tensor, depth: int, height: int, width: int, eps: float = 1e-08) → torch.Tensor[source]¶ Denormalize pixel coordinates.
The input is assumed to be -1 if on extreme left, 1 if on extreme right (x = w-1).
- Parameters
pixel_coordinates (torch.Tensor) – the normalized grid coordinates. Shape can be \((*, 3)\).
depth (int) – the maximum depth in the x-axis.
height (int) – the maximum height in the y-axis.
width (int) – the maximum width in the x-axis.
eps (float) – safe division by zero. (default 1e-8).
- Returns
the denormalized pixel coordinates.
- Return type
-
normalize_pixel_coordinates3d
(pixel_coordinates: torch.Tensor, depth: int, height: int, width: int, eps: float = 1e-08) → torch.Tensor[source]¶ Normalize pixel coordinates between -1 and 1.
Normalized, -1 if on extreme left, 1 if on extreme right (x = w-1).
- Parameters
pixel_coordinates (torch.Tensor) – the grid with pixel coordinates. Shape can be \((*, 3)\).
depth (int) – the maximum depth in the z-axis.
height (int) – the maximum height in the y-axis.
width (int) – the maximum width in the x-axis.
eps (float) – safe division by zero. (default 1e-8).
- Returns
the normalized pixel coordinates.
- Return type
-
normalize_quaternion
(quaternion: torch.Tensor, eps: float = 1e-12) → torch.Tensor[source]¶ Normalizes a quaternion. The quaternion should be in (x, y, z, w) format.
- Parameters
quaternion (torch.Tensor) – a tensor containing a quaternion to be normalized. The tensor can be of shape \((*, 4)\).
eps (Optional[bool]) – small value to avoid division by zero. Default: 1e-12.
- Returns
the normalized quaternion of shape \((*, 4)\).
- Return type
Example
>>> quaternion = torch.tensor([1., 0., 1., 0.]) >>> kornia.normalize_quaternion(quaternion) tensor([0.7071, 0.0000, 0.7071, 0.0000])