旋转矩阵与欧拉角互转

参考:https://www.learnopencv.com/rotation-matrix-to-euler-angles/

一般用Z-Y-X欧拉角,它对应于yaw偏航,pitch俯仰和roll滚转。

欧拉角 -> 旋转矩阵

\[ \mathbf{R_x} = \begin{bmatrix}     1       & 0 & 0 \\     0 & \cos(\theta_x) & -\sin(\theta_x)\\     0 & \sin(\theta_x) & (\cos\theta_x) \end{bmatrix} \]

\[ \mathbf{R_y} = \begin{bmatrix}     \cos(\theta_y)       & 0 & \sin(\theta_y) \\     0 & 1 & 0\\     -\sin(\theta_y) & 0 & (\cos\theta_y) \end{bmatrix} \]

\[ \mathbf{R_z} = \begin{bmatrix}     \cos(\theta_z) & -\sin(\theta_z) & 0 \\     \sin(\theta_z) & (\cos\theta_z) & 0 \\      0       & 0 & 1 \\ \end{bmatrix} \]

C++代码:

// Calculates rotation matrix given euler angles.
Mat eulerAnglesToRotationMatrix(Vec3f &theta)
{
    // Calculate rotation about x axis
    Mat R_x = (Mat_<double>(3,3) <<
               1,       0,              0,
               0,       cos(theta[0]),   -sin(theta[0]),
               0,       sin(theta[0]),   cos(theta[0])
               );
     
    // Calculate rotation about y axis
    Mat R_y = (Mat_<double>(3,3) <<
               cos(theta[1]),    0,      sin(theta[1]),
               0,               1,      0,
               -sin(theta[1]),   0,      cos(theta[1])
               );
     
    // Calculate rotation about z axis
    Mat R_z = (Mat_<double>(3,3) <<
               cos(theta[2]),    -sin(theta[2]),      0,
               sin(theta[2]),    cos(theta[2]),       0,
               0,               0,                  1);
     
    // Combined rotation matrix
    Mat R = R_z * R_y * R_x;
     
    return R;
 
}

python

def eulerAngles2rotationMat(theta, format='degree'):
    """
    Calculates Rotation Matrix given euler angles.
    :param theta: 1-by-3 list [rx, ry, rz] angle in degree
    :return:
    RPY角,是ZYX欧拉角,依次 绕定轴XYZ转动[rx, ry, rz]
    """
    if format is 'degree':
        theta = [i * math.pi / 180.0 for i in theta]

    R_x = np.array([[1, 0, 0],
                    [0, math.cos(theta[0]), -math.sin(theta[0])],
                    [0, math.sin(theta[0]), math.cos(theta[0])]
                    ])

    R_y = np.array([[math.cos(theta[1]), 0, math.sin(theta[1])],
                    [0, 1, 0],
                    [-math.sin(theta[1]), 0, math.cos(theta[1])]
                    ])

    R_z = np.array([[math.cos(theta[2]), -math.sin(theta[2]), 0],
                    [math.sin(theta[2]), math.cos(theta[2]), 0],
                    [0, 0, 1]
                    ])
    R = np.dot(R_z, np.dot(R_y, R_x))
    return R

 

旋转矩阵 -> 欧拉角

 将旋转矩阵转换成欧拉角是有点棘手的。该解决方案在大多数情况下不是唯一的

可以验证,[11,136,64]和[-169,44,-116]对应的旋转矩阵是一样的。

theta2=[11,136,64]
theta3=[-169,44,-116]

R2 = eulerAngles2rotationMat(theta2)
R3 = eulerAngles2rotationMat(theta3)
print "R2 = ",R2
print "R3 = ", R3

输出: 

R2 =  [[-0.31533781 -0.82417586  0.47042132]
 [-0.64653833  0.54944955  0.52923849]
 [-0.69465837 -0.1372565  -0.7061235 ]]
R3 =  [[-0.31533781 -0.82417586  0.47042132]
 [-0.64653833  0.54944955  0.52923849]
 [-0.69465837 -0.1372565  -0.7061235 ]]

 

       下面的代码显示了求解给定旋转矩阵的欧拉角的一种方法。其输出应该与MATLAB的rotm2euler.m的输出匹配,但是x和z的顺序是交换的(Z-Y-X);

(MATLAB中rotm2euler.m的输出,依次为ZYX角;该文代码的输出,依次为RPY角(XYZ),x和z的顺序是交换的)

C++代码:

// Checks if a matrix is a valid rotation matrix.
bool isRotationMatrix(Mat &R)
{
    Mat Rt;
    transpose(R, Rt);
    Mat shouldBeIdentity = Rt * R;
    Mat I = Mat::eye(3,3, shouldBeIdentity.type());
     
    return  norm(I, shouldBeIdentity) < 1e-6;
     
}
 
// Calculates rotation matrix to euler angles
// The result is the same as MATLAB except the order
// of the euler angles ( x and z are swapped ).
Vec3f rotationMatrixToEulerAngles(Mat &R)
{
 
    assert(isRotationMatrix(R));
     
    float sy = sqrt(R.at<double>(0,0) * R.at<double>(0,0) +  R.at<double>(1,0) * R.at<double>(1,0) );
 
    bool singular = sy < 1e-6; // If
 
    float x, y, z;
    if (!singular)
    {
        x = atan2(R.at<double>(2,1) , R.at<double>(2,2));
        y = atan2(-R.at<double>(2,0), sy);
        z = atan2(R.at<double>(1,0), R.at<double>(0,0));
    }
    else
    {
        x = atan2(-R.at<double>(1,2), R.at<double>(1,1));
        y = atan2(-R.at<double>(2,0), sy);
        z = 0;
    }
    return Vec3f(x, y, z);
             
}


python代码:

# Checks if a matrix is a valid rotation matrix.
def isRotationMatrix(R) :
    Rt = np.transpose(R)
    shouldBeIdentity = np.dot(Rt, R)
    I = np.identity(3, dtype = R.dtype)
    n = np.linalg.norm(I - shouldBeIdentity)
    return n < 1e-6
 
 
# Calculates rotation matrix to euler angles
# The result is the same as MATLAB except the order
# of the euler angles ( x and z are swapped ).
def rotationMatrixToEulerAngles(R) :
 
    assert(isRotationMatrix(R))
     
    sy = math.sqrt(R[0,0] * R[0,0] +  R[1,0] * R[1,0])
     
    singular = sy < 1e-6
 
    if  not singular :
        x = math.atan2(R[2,1] , R[2,2])
        y = math.atan2(-R[2,0], sy)
        z = math.atan2(R[1,0], R[0,0])
    else :
        x = math.atan2(-R[1,2], R[1,1])
        y = math.atan2(-R[2,0], sy)
        z = 0
 
    return np.array([x, y, z])


 

  • 4
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值