首頁  技術(shù)文章  C++ Eigen庫在3D相機(jī)手眼標(biāo)定中的應(yīng)用

C++ Eigen庫在3D相機(jī)手眼標(biāo)定中的應(yīng)用

發(fā)布時間:2021-03-02 10:32:25 瀏覽量:5701 作者:Criket

摘要

Eigen 是基于C++開發(fā)代數(shù)的一個模板庫:矩陣控乾、矢量么介、數(shù)值解算器和相關(guān)算法娜遵。相比較Matlab,優(yōu)勢是利于基于c++的3D相機(jī)開發(fā)

(大部分3D相機(jī)SDK都支持c++)壤短,劣勢是語法較復(fù)雜设拟。本文目標(biāo)是針對3D相機(jī)手眼標(biāo)定過程中有關(guān)矩陣算術(shù)的eigen庫運(yùn)用進(jìn)行學(xué)習(xí)。

正文


Eigen 是基于C++開發(fā)代數(shù)的一個模板庫:矩陣久脯、矢量纳胧、數(shù)值解算器和相關(guān)算法。相比較Matlab帘撰,優(yōu)勢是利于c++開發(fā)跑慕,劣勢是語法較復(fù)雜


環(huán)境配置

1.下載eigen源碼包,可解壓到任意位置

2.新建vc++工程摧找,項(xiàng)目屬性 -> C/C++ -> 常規(guī) -> 附加包含目錄 -> 編輯 -> 新建路徑 -> 選擇eigen文件夾所在路徑 -> 運(yùn)行下面的demo


Demo代碼

https://www.cnblogs.com/winslam/p/12765822.html


Matrix類介紹

Matrix類模板6個參數(shù)

//共6個

Matrix

int RowsAtCompileTime,

int ColsAtCompileTime,

int Options = 0, //默認(rèn)是按列存儲數(shù)據(jù)核行,可改成按行

int MaxRowsAtCompileTime = RowsAtCompileTime, //行數(shù)上限

int MaxColsAtCompileTime = ColsAtCompileTime> //列數(shù)上限


Matrinx類模板前三個參數(shù)

數(shù)據(jù)類型,行數(shù)蹬耘,列數(shù)芝雪,eigen已經(jīng)定義好了常用的,規(guī)律很好找

Matrix

示例

typedef Matrix Matrix4f; //矩陣內(nèi)存大小 float[16]

typedef Matrix Matrix3d; //矩陣內(nèi)存大小 double[9]


特殊的Matrix類:Vector向量

是特殊的Matrix類综苔,只有一行或一列惩系,定義如下

typedef Matrix Vector3f; // 三行一列

typedef Matrix RowVector2i;// 一行兩列


動態(tài)創(chuàng)建Matrix類對象

typedef Matrix MatrixXd; //行列都可不指定長度

typedef Matrix VectorXi;// 行動態(tài)

Matrix; //列動態(tài)


Matrix類構(gòu)造函數(shù)

默認(rèn)構(gòu)造函數(shù)不會動態(tài)分配內(nèi)存

對矩陣傳參總是優(yōu)先傳行數(shù)

MatrixXf a(3,2); //3行2列

 而給向量傳參 = 給向量傳大小:

Vector2d a(5.0, 6.0);

Vector3d b(5.0, 6.0, 7.0);

Vector4d c(5.0, 6.0, 7.0, 8.0);


訪問/初始化 Matrix元素

Matrix數(shù)據(jù)存儲順序如筛,總是先列后行

逗號初始化堡牡,總是先行后列,但數(shù)據(jù)存儲順序還是不變

Vector無所謂杨刨,使用.transform()方法晤柄,即可轉(zhuǎn)換行列

盡量用固定大小的Matrix,內(nèi)存機(jī)制沒深究


Matrix&Vector 運(yùn)算

加減法

原則很簡單拭嫁,相同行列才能運(yùn)算:

binary operator + as in a+b

binary operator - as in a-b

unary operator - as in -a

compound operator += as in a+=b

compound operator -= as in a-=b

注意:運(yùn)算符已被重載可免,不可2個以上的矩陣同時參與運(yùn)算抓于。此時用遍歷矩陣索引

VectorXf a(50),b(50),c(50),d(50);

a = 3*b + 4*c + 5*d;

改成

for(int i = 0;i < 50; + + i){

a[i] = 3*b[i] + 4*c[i] + 5*d[i];}


系數(shù)乘除法

binary operator * as in matrix*scalar

binary operator * as in scalar*matrix

binary operator / as in matrix/scalar

compound operator *= as in matrix*=scalar

compound operator /= as in matrix/=scalar


Matrix 轉(zhuǎn)置,共軛浇借,逆

//n階正交矩陣a特性: a*=aT,無共軛

Eigen::MatrixXd t = Eigen::MatrixXd::Random(3,3);

cout << t.transpose() << endl; //a的轉(zhuǎn)置

cout << t.conjugate() << endl; //a的共軛

cout << t.adjoint() << endl;   //a的逆


Matrix 乘法

代碼

#include

#include

int main()

{

    using namespace Eigen;

    Matrix2d mat;mat << 1, 2, 3, 4;//運(yùn)算符重載捉撮,矩陣按行接收數(shù)據(jù),但是儲存機(jī)制依舊優(yōu)先按列妇垢,即mat經(jīng)過上述賦值后為 [1,2

    //                   3,4]巾遭,但取值時,mat(1,0)值為3闯估,mat(0,1)值為2

    Vector2d v1(-1, 1), v2(2, 0); //默認(rèn)列向量

    cout << "mat * v1:" << endl<< mat * v1 << endl; cout << endl;

    cout << "mat * v2:" << endl << mat * v2 << endl; cout << endl;

    cout << "mat * mat:" << endl << mat * mat << endl;

}

輸出

mat * v1:

1

1


mat * v2:

2

6


mat * mat:

 7 10

15 22


Vector的點(diǎn)乘和叉乘


點(diǎn)乘灼舍。純代數(shù)運(yùn)算,適用與任意長度的向量涨薪,前提是2個向量長度相等

/*

計(jì)算公式:

a(a1,a2,a3) , b(b1,b2,b3)

a·b = a1*b1 + a2*b2 + a3*b3


幾何意義:

a·b =|a|*|b|*cosθ

*/


叉乘骑素。用于空間幾何,所以只適用于長度為3的向量

/*

幾何意義

axb =|a|*|b|*sinθ

注意:叉乘結(jié)果是向量刚夺,方向在z軸上献丑,θ表示向量a到向量b的角度,右手法則(從a到b)確定z朝向

*/

代碼

#include

#include


int main()

{

    using namespace Eigen;

Vector3d v1(1, 2, 3);

    Vector3d v2(1, 1, 2);


    //點(diǎn)積方法1:用同維度的向量做點(diǎn)積

    cout << "v1 點(diǎn)乘 v2 :" << endl << v.dot(v2) << endl;

    //點(diǎn)積方法2:向量轉(zhuǎn)化為矩陣乘積來做點(diǎn)積

    cout << "v1逆 點(diǎn)乘 v2 :" << endl << v.adjoint() * w << endl;

    

    //叉乘侠姑,外積

    cout << "v1 叉乘 v2 :" << endl << v.cross(v2) << endl;

    return 1;

}

輸出

v1 點(diǎn)乘 v2 :

9

v1 叉乘 v2 :

 1

 1

-1

v1逆 點(diǎn)乘 v2 :

9


Matrix內(nèi)部數(shù)據(jù)算術(shù)

代碼

#include

#include

using namespace std;

int main()

{

  Eigen::Matrix2d mat;

  mat << 1, 2,

         3, 4;

  cout << "Here is mat.sum():       " << mat.sum()       << endl;//求和

  cout << "Here is mat.prod():      " << mat.prod()      << endl;//連乘

  cout << "Here is mat.mean():      " << mat.mean()      << endl;//均值

  cout << "Here is mat.minCoeff():  " << mat.minCoeff()  << endl;//最小值

  cout << "Here is mat.maxCoeff():  " << mat.maxCoeff()  << endl;//最大值

  //主對角線系數(shù)和

  cout << "Here is mat.trace():     " << mat.trace()     << endl;


  //某些函數(shù)可以重載

  Matrix3f m = Matrix3f::Random();

  std::ptrdiff_t i, j;

  float minOfM = m.minCoeff(&i,&j);

  cout << "Here is the matrix m:\n" << m << endl;

  cout << "Its minimum coefficient (" << minOfM 

       << ") is at position (" << i << "," << j << ")\n\n";

 

  RowVector4i v = RowVector4i::Random();

  int maxOfV = v.maxCoeff(&i);

  cout << "Here is the vector v: " << v << endl;

  cout << "Its maximum coefficient (" << maxOfV 

       << ") is at position " << i << endl;

}

輸出

Here is mat.sum():       10

Here is mat.prod():      24

Here is mat.mean():      2.5

Here is mat.minCoeff():  1

Here is mat.maxCoeff():  4

Here is mat.trace():     5

Here is the matrix m:

     -1 -0.0827  -0.906

 -0.737  0.0655   0.358

  0.511  -0.562   0.359

Its minimum coefficient (-1) is at position (0,0)


Here is the vector v:  9 -2  0  7

Its maximum coefficient (9) is at position 0


Matrix類與Array類互換

矩陣運(yùn)算用矩陣類创橄,系數(shù)運(yùn)算用數(shù)組類,互相轉(zhuǎn)換用.matrix()方法 和 .array()方法


求方陣的行列式值

Tatrix3d mat;mat << 1,2,3,4,5,6,7,8,9; //3階方陣

double result = matrix.determinant();


Matrix初始化方法

前面提到過的逗號初始化

.Zero()方法初始化所有系數(shù)為0

.Random()方法用隨機(jī)系數(shù)填充矩陣或數(shù)組

.Identity()方法初始化一個單位矩陣


用分塊矩陣構(gòu)造成一個大矩陣

代碼

const int size = 6;

MatrixXd mat1(size, size);

mat1.topLeftCorner(size/2, size/2)     = MatrixXd::Zero(size/2, size/2);

mat1.topRightCorner(size/2, size/2)    = MatrixXd::Identity(size/2, size/2);

mat1.bottomLeftCorner(size/2, size/2)  = MatrixXd::Identity(size/2, size/2);

mat1.bottomRightCorner(size/2, size/2) = MatrixXd::Zero(size/2, size/2);

std::cout << mat1 << std::endl << std::endl;

 

MatrixXd mat2(size, size);

mat2.topLeftCorner(size/2, size/2).setZero();

mat2.topRightCorner(size/2, size/2).setIdentity();

mat2.bottomLeftCorner(size/2, size/2).setIdentity();

mat2.bottomRightCorner(size/2, size/2).setZero();

std::cout << mat2 << std::endl << std::endl;

 

MatrixXd mat3(size, size);

mat3 << MatrixXd::Zero(size/2, size/2), MatrixXd::Identity(size/2, size/2),

        MatrixXd::Identity(size/2, size/2), MatrixXd::Zero(size/2, size/2);

std::cout << mat3 << std::endl;


輸出


0 0 0 1 0 0

0 0 0 0 1 0

0 0 0 0 0 1

1 0 0 0 0 0

0 1 0 0 0 0

0 0 1 0 0 0


0 0 0 1 0 0

0 0 0 0 1 0

0 0 0 0 0 1

1 0 0 0 0 0

0 1 0 0 0 0

0 0 1 0 0 0


0 0 0 1 0 0

0 0 0 0 1 0

0 0 0 0 0 1

1 0 0 0 0 0

0 1 0 0 0 0

0 0 1 0 0 0


構(gòu)造齊次矩陣(4*4)

旋轉(zhuǎn)向量莽红,旋轉(zhuǎn)矩陣妥畏,歐拉角,四元數(shù)4者互換

https://blog.csdn.net/yang__jing/article/details/82316093?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control


四元數(shù)注意點(diǎn)

輸入順序是[w,x,y,z] 安吁,其中w是實(shí)數(shù)部分

儲存和輸出順序是[x,y,z,w] 醉蚁,其中w是實(shí)數(shù)部分

互為相反數(shù)的2組四元數(shù)效果一樣


歐拉角注意點(diǎn)

pose轉(zhuǎn)matrix默認(rèn)用'gba'形式( R = Rx * Ry * Rz),T1.rotation().eulerAngles(0, 1, 2) 


線性代數(shù)求解Ax=b形式方程

QR分解法:

代碼

#include

#include

 

using namespace std;

using namespace Eigen;

 

int main()

{

   Matrix3f A;

   Vector3f b;

   A << 1,2,3,  4,5,6,  7,8,10;

   b << 3, 3, 4;

   cout << "Here is the matrix A:\n" << A << endl;

   cout << "Here is the vector b:\n" << b << endl;

   Vector3f x = A.colPivHouseholderQr().solve(b);

   cout << "The solution is:\n" << x << endl;

}


輸出


Here is the matrix A:

 1  2  3

 4  5  6

 7  8 10

Here is the vector b:

3

3

4

The solution is:

-2

 1

 1



bdcSVD分解法

最精確但速度最慢的求解方法柳畔,用于求解線性方程馍管,在沒有解的情況下能逼近


代碼


#include

#include

 

using namespace std;

using namespace Eigen;

 

int main()

{

   MatrixXf A = MatrixXf::Random(3, 2);

   cout << "Here is the matrix A:\n" << A << endl;

   VectorXf b = VectorXf::Random(3);

   cout << "Here is the right hand side b:\n" << b << endl;

   cout << "The least-squares solution is:\n"

        << A.bdcSvd(ComputeThinU | ComputeThinV).solve(b) << endl;

}


您可以通過我們的官方網(wǎng)站了解更多的產(chǎn)品信息,或直接來電咨詢4006-888-532薪韩。


国产福利姬视频在线观看,国产原创激情在线观看网站,亚洲欧美日韩激色国产精品,日韩精品亚洲国产