目录
一:安装Eigen
(1)安装
方式一、直接命令安装
方式二、源码安装:
(2)移动文件
二:使用Eigen——旋转矩阵转换欧拉角
三:其他用法示例
简单记录下~~
Eigen是一个基于C++模板的开源库,支持线性代数,矩阵和矢量运算,数值分析及其相关的算法。
官网:Eigen
一:安装Eigen
(1)安装
方式一、直接命令安装
sudo apt-get install libeigen3-dev
方式二、源码安装:
https://gitlab.com/libeigen/eigen/-/releases
mkdir build
cd build/
cmake ..
make -j 4
sudo make install
(2)移动文件
sudo cp -r /usr/include/eigen3/Eigen /usr/include
sudo cp -r /usr/include/eigen3/signature_of_eigen3_matrix_library /usr/include
sudo cp -r /usr/include/eigen3/unsupported /usr/include
不复制,链接也行:
sudo ln -s /usr/include/eigen3/Eigen /usr/include/Eigen
sudo ln -s /usr/include/eigen3/Eigen /usr/include/unsupported
sudo ln -s /usr/include/eigen3/Eigen /usr/include/signature_of_eigen3_matrix_library
二:使用Eigen——旋转矩阵转换欧拉角
#include <iostream>
// eigen相关头文件在opencv之前
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <opencv2/aruco/charuco.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/aruco/dictionary.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/core/eigen.hpp>
cv::Mat R = cv::Mat::zeros(3,3,CV_32FC1);
Eigen::Matrix3d R_eigen;
cv::cv2eigen(R,R_eigen);
// Eigen中使用右乘的顺序,因此ZYX对应的是012,实际上这个编号跟乘法的顺序一致就可以了(从左向右看的顺序)
Eigen::Vector3d zyx_Euler_fromR=R_eigen.eulerAngles(0,1,2);
cout<< "zyx euler from Rotation \n[输出顺序为:x,y,z]:\n"<<(180)/(M_PI)*zyx_Euler_fromR.transpose()<<endl;
三:其他用法示例
一、旋转向量
1.0 初始化旋转向量:旋转角为alpha,旋转轴为(x,y,z)
Eigen::AngleAxisd rotation_vector(alpha,Vector3d(x,y,z))
1.1 旋转向量转旋转矩阵
Eigen::Matrix3d rotation_matrix;rotation_matrix=rotation_vector.matrix();
Eigen::Matrix3d rotation_matrix;rotation_matrix=rotation_vector.toRotationMatrix();
1.2 旋转向量转欧拉角(Z-Y-X,即RPY)
Eigen::Vector3d eulerAngle=rotation_vector.matrix().eulerAngles(2,1,0);
1.3 旋转向量转四元数
Eigen::Quaterniond quaternion(rotation_vector);
Eigen::Quaterniond quaternion;Quaterniond quaternion;
Eigen::Quaterniond quaternion;quaternion=rotation_vector;
二、旋转矩阵
2.0 初始化旋转矩阵
Eigen::Matrix3d rotation_matrix;rotation_matrix<<x_00,x_01,x_02,x_10,x_11,x_12,x_20,x_21,x_22;
2.1 旋转矩阵转旋转向量
Eigen::AngleAxisd rotation_vector(rotation_matrix);
Eigen::AngleAxisd rotation_vector;rotation_vector=rotation_matrix;
Eigen::AngleAxisd rotation_vector;rotation_vector.fromRotationMatrix(rotation_matrix);
2.2 旋转矩阵转欧拉角(Z-Y-X,即RPY)
Eigen::Vector3d eulerAngle=rotation_matrix.eulerAngles(2,1,0);
2.3 旋转矩阵转四元数
Eigen::Quaterniond quaternion(rotation_matrix);
Eigen::Quaterniond quaternion;quaternion=rotation_matrix;
三、欧拉角
3.0 初始化欧拉角(Z-Y-X,即RPY)
Eigen::Vector3d eulerAngle(yaw,pitch,roll);
3.1 欧拉角转旋转向量
Eigen::AngleAxisd rollAngle(AngleAxisd(eulerAngle(2),Vector3d::UnitX()));Eigen::AngleAxisd pitchAngle(AngleAxisd(eulerAngle(1),Vector3d::UnitY()));Eigen::AngleAxisd yawAngle(AngleAxisd(eulerAngle(0),Vector3d::UnitZ())); Eigen::AngleAxisd rotation_vector;rotation_vector=yawAngle*pitchAngle*rollAngle;
3.2 欧拉角转旋转矩阵
Eigen::AngleAxisd rollAngle(AngleAxisd(eulerAngle(2),Vector3d::UnitX()));Eigen::AngleAxisd pitchAngle(AngleAxisd(eulerAngle(1),Vector3d::UnitY()));Eigen::AngleAxisd yawAngle(AngleAxisd(eulerAngle(0),Vector3d::UnitZ())); Eigen::Matrix3d rotation_matrix;rotation_matrix=yawAngle*pitchAngle*rollAngle;
3.3 欧拉角转四元数
Eigen::AngleAxisd rollAngle(AngleAxisd(eulerAngle(2),Vector3d::UnitX()));Eigen::AngleAxisd pitchAngle(AngleAxisd(eulerAngle(1),Vector3d::UnitY()));Eigen::AngleAxisd yawAngle(AngleAxisd(eulerAngle(0),Vector3d::UnitZ())); Eigen::Quaterniond quaternion;quaternion=yawAngle*pitchAngle*rollAngle;
四、四元数
4.0 初始化四元数
Eigen::Quaterniond quaternion(w,x,y,z);
4.1 四元数转旋转向量
Eigen::AngleAxisd rotation_vector(quaternion);
Eigen::AngleAxisd rotation_vector;rotation_vector=quaternion;
4.2 四元数转旋转矩阵
Eigen::Matrix3d rotation_matrix;rotation_matrix=quaternion.matrix();
Eigen::Matrix3d rotation_matrix;rotation_matrix=quaternion.toRotationMatrix();
4.4 四元数转欧拉角(Z-Y-X,即RPY)
Eigen::Vector3d eulerAngle=quaternion.matrix().eulerAngles(2,1,0)