Eigen 是基於(yu) C++開發代數的一個(ge) 模板庫:矩陣、矢量、數值解算器和相關(guan) 算法。相比較Matlab,優(you) 勢是利於(yu) 基於(yu) c++的3D相機開發
(大部分3D相機SDK都支持c++),劣勢是語法較複雜。本文目標是針對3D相機手眼標定過程中有關(guan) 矩陣算術的eigen庫運用進行學習(xi) 。
展示全部
背景
Eigen 是基於(yu) C++開發代數的一個(ge) 模板庫:矩陣、矢量、數值解算器和相關(guan) 算法。相比較Matlab,優(you) 勢是利於(yu) c++開發,劣勢是語法較複雜
環境配置
1.下載eigen源碼包,可解壓到任意位置
2.新建vc++工程,項目屬性 -> C/C++ -> 常規 -> 附加包含目錄 -> 編輯 -> 新建路徑 -> 選擇eigen文件夾所在路徑 -> 運行下麵的demo
Demo代碼
Matrix類介紹
Matrix類模板6個(ge) 參數
//共6個(ge)
Matrix int RowsAtCompileTime, int ColsAtCompileTime, int Options = 0, //默認是按列存儲(chu) 數據,可改成按行 int MaxRowsAtCompileTime = RowsAtCompileTime, //行數上限 int MaxColsAtCompileTime = ColsAtCompileTime> //列數上限 Matrinx類模板前三個(ge) 參數 數據類型,行數,列數,eigen已經定義(yi) 好了常用的,規律很好找 Matrix 示例 typedef Matrix typedef Matrix 特殊的Matrix類:Vector向量 是特殊的Matrix類,隻有一行或一列,定義(yi) 如下 typedef Matrix typedef Matrix 動態創建Matrix類對象 typedef Matrix typedef Matrix Matrix Matrix類構造函數 默認構造函數不會(hui) 動態分配內(nei) 存 對矩陣傳(chuan) 參總是優(you) 先傳(chuan) 行數 MatrixXf a(3,2); //3行2列 而給向量傳(chuan) 參 = 給向量傳(chuan) 大小: 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數據存儲(chu) 順序,總是先列後行 逗號初始化,總是先行後列,但數據存儲(chu) 順序還是不變 Vector無所謂,使用.transform()方法,即可轉換行列 盡量用固定大小的Matrix,內(nei) 存機製沒深究 Matrix&Vector 運算 加減法 原則很簡單,相同行列才能運算: 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 注意:運算符已被重載,不可2個(ge) 以上的矩陣同時參與(yu) 運算。此時用遍曆矩陣索引 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];} 係數乘除法 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 轉置,共軛,逆 //n階正交矩陣a特性: a*=aT,無共軛 Eigen::MatrixXd t = Eigen::MatrixXd::Random(3,3); cout << t.transpose() << endl; //a的轉置 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;//運算符重載,矩陣按行接收數據,但是儲(chu) 存機製依舊優(you) 先按列,即mat經過上述賦值後為(wei) [1,2 // 3,4],但取值時,mat(1,0)值為(wei) 3,mat(0,1)值為(wei) 2 Vector2d v1(-1, 1), v2(2, 0); //默認列向量 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的點乘和叉乘 點乘。純代數運算,適用與(yu) 任意長度的向量,前提是2個(ge) 向量長度相等 /* 計算公式: a(a1,a2,a3) , b(b1,b2,b3) a·b = a1*b1 + a2*b2 + a3*b3 幾何意義(yi) : a·b =|a|*|b|*cosθ */ 叉乘。用於(yu) 空間幾何,所以隻適用於(yu) 長度為(wei) 3的向量 /* 幾何意義(yi) axb =|a|*|b|*sinθ 注意:叉乘結果是向量,方向在z軸上,θ表示向量a到向量b的角度,右手法則(從(cong) a到b)確定z朝向 */ 代碼 #include #include int main() { using namespace Eigen; Vector3d v1(1, 2, 3); Vector3d v2(1, 1, 2); //點積方法1:用同維度的向量做點積 cout << "v1 點乘 v2 :" << endl << v.dot(v2) << endl; //點積方法2:向量轉化為(wei) 矩陣乘積來做點積 cout << "v1逆 點乘 v2 :" << endl << v.adjoint() * w << endl; //叉乘,外積 cout << "v1 叉乘 v2 :" << endl << v.cross(v2) << endl; return 1; } 輸出 v1 點乘 v2 : 9 v1 叉乘 v2 : 1 1 -1 v1逆 點乘 v2 : 9 Matrix內(nei) 部數據算術 代碼 #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;//最大值 //主對角線係數和 cout << "Here is mat.trace(): " << mat.trace() << endl; //某些函數可以重載 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類與(yu) Array類互換 矩陣運算用矩陣類,係數運算用數組類,互相轉換用.matrix()方法 和 .array()方法 求方陣的行列式值 Tatrix3d mat;mat << 1,2,3,4,5,6,7,8,9; //3階方陣 double result = matrix.determinant(); Matrix初始化方法 前麵提到過的逗號初始化 .Zero()方法初始化所有係數為(wei) 0 .Random()方法用隨機係數填充矩陣或數組 .Identity()方法初始化一個(ge) 單位矩陣 用分塊矩陣構造成一個(ge) 大矩陣 代碼 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 構造齊次矩陣(4*4) 旋轉向量,旋轉矩陣,歐拉角,四元數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 四元數注意點 輸入順序是[w,x,y,z] ,其中w是實數部分 儲(chu) 存和輸出順序是[x,y,z,w] ,其中w是實數部分 互為(wei) 相反數的2組四元數效果一樣 歐拉角注意點 pose轉matrix默認用'gba'形式( R = Rx * Ry * Rz),T1.rotation().eulerAngles(0, 1, 2) 線性代數求解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分解法 最精確但速度最慢的求解方法,用於(yu) 求解線性方程,在沒有解的情況下能逼近 代碼 #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; } 您可以通過我們(men) 的官方網站了解更多的国产欧美在线信息,或直接來電谘詢4006-888-532。