# kornia.geometry.conversions¶

## Angles¶

Function that converts angles from radians to degrees.

Parameters

tensor (Tensor) – Tensor of arbitrary shape.

Return type

Tensor

Returns

Tensor with same shape as input.

Example

>>> input = torch.tensor(3.1415926535)
tensor(180.)


Function that converts angles from degrees to radians.

Parameters

tensor (Tensor) – Tensor of arbitrary shape.

Return type

Tensor

Returns

tensor with same shape as input.

Examples

>>> input = torch.tensor(180.)
tensor(3.1416)

kornia.geometry.conversions.pol2cart(rho, phi)[source]

Function that converts polar coordinates to cartesian coordinates.

Parameters
Return type
Returns

Tensor with same shape as input.

Example

>>> rho = torch.rand(1, 3, 3)
>>> phi = torch.rand(1, 3, 3)
>>> x, y = pol2cart(rho, phi)

kornia.geometry.conversions.cart2pol(x, y, eps=1e-08)[source]

Function that converts cartesian coordinates to polar coordinates.

Parameters
• rho – Tensor of arbitrary shape.

• phi – Tensor of same arbitrary shape.

• eps (float, optional) – To avoid division by zero. Default: 1e-08

Return type
Returns

Tensor with same shape as input.

Example

>>> x = torch.rand(1, 3, 3)
>>> y = torch.rand(1, 3, 3)
>>> rho, phi = cart2pol(x, y)


## Coordinates¶

kornia.geometry.conversions.convert_points_from_homogeneous(points, eps=1e-08)[source]

Function that converts points from homogeneous to Euclidean space.

Parameters
Return type

Tensor

Returns

the points in Euclidean space $$(B, N, D-1)$$.

Examples

>>> input = torch.tensor([[0., 0., 1.]])
>>> convert_points_from_homogeneous(input)
tensor([[0., 0.]])

kornia.geometry.conversions.convert_points_to_homogeneous(points)[source]

Function that converts points from Euclidean to homogeneous space.

Parameters

points (Tensor) – the points to be transformed with shape $$(B, N, D)$$.

Return type

Tensor

Returns

the points in homogeneous coordinates $$(B, N, D+1)$$.

Examples

>>> input = torch.tensor([[0., 0.]])
>>> convert_points_to_homogeneous(input)
tensor([[0., 0., 1.]])

kornia.geometry.conversions.convert_affinematrix_to_homography(A)[source]

Function that converts batch of affine matrices.

Parameters

A (Tensor) – the affine matrix with shape $$(B,2,3)$$.

Return type

Tensor

Returns

the homography matrix with shape of $$(B,3,3)$$.

Examples

>>> A = torch.tensor([[[1., 0., 0.],
...                    [0., 1., 0.]]])
>>> convert_affinematrix_to_homography(A)
tensor([[[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]])

kornia.geometry.conversions.denormalize_pixel_coordinates(pixel_coordinates, height, width, eps=1e-08)[source]

Denormalize pixel coordinates.

The input is assumed to be -1 if on extreme left, 1 if on extreme right (x = w-1).

Parameters
Return type

Tensor

Returns

the denormalized pixel coordinates with shape $$(*, 2)$$.

Examples

>>> coords = torch.tensor([[-1., -1.]])
>>> denormalize_pixel_coordinates(coords, 100, 50)
tensor([[0., 0.]])

kornia.geometry.conversions.normalize_pixel_coordinates(pixel_coordinates, height, width, eps=1e-08)[source]

Normalize pixel coordinates between -1 and 1.

Normalized, -1 if on extreme left, 1 if on extreme right (x = w-1).

Parameters
Return type

Tensor

Returns

the normalized pixel coordinates with shape $$(*, 2)$$.

Examples

>>> coords = torch.tensor([[50., 100.]])
>>> normalize_pixel_coordinates(coords, 100, 50)
tensor([[1.0408, 1.0202]])

kornia.geometry.conversions.denormalize_pixel_coordinates3d(pixel_coordinates, depth, height, width, eps=1e-08)[source]

Denormalize pixel coordinates.

The input is assumed to be -1 if on extreme left, 1 if on extreme right (x = w-1).

Parameters
Return type

Tensor

Returns

the denormalized pixel coordinates.

kornia.geometry.conversions.normalize_pixel_coordinates3d(pixel_coordinates, depth, height, width, eps=1e-08)[source]

Normalize pixel coordinates between -1 and 1.

Normalized, -1 if on extreme left, 1 if on extreme right (x = w-1).

Parameters
Return type

Tensor

Returns

the normalized pixel coordinates.

## Quaternion¶

kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.XYZW)[source]

Convert quaternion vector to angle axis of rotation in radians.

The quaternion should be in (x, y, z, w) or (w, x, y, z) format.

Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h

Parameters
• quaternion (Tensor) – tensor with quaternions.

• order (QuaternionCoeffOrder, optional) – quaternion coefficient order. Note: ‘xyzw’ will be deprecated in favor of ‘wxyz’. Default: QuaternionCoeffOrder.XYZW

Return type

Tensor

Returns

tensor with angle axis of rotation.

Shape:
• Input: $$(*, 4)$$ where * means, any number of dimensions

• Output: $$(*, 3)$$

Example

>>> quaternion = torch.tensor((1., 0., 0., 0.))
>>> quaternion_to_angle_axis(quaternion)
tensor([3.1416, 0.0000, 0.0000])

kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.XYZW)[source]

Convert a quaternion to a rotation matrix.

The quaternion should be in (x, y, z, w) or (w, x, y, z) format.

Parameters
• quaternion (Tensor) – a tensor containing a quaternion to be converted. The tensor can be of shape $$(*, 4)$$.

• order (QuaternionCoeffOrder, optional) – quaternion coefficient order. Note: ‘xyzw’ will be deprecated in favor of ‘wxyz’. Default: QuaternionCoeffOrder.XYZW

Return type

Tensor

Returns

the rotation matrix of shape $$(*, 3, 3)$$.

Example

>>> quaternion = torch.tensor((0., 0., 0., 1.))
>>> quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
tensor([[-1.,  0.,  0.],
[ 0., -1.,  0.],
[ 0.,  0.,  1.]])

kornia.geometry.conversions.quaternion_log_to_exp(quaternion, eps=1e-08, order=QuaternionCoeffOrder.XYZW)[source]

Apply exponential map to log quaternion.

The quaternion should be in (x, y, z, w) or (w, x, y, z) format.

Parameters
• quaternion (Tensor) – a tensor containing a quaternion to be converted. The tensor can be of shape $$(*, 3)$$.

• eps (float, optional) – a small number for clamping. Default: 1e-08

• order (QuaternionCoeffOrder, optional) – quaternion coefficient order. Note: ‘xyzw’ will be deprecated in favor of ‘wxyz’. Default: QuaternionCoeffOrder.XYZW

Return type

Tensor

Returns

the quaternion exponential map of shape $$(*, 4)$$.

Example

>>> quaternion = torch.tensor((0., 0., 0.))
>>> quaternion_log_to_exp(quaternion, eps=torch.finfo(quaternion.dtype).eps,
...                       order=QuaternionCoeffOrder.WXYZ)
tensor([1., 0., 0., 0.])

kornia.geometry.conversions.quaternion_exp_to_log(quaternion, eps=1e-08, order=QuaternionCoeffOrder.XYZW)[source]

Apply the log map to a quaternion.

The quaternion should be in (x, y, z, w) format.

Parameters
• quaternion (Tensor) – a tensor containing a quaternion to be converted. The tensor can be of shape $$(*, 4)$$.

• eps (float, optional) – a small number for clamping. Default: 1e-08

• order (QuaternionCoeffOrder, optional) – quaternion coefficient order. Note: ‘xyzw’ will be deprecated in favor of ‘wxyz’. Default: QuaternionCoeffOrder.XYZW

Return type

Tensor

Returns

the quaternion log map of shape $$(*, 3)$$.

Example

>>> quaternion = torch.tensor((1., 0., 0., 0.))
>>> quaternion_exp_to_log(quaternion, eps=torch.finfo(quaternion.dtype).eps,
...                       order=QuaternionCoeffOrder.WXYZ)
tensor([0., 0., 0.])

kornia.geometry.conversions.normalize_quaternion(quaternion, eps=1e-12)[source]

Normalize a quaternion.

The quaternion should be in (x, y, z, w) or (w, x, y, z) format.

Parameters
• quaternion (Tensor) – a tensor containing a quaternion to be normalized. The tensor can be of shape $$(*, 4)$$.

• eps (float, optional) – small value to avoid division by zero. Default: 1e-12

Return type

Tensor

Returns

the normalized quaternion of shape $$(*, 4)$$.

Example

>>> quaternion = torch.tensor((1., 0., 1., 0.))
>>> normalize_quaternion(quaternion)
tensor([0.7071, 0.0000, 0.7071, 0.0000])


## Rotation Matrix¶

kornia.geometry.conversions.rotation_matrix_to_angle_axis(rotation_matrix)[source]

Convert 3x3 rotation matrix to Rodrigues vector in radians.

Parameters

rotation_matrix (Tensor) – rotation matrix of shape $$(N, 3, 3)$$.

Return type

Tensor

Returns

Rodrigues vector transformation of shape $$(N, 3)$$.

Example

>>> input = torch.tensor([[1., 0., 0.],
...                       [0., 1., 0.],
...                       [0., 0., 1.]])
>>> rotation_matrix_to_angle_axis(input)
tensor([0., 0., 0.])

>>> input = torch.tensor([[1., 0., 0.],
...                       [0., 0., -1.],
...                       [0., 1., 0.]])
>>> rotation_matrix_to_angle_axis(input)
tensor([1.5708, 0.0000, 0.0000])

kornia.geometry.conversions.rotation_matrix_to_quaternion(rotation_matrix, eps=1e-08, order=QuaternionCoeffOrder.XYZW)[source]

Convert 3x3 rotation matrix to 4d quaternion vector.

The quaternion vector has components in (w, x, y, z) or (x, y, z, w) format.

Note

The (x, y, z, w) order is going to be deprecated in favor of efficiency.

Parameters
• rotation_matrix (Tensor) – the rotation matrix to convert with shape $$(*, 3, 3)$$.

• eps (float, optional) – small value to avoid zero division. Default: 1e-08

• order (QuaternionCoeffOrder, optional) – quaternion coefficient order. Note: ‘xyzw’ will be deprecated in favor of ‘wxyz’. Default: QuaternionCoeffOrder.XYZW

Return type

Tensor

Returns

the rotation in quaternion with shape $$(*, 4)$$.

Example

>>> input = torch.tensor([[1., 0., 0.],
...                       [0., 1., 0.],
...                       [0., 0., 1.]])
>>> rotation_matrix_to_quaternion(input, eps=torch.finfo(input.dtype).eps,
...                               order=QuaternionCoeffOrder.WXYZ)
tensor([1., 0., 0., 0.])


## Angle Axis¶

kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.XYZW)[source]

Convert an angle axis to a quaternion.

The quaternion vector has components in (x, y, z, w) or (w, x, y, z) format.

Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h

Parameters
• angle_axis (Tensor) – tensor with angle axis in radians.

• order (QuaternionCoeffOrder, optional) – quaternion coefficient order. Note: ‘xyzw’ will be deprecated in favor of ‘wxyz’. Default: QuaternionCoeffOrder.XYZW

Return type

Tensor

Returns

tensor with quaternion.

Shape:
• Input: $$(*, 3)$$ where * means, any number of dimensions

• Output: $$(*, 4)$$

Example

>>> angle_axis = torch.tensor((0., 1., 0.))
>>> angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
tensor([0.8776, 0.0000, 0.4794, 0.0000])

kornia.geometry.conversions.angle_axis_to_rotation_matrix(angle_axis)[source]

Convert 3d vector of axis-angle rotation to 3x3 rotation matrix.

Parameters

angle_axis (Tensor) – tensor of 3d vector of axis-angle rotations in radians with shape $$(N, 3)$$.

Return type

Tensor

Returns

tensor of rotation matrices of shape $$(N, 3, 3)$$.

Example

>>> input = torch.tensor([[0., 0., 0.]])
>>> angle_axis_to_rotation_matrix(input)
tensor([[[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]])

>>> input = torch.tensor([[1.5708, 0., 0.]])
>>> angle_axis_to_rotation_matrix(input)
tensor([[[ 1.0000e+00,  0.0000e+00,  0.0000e+00],
[ 0.0000e+00, -3.6200e-06, -1.0000e+00],
[ 0.0000e+00,  1.0000e+00, -3.6200e-06]]])