分别基于Egien和OpenCV实现旋转矩阵到欧拉角的转换

基于Eigen:

#include <iostream>
#include <Eigen/Core>
#include <Eigen/Geometry>

using namespace std;

#define PI (3.1415926535897932346f)

int main(int argc,char**argv)
{
    cout<<endl<<"********** RotationMatrix **********"<<endl;
    //旋转矩阵初始化
    Eigen::Matrix3d R;
    R<<0.9929795546761236, 0.05236994408031403, -0.1060612698030327,
       -0.01543295134771394, 0.9463450518837896, 0.3227891986850962,
       0.1172750101594792, -0.3188862363478429, 0.9405095109885926;

    //旋转矩阵转换为欧拉角
    //ZYX顺序,即先绕x轴roll,再绕y轴pitch,最后绕z轴yaw,0表示X轴,1表示Y轴,2表示Z轴
    Eigen::Vector3d euler_angles = R.eulerAngles(2, 1, 0);
    cout << "yaw(z) pitch(y) roll(x) = " << euler_angles.transpose() << endl;
    return 0;
}

基于OpenCV:

#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <fstream>
#include <cmath>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;
// 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;
    }
#if 1
    x = x*180.0f/3.141592653589793f;
    y = y*180.0f/3.141592653589793f;
    z = z*180.0f/3.141592653589793f;
#endif
    return Vec3f(x, y, z);
}

int main(){

    Vec3f eulerAngles;
    Mat R = (Mat_<double>(3,3) << 0.9929795546761236, 0.05236994408031403, -0.1060612698030327,
                                             -0.01543295134771394, 0.9463450518837896, 0.3227891986850962,
                                             0.1172750101594792, -0.3188862363478429, 0.9405095109885926);
    eulerAngles = rotationMatrixToEulerAngles(R);
    cout << "eulerAngles = " << endl;
    cout << eulerAngles << endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值