Installation

  • http://eigen.tuxfamily.org/
  • Eigen是C++中的一个开源模板库,支持:
    • 线性代数运算
    • 矩阵与矢量运算
    • 数值分析及相关运算
  • Eigen只包含头文件,不需要编译,只需使用#include
  • 默认安装位置:/usr/include/eigen3
  • 第一次使用头文件<Eigen/Core>时,由于路径问题会提示:error: Eigen/Core: No such file or directory,此时需要执行sudo ln -s /usr/include/eigen3/Eigen /usr/include/Eigen更改头文件路径。
sudo apt-get install libeigen3-dev

Eigen VS Matlab

#include <Eigen/Dense>
// 基本用法  
// Eigen          // Matlab           // 注释  
x.size()          // length(x)        // 向量的长度  
C.rows()          // size(C,1)        // 矩阵的行数  
C.cols()          // size(C,2)        // 矩阵的列数  
x(i)              // x(i+1)           // 访问向量元素(Matlab的下标从1开始计数)  
C(i,j)            // C(i+1,j+1)       // 访问矩阵元素  
    
A << 1, 2, 3,     // 初始化A,元素也可以是矩阵,先按列堆叠,再按行堆叠。  
     4, 5, 6,       
     7, 8, 9;       
B << A, A, A;     // B 是3个A水平排列  
A.fill(10);       // 将A的所有元素填充为10  

// Eigen                                    // Matlab                       注释  
MatrixXd::Identity(rows,cols)               // eye(rows,cols)               //单位矩阵  
C.setIdentity(rows,cols)                    // C = eye(rows,cols)           //单位矩阵  
MatrixXd::Zero(rows,cols)                   // zeros(rows,cols)             //全零矩阵  
C.setZero(rows,cols)                        // C = zeros(rows,cols)         //全零矩阵  
MatrixXd::Ones(rows,cols)                   // ones(rows,cols)              //全一矩阵  
C.setOnes(rows,cols)                        // C = ones(rows,cols)          //全一矩阵  
MatrixXd::Random(rows,cols)                 // rand(rows,cols)*2-1          //MatrixXd::Random 返回范围为(-1, 1)的均匀分布的随机数  
C.setRandom(rows,cols)                      // C = rand(rows,cols)*2-1      //返回范围为(-1, 1)的均匀分布的随机数  
VectorXd::LinSpaced(size,low,high)          // linspace(low,high,size)'     //返回size个等差数列,第一个数为low,最后一个数为high  
v.setLinSpaced(size,low,high)               // v = linspace(low,high,size)' //返回size个等差数列,第一个数为low,最后一个数为high  
VectorXi::LinSpaced(((hi-low)/step)+1,      // low:step:hi                  //以step为步长的等差数列。((hi-low)/step)+1为个数  
                    low,low+step*(size-1))  //  


// Matrix 切片和块。下面列出的所有表达式都是可读/写的。
// 使用模板参数更快(如第2个)。注意:Matlab的下标是从1开始的。
// Eigen                           // Matlab                        // 注释  
x.head(n)                          // x(1:n)                        //前n个元素  
x.head<n>()                        // x(1:n)                        //前n个元素  
x.tail(n)                          // x(end - n + 1: end)           //倒数n个元素  
x.tail<n>()                        // x(end - n + 1: end)           //倒数n个元素  
x.segment(i, n)                    // x(i+1 : i+n)                  //切片,从第i个元素开始的n个元素
x.segment<n>(i)                    // x(i+1 : i+n)                  //切片,从第i个元素开始的n个元素
P.block(i, j, rows, cols)          // P(i+1 : i+rows, j+1 : j+cols) //块  
P.block<rows, cols>(i, j)          // P(i+1 : i+rows, j+1 : j+cols) //块  
P.row(i)                           // P(i+1, :)                     //第i行  
P.col(j)                           // P(:, j+1)                     //第j列  
P.leftCols<cols>()                 // P(:, 1:cols)                  //前cols列  
P.leftCols(cols)                   // P(:, 1:cols)                  //前cols列  
P.middleCols<cols>(j)              // P(:, j+1:j+cols)              //中间cols列  
P.middleCols(j, cols)              // P(:, j+1:j+cols)              //中间cols列  
P.rightCols<cols>()                // P(:, end-cols+1:end)          //后cols列  
P.rightCols(cols)                  // P(:, end-cols+1:end)          //后cols列  
P.topRows<rows>()                  // P(1:rows, :)                  //前rows行  
P.topRows(rows)                    // P(1:rows, :)                  //前rows行  
P.middleRows<rows>(i)              // P(i+1:i+rows, :)              //中间rows行  
P.middleRows(i, rows)              // P(i+1:i+rows, :)              //中间rows行  
P.bottomRows<rows>()               // P(end-rows+1:end, :)          //最后rows行  
P.bottomRows(rows)                 // P(end-rows+1:end, :)          //最后rows行  
P.topLeftCorner(rows, cols)        // P(1:rows, 1:cols)             //左上角块  
P.topRightCorner(rows, cols)       // P(1:rows, end-cols+1:end)     //右上角块  
P.bottomLeftCorner(rows, cols)     // P(end-rows+1:end, 1:cols)     //左下角块  
P.bottomRightCorner(rows, cols)    // P(end-rows+1:end, end-cols+1:end) //右下角块  
P.topLeftCorner<rows,cols>()       // P(1:rows, 1:cols)                 //左上角块  
P.topRightCorner<rows,cols>()      // P(1:rows, end-cols+1:end)         //右上角块  
P.bottomLeftCorner<rows,cols>()    // P(end-rows+1:end, 1:cols)         //左下角块  
P.bottomRightCorner<rows,cols>()   // P(end-rows+1:end, end-cols+1:end) //右下角块


// 特别说明:Eigen的交换函数进行了高度优化
// Eigen                           // Matlab  
R.row(i) = P.col(j);               // R(i, :) = P(:, j)  
R.col(j1).swap(mat1.col(j2));      // R(:, [j1 j2]) = R(:, [j2, j1]) //交换列  


// Views, transpose, etc;  
// Eigen                           // Matlab  
R.adjoint()                        // R'                    // 共轭转置  
R.transpose()                      // R.' or conj(R')       // 可读/写 转置  
R.diagonal()                       // diag(R)               // 可读/写 对角元素  
x.asDiagonal()                     // diag(x)               // 对角矩阵化  
R.transpose().colwise().reverse()  // rot90(R)              // 可读/写 逆时针旋转90度  
R.rowwise().reverse()              // fliplr(R)             // 水平翻转  
R.colwise().reverse()              // flipud(R)             // 垂直翻转  
R.replicate(i,j)                   // repmat(P,i,j)         // 复制矩阵,垂直复制i个,水平复制j个  


// 四则运算,和Matlab相同。但Matlab中不能使用*=这样的赋值运算符  
// 矩阵 - 向量    矩阵 - 矩阵      矩阵 - 标量  
y  = M*x;          R  = P*Q;        R  = P*s;  
a  = b*M;          R  = P - Q;      R  = s*P;  
a *= M;            R  = P + Q;      R  = P/s;  
                   R *= Q;          R  = s*P;  
                   R += Q;          R *= s;  
                   R -= Q;          R /= s;  


// 逐像素操作Vectorized operations on each element independently  
// Eigen                       // Matlab        //注释  
R = P.cwiseProduct(Q);         // R = P .* Q    //逐元素乘法  
R = P.array() * s.array();     // R = P .* s    //逐元素乘法(s为标量)  
R = P.cwiseQuotient(Q);        // R = P ./ Q    //逐元素除法  
R = P.array() / Q.array();     // R = P ./ Q    //逐元素除法  
R = P.array() + s.array();     // R = P + s     //逐元素加法(s为标量)  
R = P.array() - s.array();     // R = P - s     //逐元素减法(s为标量)  
R.array() += s;                // R = R + s     //逐元素加法(s为标量)  
R.array() -= s;                // R = R - s     //逐元素减法(s为标量)  
R.array() < Q.array();         // R < Q         //逐元素比较运算  
R.array() <= Q.array();        // R <= Q        //逐元素比较运算  
R.cwiseInverse();              // 1 ./ P        //逐元素取倒数  
R.array().inverse();           // 1 ./ P        //逐元素取倒数  
R.array().sin()                // sin(P)        //逐元素计算正弦函数  
R.array().cos()                // cos(P)        //逐元素计算余弦函数  
R.array().pow(s)               // P .^ s        //逐元素计算幂函数  
R.array().square()             // P .^ 2        //逐元素计算平方  
R.array().cube()               // P .^ 3        //逐元素计算立方  
R.cwiseSqrt()                  // sqrt(P)       //逐元素计算平方根  
R.array().sqrt()               // sqrt(P)       //逐元素计算平方根  
R.array().exp()                // exp(P)        //逐元素计算指数函数  
R.array().log()                // log(P)        //逐元素计算对数函数  
R.cwiseMax(P)                  // max(R, P)     //逐元素计算R和P的最大值  
R.array().max(P.array())       // max(R, P)     //逐元素计算R和P的最大值  
R.cwiseMin(P)                  // min(R, P)     //逐元素计算R和P的最小值  
R.array().min(P.array())       // min(R, P)     //逐元素计算R和P的最小值  
R.cwiseAbs()                   // abs(P)        //逐元素计算R和P的绝对值  
R.array().abs()                // abs(P)        //逐元素计算绝对值  
R.cwiseAbs2()                  // abs(P.^2)     //逐元素计算平方  
R.array().abs2()               // abs(P.^2)     //逐元素计算平方  
(R.array() < s).select(P,Q );  // (R < s ? P : Q)                             //根据R的元素值是否小于s,选择P和Q的对应元素  
R = (Q.array()==0).select(P,A) // R(Q==0) = P(Q==0) R(Q!=0) = P(Q!=0)         //根据Q中元素等于零的位置选择P中元素  
R = P.unaryExpr(ptr_fun(func)) // R = arrayfun(func, P)     // 对P中的每个元素应用func函数  


// Reductions.  
int r, c;  
// Eigen                  // Matlab                 //注释  
R.minCoeff()              // min(R(:))              //最小值  
R.maxCoeff()              // max(R(:))              //最大值  
s = R.minCoeff(&r, &c)    // [s, i] = min(R(:)); [r, c] = ind2sub(size(R), i); //计算最小值和它的位置  
s = R.maxCoeff(&r, &c)    // [s, i] = max(R(:)); [r, c] = ind2sub(size(R), i); //计算最大值和它的位置  
R.sum()                   // sum(R(:))              //求和(所有元素)  
R.colwise().sum()         // sum(R)                 //按列求和  
R.rowwise().sum()         // sum(R, 2) or sum(R')'  //按行求和  
R.prod()                  // prod(R(:))                 //累积  
R.colwise().prod()        // prod(R)                    //按列累积  
R.rowwise().prod()        // prod(R, 2) or prod(R')'    //按行累积  
R.trace()                 // trace(R)                   //迹  
R.all()                   // all(R(:))                  //是否所有元素都非零  
R.colwise().all()         // all(R)                     //按列判断,是否该列所有元素都非零  
R.rowwise().all()         // all(R, 2)                  //按行判断,是否该行所有元素都非零  
R.any()                   // any(R(:))                  //是否有元素非零  
R.colwise().any()         // any(R)                     //按列判断,是否该列有元素都非零  
R.rowwise().any()         // any(R, 2)                  //按行判断,是否该行有元素都非零  


// 点积,范数等  
// Eigen                  // Matlab           // 注释  
x.norm()                  // norm(x).         //范数(注意:Eigen中没有norm(R))  
x.squaredNorm()           // dot(x, x)        //平方和(注意:对于复数而言,不等价)  
x.dot(y)                  // dot(x, y)        //点积  
x.cross(y)                // cross(x, y)      //交叉积,需要头文件 #include <Eigen/Geometry>  


//// 类型转换  
// Eigen                  // Matlab             // 注释  
A.cast<double>();         // double(A)          //变成双精度类型  
A.cast<float>();          // single(A)          //变成单精度类型  
A.cast<int>();            // int32(A)           //编程整型  
A.real();                 // real(A)            //实部  
A.imag();                 // imag(A)            //虚部  
// 如果变换前后的类型相同,不做任何事情。  


// 注意:Eigen中,绝大多数的涉及多个操作数的运算都要求操作数具有相同的类型  
MatrixXf F = MatrixXf::Zero(3,3);  
A += F;                // 非法。Matlab中允许。(单精度+双精度)  
A += F.cast<double>(); // 将F转换成double,并累加。(一般都是在使用时临时转换)  


// Eigen 可以将已存储数据的缓存 映射成 Eigen矩阵  
float array[3];  
Vector3f::Map(array).fill(10);            // create a temporary Map over array and sets entries to 10  
int data[4] = {1, 2, 3, 4};  
Matrix2i mat2x2(data);                    // 将 data 复制到 mat2x2  
Matrix2i::Map(data) = 2*mat2x2;           // 使用 2*mat2x2 覆写data的元素   
MatrixXi::Map(data, 2, 2) += mat2x2;      // 将 mat2x2 加到 data的元素上 (当编译时不知道大小时,可选语法)

Eigen \(\rightleftharpoons\) OpenCV

Eigen \(\to\) OpenCV

#include <Eigen/Dense>
#include <iostream>
#include <opencv2/core/eigen.hpp>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
using namespace Eigen;
 
void main()
{
    Mat img;
    Matrix<int,100,100> m ;
    m.fill(255);
    eigen2cv(m, img);
    return;
}

OpenCV \(\to\) Eigen

#include <Eigen/Dense>
#include <iostream>
#include <opencv2/core/eigen.hpp>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
using namespace Eigen;
 
void main()
{
    Mat img = imread("jasen.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    int row = img.rows;
    int col = img.cols;
    MatrixXd m(row, col);
    cv2eigen(img,m);
    return;
}

矩阵运算

#include <iostream>
using namespace std;
#include <ctime>
//eigen核心部分
#include <Eigen/Core>
//稠密矩阵的代数运算(逆、特征值等)
#include <Eigen/Dense>
using namespace Eigen;
#define MATRIX_SEZE 50

int main(int argc, char **argv)
{
    //声明一个2*3的矩阵,eigen中所有的向量矩阵均为Eigen::Matrix,是一个模板类。
    //三个参数分别为数据类型、行数、列数
    Matrix<float, 2, 3> matrix_23;

    //声明一个三维向量,Vector_3d实际上是一个通过typedef定义的内置类型,本质上仍然是Eigen::Matrix模板类
    Vector3d v_3d;
    //Matrix<float, 2, 3> vd_3d;

    //声明一个3*3的矩阵,并初始化为0矩阵
    Matrix3d matrix_33 = Matrix3d::Zero();

    //不确定矩阵大小时,可以使用动态大小的矩阵
    Matrix<double, Dynamic, Dynamic> matrix_dynamic;

    //或者更简单的方式
    MatrixXd matrix_x;

    //准备对Eigen矩阵初始化
    //输入数据
    matrix_23 << 1, 2, 3, 4, 5, 6;
    //shuchu
    cout << "matrix 2*3 from 1 to 6 : \n"<< matrix_23 << endl;

    //使用()访问矩阵中的元素
    cout << "print matirx 2*3 : "<< endl;
    for (int i = 0; i < 2; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            cout << matrix_23(i,j) << "\t";
        }
        cout << endl;
    }

    //初始化两个三维向量
    v_3d << 3, 2, 1;
    //vd_3d << 4, 5, 6;
    //不能混乘两种类型的矩阵,因此需要显示转换类型:.cast<type>
    Matrix<double, 2, 1> result = matrix_23.cast<double>() * v_3d;
    cout << "[1,2,3;4,5,6]*[3,2,1] = "<< result.transpose() << endl;
    //.transpose()是转置函数
    cout << "not transpose [1,2,3;4,5,6]*[3,2,1] = "<< result << endl;

    Matrix<float, 2, 1> result2 = matrix_23 * v_3d.cast<float>();
    cout << "[1,2,3;4,5,6]*[3,2,1] = "<< result2.transpose() << endl;

    //生成一个随机矩阵
    matrix_33 = Matrix3d::Random();
    cout << "random matrix: \n"<< matrix_33 << endl;
    cout << "transpose : \n"<< matrix_33.transpose() << endl;  //转置
    cout << "sum :" << matrix_33.sum() << endl;                //求和
    cout << "trace : "<< matrix_33.trace() << endl;            //求迹
    cout << "time 10: \n"<< 10 * matrix_33 << endl;            //数乘
    cout << "inverse : \n"<< matrix_33.inverse() << endl;      //求逆
    cout << "det : \n"<< matrix_33.determinant() << endl;      //求行列式

    //求特征值
    //实对称矩阵可以保证对角化成功
    SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33);   //SelfAdjointEigenSolver<Matrix3d>y用于求解特征值和特征向量
    cout << "Eigen values = \n" << eigen_solver.eigenvalues() << endl;
    cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;

    //求解方程 Ax = b
    //求解Matrix_NN * x = v_Nd方程
    Matrix<double, MATRIX_SEZE, MATRIX_SEZE> matrix_NN
        = MatrixXd::Random(MATRIX_SEZE,MATRIX_SEZE);     //MATRIX_SEZE在前面宏定义中
    matrix_NN = matrix_NN *matrix_NN.transpose();        //此处理可以保证矩阵是半正定的
    Matrix<double, MATRIX_SEZE, 1> v_Nd = MatrixXd::Random(MATRIX_SEZE, 1);

    clock_t time_stt = clock();                          //用于计时

    //直接求逆
    Matrix<double, MATRIX_SEZE, 1> x = matrix_NN.inverse() * v_Nd;
    cout << "time of normal inverse is"
        << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout << "x = "<< x.transpose() << endl;

    //再用矩阵分解,例如QR分解求解,速度将会快很多
    time_stt = clock();
    x = matrix_NN.colPivHouseholderQr().solve(v_Nd);
    cout << "time of Qr decomposition is "
       << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout << "x = " << x.transpose() << endl;

    //对于正定矩阵,还可以用cholesky分解来分解方程
    time_stt = clock();
    x = matrix_NN.ldlt().solve(v_Nd);
    cout << "time of ldlt decomposition is "
       << 1000 * (clock() - time_stt) / (double) CLOCKS_PER_SEC << "ms" << endl;
    cout << "x = " << x.transpose() << endl;

    return 0;
}

几何运算

  • 头文件Eigen/Geometry.
  • 支持运算:
    • 四元数
    • 欧拉角
    • 旋转矩阵
    • 旋转和平移
Eigen::Matrix3d      //旋转矩阵(3*3)
Eigen::AngleAxisd    //旋转向量(3*1)
Eigen::Vector3d      //欧拉角(3*1)
Eigen::Quaterniond   //四元数(4*1)
Eigen::Isometry3d    //欧式变换矩阵(4*4)
Eigen::Affine3d      //放射变换矩阵(4*4)
Eigen::Projective3d  //射影变换矩阵(4*4)

上述数据类型均为双精度(double)型,若改为单精度(float)型,需将类型最后的d改为f。

//本程序将演示Geometry几何模块的使用
#include <iostream>
#include <cmath>
using namespace std;

#include <Eigen/Core>
#include <Eigen/Geometry>
//Geometry模块提供了各种旋转和平移的表示
using namespace Eigen;

int main(int argc, char ** argv)
{
    //3d旋转矩阵可以直接使用Matrix3d或者Matrix3f
    Matrix3d rotation_matrix = Matrix3d :: Identity();

    //旋转向量使用AngleAxis,运算可以当做矩阵
    AngleAxisd rotation_vector(M_PI / 4, Vector3d(0,0,1));     //饶Z轴旋转45°
    cout.precision(3);                                         //输出精度为小数点后两位
    cout << "rotation matrix = \n" << rotation_vector.matrix() << endl;
    //用matrix转换成矩阵可以直接赋值
    rotation_matrix = rotation_vector.toRotationMatrix();

    //使用Amgleanxis可以进行坐标变换
    Vector3d v(1, 0, 0);
    Vector3d v_rotated = rotation_vector * v;
    cout << "(1,0,0) after rotation (by angle axis) = " << v_rotated.transpose() << endl;

    //使用旋转矩阵
    v_rotated = rotation_matrix * v;
    cout << "(1,0,0) after rotation (by matrix) = " << v_rotated.transpose() << endl;

    //欧拉角:可以将矩阵直接转换成欧拉角
    Vector3d euler_angles = rotation_matrix.eulerAngles(2, 1, 0);       //按照ZYX顺序,即yaw,pitch,roll顺序
    cout << "yaw pitch roll = "<< euler_angles.transpose() << endl;

    //欧式变换矩阵使用Eigen::Isometry
    Isometry3d T = Isometry3d::Identity();      //实质为4*4的矩阵
    T.rotate(rotation_vector);                  //按照rotation_vector进行转化
    T.pretranslate(Vector3d(1, 3, 4));          //平移向量设为(1, 3, 4)
    cout << "Transform matrix = \n" << T.matrix() <<endl;

    //变换矩阵进行坐标变换
    Vector3d v_transformed = T *v;
    cout << "v transormed =" << v_transformed.transpose() << endl;

    //四元数
    //直接把AngleAxis赋值给四元数,反之亦然
    Quaterniond q = Quaterniond(rotation_vector);
    cout << "quaternion from rotation vector = " << q.coeffs().transpose() << endl;   // 请注意coeffs的顺序是(x,y,z,w),w为实部,前三者为虚部,而定义quaterniond时顺序为(w,x,y,z)
    q = Quaterniond(rotation_matrix);
    cout << "quaternion from rotation matrix = "<< q.coeffs().transpose() << endl;

    //四元数转换为旋转矩阵再转换为欧拉角
    euler_angles = q.toRotationMatrix().eulerAngles(2, 1, 0);       //按照ZYX顺序,即yaw,pitch,roll顺序

    //使用四元数旋转一个向量,使用重载的乘法即可
    v_rotated = q * v;  // 数学表达是 q v q^{-1}
    cout << "(1,0,0) after rotation = " << v_rotated.transpose() << endl;
    cout << "should be equal to " << (q * Quaterniond(0, 1, 0, 0) * q.inverse()).coeffs().transpose() << endl;

    return 0;
}

向量/矩阵范数运算1

#include <Eigen/Dense>
#include <iostream>
 
using namespace std;
using namespace Eigen;
 
int main()
{
    VectorXf v(2);
    MatrixXf m(2,2), n(2,2);
    
    v << -1,
        2;
    
    m << 1,-2,
        -3,4;
    
    cout << "v.squaredNorm() = " << v.squaredNorm() << endl;
    cout << "v.norm() = " << v.norm() << endl;
    cout << "v.lpNorm<1>() = " << v.lpNorm<1>() << endl;
    cout << "v.lpNorm<Infinity>() = " << v.lpNorm<Infinity>() << endl;
    
    cout << endl;
    cout << "m.squaredNorm() = " << m.squaredNorm() << endl;
    cout << "m.norm() = " << m.norm() << endl;
    cout << "m.lpNorm<1>() = " << m.lpNorm<1>() << endl;
    cout << "m.lpNorm<Infinity>() = " << m.lpNorm<Infinity>() << endl;
}

Output:

v.squaredNorm() = 5
v.norm() = 2.23607
v.lpNorm<1>() = 3
v.lpNorm<Infinity>() = 2

m.squaredNorm() = 30
m.norm() = 5.47723
m.lpNorm<1>() = 10
m.lpNorm<Infinity>() = 4

References