(code)[https://gist.github.com/zalo/71fbd5dbfe23cb46406d211b84be9f7e]
# Construct a "rotation" matrix (strong simplification, might have shearing)
rotationMatrix = np.empty((3,3))
# 脸颊两点连线为x轴
rotationMatrix[0,:] = (centered[16] - centered[0])/np.linalg.norm(centered[16] - centered[0])
# 下巴眉心两点连线为y轴
rotationMatrix[1,:] = (centered[8] - centered[27])/np.linalg.norm(centered[8] - centered[27])
# 上面两条线所在平面的法向量为z轴( 所以用叉乘)
rotationMatrix[2,:] = np.cross(rotationMatrix[0, :], rotationMatrix[1, :])
# 所有点的中心为原点,由此构建一个三维坐标系
def RotMat2Euler(R):
''' 根据旋转矩阵计算各个轴旋转角度 '''
E2 = -np.arcsin(R[0,2]) #
E1 = np.arctan2(R[1,2]/np.cos(E2), R[2,2]/np.cos(E2)) * 180 / np.pi
E3 = np.arctan2(R[0,1]/np.cos(E2), R[1,1]/np.cos(E2)) * 180 / np.pi
E2 = E2* 180 / np.pi
return (E1,E2,E3)