Skip to content

Instantly share code, notes, and snippets.

@ChingT
Created July 1, 2020 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChingT/3830c9c2a7ef8bc327070745bf357410 to your computer and use it in GitHub Desktop.
Save ChingT/3830c9c2a7ef8bc327070745bf357410 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
def rvec_to_yawpitchroll(rvec):
"""
Convert rotation vector to yaw, pitch, roll
Args:
rvec: pose rotation vector (rvec can be obtained by
[rotation_x, rotation_y, rotation_z] in head_pose_tacker_poses.csv)
Returns:
yaw, pitch, roll in degrees
"""
rvec = np.asarray(rvec, dtype=np.float64)
rmat = cv2.Rodrigues(rvec)[0]
sy = np.sqrt(rmat[0, 0] * rmat[0, 0] + rmat[1, 0] * rmat[1, 0])
if sy < 1e-6:
x = np.arctan2(-rmat[1, 2], rmat[1, 1])
y = np.arctan2(-rmat[2, 0], sy)
z = 0
else:
x = np.arctan2(rmat[2, 1], rmat[2, 2])
y = np.arctan2(-rmat[2, 0], sy)
z = np.arctan2(rmat[1, 0], rmat[0, 0])
yaw, pitch, roll = np.rad2deg([y, x, z])
return yaw, pitch, roll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment