ct.convert

Functions for converting between different camera parameters and representations.

ct.convert.pad_0001(array)[source]

Pad [0, 0, 0, 1] to the bottom row.

Parameters:

array – (3, 4) or (N, 3, 4).

Returns:

Array of shape (4, 4) or (N, 4, 4).

ct.convert.rm_pad_0001(array, check_vals=False)[source]

Remove the bottom row of [0, 0, 0, 1].

Parameters:
  • array – (4, 4) or (N, 4, 4).

  • check_vals (bool) – If True, check that the bottom row is [0, 0, 0, 1].

Returns:

Array of shape (3, 4) or (N, 3, 4).

ct.convert.to_homo(array)[source]

Convert a 2D array to homogeneous coordinates by appending a column of ones.

Parameters:

array – A 2D numpy array of shape (N, M).

Returns:

A numpy array of shape (N, M+1) with a column of ones appended.

ct.convert.from_homo(array)[source]

Convert an array from homogeneous to Cartesian coordinates by dividing by the last column and removing it.

Parameters:

array – A 2D numpy array of shape (N, M) in homogeneous coordinates.

Returns:

A numpy array of shape (N, M-1) in Cartesian coordinates.

ct.convert.R_to_quat(R)[source]

Convert rotation matrix to quaternion.

Parameters:

R – Rotation matrix of shape (3, 3) or (N, 3, 3).

Returns:

Quaternion of shape (4,) or (N, 4). The quaternion is normalized.

ct.convert.T_to_C(T)[source]

Convert extrinsic matrix T to camera center C.

Parameters:

T (Float[ndarray, '4 4']) – Extrinsic matrix (world-to-camera) of shape (4, 4).

Returns:

Camera center in world coordinates of shape (3,).

Return type:

Float[ndarray, ‘3’]

ct.convert.pose_to_C(pose)[source]

Convert pose matrix to camera center C.

Parameters:

pose (Float[ndarray, '4 4']) – Pose matrix (camera-to-world) of shape (4, 4).

Returns:

Camera center in world coordinates of shape (3,).

Return type:

Float[ndarray, ‘3’]

ct.convert.T_to_pose(T)[source]

Convert extrinsic matrix T to pose matrix.

Parameters:

T – Extrinsic matrix (world-to-camera) of shape (4, 4).

Returns:

Pose matrix (camera-to-world) of shape (4, 4), which is the inverse of T.

ct.convert.pose_to_T(pose)[source]

Convert pose matrix to extrinsic matrix T.

Parameters:

pose – Pose matrix (camera-to-world) of shape (4, 4).

Returns:

Extrinsic matrix (world-to-camera) of shape (4, 4), which is the inverse of pose.

ct.convert.T_opengl_to_opencv(T)[source]

Convert T from OpenGL convention to OpenCV convention.

Parameters:

T (Float[ndarray, '4 4']) – Extrinsic matrix (world-to-camera) of shape (4, 4) in OpenCV convention.

Returns:

Extrinsic matrix (world-to-camera) of shape (4, 4) in OpenGL convention.

Return type:

Float[ndarray, ‘4 4’]

ct.convert.T_opencv_to_opengl(T)[source]

Convert T from OpenCV convention to OpenGL convention.

Parameters:

T (Float[ndarray, '4 4']) – Extrinsic matrix (world-to-camera) of shape (4, 4) in OpenCV convention.

Returns:

Extrinsic matrix (world-to-camera) of shape (4, 4) in OpenGL convention.

Return type:

Float[ndarray, ‘4 4’]

ct.convert.pose_opengl_to_opencv(pose)[source]

Convert pose from OpenGL convention to OpenCV convention.

Parameters:

pose (Float[ndarray, '4 4']) – Pose matrix (camera-to-world) of shape (4, 4) in OpenGL convention.

Returns:

Pose matrix (camera-to-world) of shape (4, 4) in OpenCV convention.

Return type:

Float[ndarray, ‘4 4’]

ct.convert.pose_opencv_to_opengl(pose)[source]

Convert pose from OpenCV convention to OpenGL convention.

Parameters:

pose (Float[ndarray, '4 4']) – Pose matrix (camera-to-world) of shape (4, 4) in OpenCV convention.

Returns:

Pose matrix (camera-to-world) of shape (4, 4) in OpenGL convention.

Return type:

Float[ndarray, ‘4 4’]

ct.convert.R_t_to_C(R, t)[source]

Convert rotation matrix R and translation vector t to camera center C.

Parameters:
  • R (Float[ndarray, '3 3']) – Rotation matrix of shape (3, 3).

  • t (Float[ndarray, '3']) – Translation vector of shape (3,).

Returns:

Camera center in world coordinates of shape (3,).

Return type:

Float[ndarray, ‘3’]

ct.convert.R_C_to_t(R, C)[source]

Convert rotation matrix R and camera center C to translation vector t.

Parameters:
  • R (Float[ndarray, '3 3']) – Rotation matrix of shape (3, 3) or (N, 3, 3).

  • C (Float[ndarray, '3']) – Camera center in world coordinates of shape (3,) or (N, 3).

Returns:

Translation vector of shape (3,) or (N, 3).

Return type:

Float[ndarray, ‘3’]

ct.convert.roll_pitch_yaw_to_R(roll, pitch, yaw)[source]

Convert roll, pitch, and yaw angles to a rotation matrix.

Parameters:
  • roll (float) – Rotation around the x-axis in radians.

  • pitch (float) – Rotation around the y-axis in radians.

  • yaw (float) – Rotation around the z-axis in radians.

Returns:

Rotation matrix of shape (3, 3).

Return type:

Float[ndarray, ‘3 3’]

ct.convert.R_t_to_T(R, t)[source]

Convert rotation matrix R and translation vector t to extrinsic matrix T.

Parameters:
  • R (Float[ndarray, '3 3']) – Rotation matrix of shape (3, 3).

  • t (Float[ndarray, '3']) – Translation vector of shape (3,).

Returns:

Extrinsic matrix (world-to-camera) of shape (4, 4).

Return type:

Float[ndarray, ‘4 4’]

ct.convert.T_to_R_t(T)[source]

Decompose extrinsic matrix T into rotation matrix R and translation vector t.

Parameters:

T (Float[ndarray, '4 4']) – Extrinsic matrix (world-to-camera) of shape (4, 4).

Returns:

  • R: Rotation matrix of shape (3, 3)

  • t: Translation vector of shape (3,)

Return type:

Tuple[Float[np.ndarray, “3 3”], Float[np.ndarray, “3”]]

ct.convert.P_to_K_R_t(P)[source]

Decompose projection matrix P into intrinsic matrix K, rotation matrix R, and translation vector t.

Parameters:

P (Float[ndarray, '3 4']) – Projection matrix of shape (3, 4).

Returns:

  • K: Intrinsic matrix of shape (3, 3)

  • R: Rotation matrix of shape (3, 3)

  • t: Translation vector of shape (3,)

Return type:

Tuple[Float[np.ndarray, “3 3”], Float[np.ndarray, “3 3”], Float[np.ndarray, “3”]]

ct.convert.P_to_K_T(P)[source]

Decompose projection matrix P into intrinsic matrix K and extrinsic matrix T.

Parameters:

P (Float[ndarray, '3 4']) – Projection matrix of shape (3, 4).

Returns:

  • K: Intrinsic matrix of shape (3, 3)

  • T: Extrinsic matrix (world-to-camera) of shape (4, 4)

Return type:

Tuple[Float[np.ndarray, “3 3”], Float[np.ndarray, “4 4”]]

ct.convert.K_T_to_P(K, T)[source]

Compute projection matrix P from intrinsic matrix K and extrinsic matrix T.

Parameters:
  • K (Float[ndarray, '3 3']) – Intrinsic matrix of shape (3, 3).

  • T (Float[ndarray, '4 4']) – Extrinsic matrix (world-to-camera) of shape (4, 4).

Returns:

Projection matrix of shape (3, 4).

Return type:

Float[ndarray, ‘3 4’]

ct.convert.K_R_t_to_P(K, R, t)[source]

Compute projection matrix from intrinsic matrix, rotation matrix, and translation vector.

Parameters:
  • K (Float[ndarray, '3 3']) – Intrinsic matrix of shape (3, 3).

  • R (Float[ndarray, '3 3']) – Rotation matrix of shape (3, 3).

  • t (Float[ndarray, '3']) – Translation vector of shape (3,).

Returns:

Projection matrix of shape (3, 4).

Return type:

Float[ndarray, ‘3 4’]

ct.convert.K_R_t_to_W2P(K, R, t)[source]

Compute world-to-projection matrix from intrinsic matrix, rotation matrix, and translation vector.

Parameters:
  • K (Float[ndarray, '3 3']) – Intrinsic matrix of shape (3, 3).

  • R (Float[ndarray, '3 3']) – Rotation matrix of shape (3, 3).

  • t (Float[ndarray, '3']) – Translation vector of shape (3,).

Returns:

World-to-projection matrix of shape (4, 4).

Return type:

Float[ndarray, ‘4 4’]

ct.convert.K_T_to_W2P(K, T)[source]

Compute world-to-projection matrix from intrinsic matrix and transformation matrix.

Parameters:
  • K (Float[ndarray, '3 3']) – Intrinsic matrix of shape (3, 3).

  • T (Float[ndarray, '4 4']) – Extrinsic matrix (world-to-camera matrix) of shape (4, 4).

Returns:

World-to-projection matrix of shape (4, 4).

Return type:

Float[ndarray, ‘4 4’]

ct.convert.P_to_W2P(P)[source]

Convert projection matrix to world-to-projection matrix.

Parameters:

P (Float[ndarray, '3 4']) – Projection matrix of shape (3, 4).

Returns:

World-to-projection matrix of shape (4, 4).

Return type:

Float[ndarray, ‘4 4’]

ct.convert.W2P_to_P(W2P)[source]

Convert world-to-projection matrix to projection matrix.

Parameters:

W2P (Float[ndarray, '4 4']) – World-to-projection matrix of shape (4, 4).

Returns:

Projection matrix of shape (3, 4).

Return type:

Float[ndarray, ‘3 4’]

ct.convert.fx_fy_cx_cy_to_K(fx, fy, cx, cy)[source]

Create intrinsic matrix from focal lengths and principal point coordinates.

Parameters:
  • fx (float) – Focal length in x direction.

  • fy (float) – Focal length in y direction.

  • cx (float) – Principal point x coordinate.

  • cy (float) – Principal point y coordinate.

Returns:

Intrinsic matrix of shape (3, 3).

Return type:

Float[ndarray, ‘3 3’]

ct.convert.K_to_fx_fy_cx_cy(K)[source]

Extract focal lengths and principal point coordinates from intrinsic matrix.

Parameters:

K (Float[ndarray, '3 3']) – Intrinsic matrix of shape (3, 3).

Returns:

  • fx: Focal length in x direction

  • fy: Focal length in y direction

  • cx: Principal point x coordinate

  • cy: Principal point y coordinate

Return type:

Tuple[float, float, float, float]

ct.convert.euler_to_R(yaw, pitch, roll)[source]

Convert Euler angles to rotation matrix. Given a unit vector x, R @ x is x rotated by applying yaw, pitch, and roll consecutively. Ref: https://en.wikipedia.org/wiki/Euler_angles

Parameters:
  • yaw (float) – Rotation around the z-axis (from x-axis to y-axis).

  • pitch (float) – Rotation around the y-axis (from z-axis to x-axis).

  • roll (float) – Rotation around the x-axis (from y-axis to z-axis).

Returns:

Rotation matrix R of shape (3, 3).

Return type:

Float[ndarray, ‘3 3’]

ct.convert.spherical_to_T_towards_origin(radius, theta, phi)[source]

Convert spherical coordinates (ISO convention) to T, where the cameras looks at the origin from a distance (radius), and the camera up direction alines with the z-axis (the angle between the up direction and the z-axis is smaller than pi/2).

Parameters:
  • radius (float) – Distance from the origin.

  • theta (float) – Inclination, angle w.r.t. positive polar (+z) axis. Range: [0, pi].

  • phi (float) – Azimuth, rotation angle from the initial meridian (x-y) plane. Range: [0, 2*pi].

Returns:

T of shape (4, 4).

Return type:

Float[ndarray, ‘4 4’]

Ref:

https://en.wikipedia.org/wiki/Spherical_coordinate_system

ct.convert.mesh_to_lineset(mesh, color=None)[source]

Convert Open3D mesh to Open3D lineset.

ct.convert.im_distance_to_im_depth(im_distance, K)[source]

Convert distance image to depth image.

Parameters:
  • im_distance (Float[ndarray, 'h w']) – Distance image (H, W), float.

  • K (Float[ndarray, '3 3']) – Camera intrinsic matrix (3, 3).

Returns:

Depth image (H, W), float.

Return type:

Float[ndarray, ‘h w’]

ct.convert.im_depth_to_im_distance(im_depth, K)[source]

Convert depth image to distance image.

Parameters:
  • im_depth (Float[ndarray, 'h w']) – Depth image (H, W), float.

  • K (Float[ndarray, '3 3']) – Camera intrinsic matrix (3, 3).

Returns:

Distance image (H, W), float.

Return type:

Float[ndarray, ‘h w’]