Skip to content

Instantly share code, notes, and snippets.

@MiaoDX
Created November 23, 2018 12:10
Show Gist options
  • Save MiaoDX/b0342639e0492364841ca05746d8c017 to your computer and use it in GitHub Desktop.
Save MiaoDX/b0342639e0492364841ca05746d8c017 to your computer and use it in GitHub Desktop.
Pose will bring point in world into camera coordinate, [R|t]=[R|-RC] R rotate camera to world coordinate, C is camara in world coordinate, and t is world origin in camera coordinate.
"""
Camera pose=[R|t]=[R|-RC], R: rotate camera to world, C: camera in world, t: world origin in camera coordinate.
"""
import transforms3d
import math
import numpy as np
def pitch_yaw_roll_C_apply_point(pitch, yaw, roll, C, p_w):
# pitch, yaw, roll = 10, 20, 30
# x, y, z = 0, 90, 0
x=math.radians(pitch)
y=math.radians(yaw)
z=math.radians(roll)
R_w2c = transforms3d.euler.euler2mat(x, y, z, axes='rxyz')
R_c2w = R_w2c.T
C = np.array(C).reshape(3, 1)
t = -np.dot(R_c2w, C)
p_w = np.array(p_w).reshape(3, 1)
p_c = np.dot(R_c2w, p_w)+t
return t, p_c
if __name__ == '__main__':
pitch, yaw, roll, C = 0, 90, 0, [10, 0, 0]
p_w = [0, 0, 10]
t, p_c = pitch_yaw_roll_C_apply_point(pitch, yaw, roll, C, p_w)
assert np.allclose(t.flatten(), [0, 0, -10])
assert np.allclose(p_c.flatten(), [-10, 0, -10])
pitch, yaw, roll, C = 90, 0, -90, [10, 0, 0]
p_w = [0, 0, 10]
t, p_c = pitch_yaw_roll_C_apply_point(pitch, yaw, roll, C, p_w)
assert np.allclose(t.flatten(), [0, -10, 0])
assert np.allclose(p_c.flatten(), [-10, -10, 0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment