首页 > 代码库 > C++图像辅助工具包Eigen入门代码学习(很好的配合Opencv)
C++图像辅助工具包Eigen入门代码学习(很好的配合Opencv)
首先要说这个工具包非常的方便,解压出来就可以用了。在工程里面加入这个文件夹的路径就可以了。
下面是一些代码的练习:
#include <iostream> #include <Eigen/Dense> using namespace Eigen; using namespace std; int main() { MatrixXd m = MatrixXd::Random(3,3); MatrixXd n; n=MatrixXd::Constant(3,3,1.2); //这是一个3*3的矩阵,里面的值全部是1。2 cout << "n =" << endl << n << endl; m = (m + MatrixXd::Constant(3,3,1.2)) * 50; cout << "m =" << endl << m << endl; VectorXd v(3); v << 1, 2, 3; // 以下的结果说明是列向量3*1的。 cout << "m * v =" << endl << m * v << endl; return 0; }
下面会用到转置:
#include <iostream> #include <Eigen/Dense> using namespace Eigen; int main() { Matrix2d mat; mat << 1, 2, 3, 4; Vector2d u(-1,1), v(2,0); std::cout << "Here is mat*mat:\n" << mat*mat << std::endl; std::cout << "Here is mat*u:\n" << mat*u << std::endl; std::cout << "Here is u^T*mat:\n" << u.transpose()*mat << std::endl; //可以看出来.transpose()用来求转置 std::cout << "Here is u^T*v:\n" << u.transpose()*v << std::endl; std::cout << "Here is u*v^T:\n" << u*v.transpose() << std::endl; std::cout << "Let's multiply mat by itself" << std::endl; mat = mat*mat; std::cout << "Now mat is mat:\n" << mat << std::endl; }
下面的例子是从一个矩阵当中取出一个子矩阵(注意两种方式是一样的。转载的那篇论文里面这里说错了。)
#include <Eigen/Dense> #include <iostream> using namespace std; int main() { Eigen::MatrixXf m(4,4); m << 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12, 13,14,15,16; cout << "Block in the middle" << endl; // 两种不同的方式 cout << m.block<2,3>(1,1) << endl << endl; cout << "other kind Block in the middle" << endl; cout << m.block(1,1,2,3) << endl << endl; for (int i = 1; i <= 3; ++i) { cout << "Block of size " << i << "x" << i << endl; cout << m.block(0,0,i,i) << endl << endl; } }
下面是向量的一些操作:
#include <Eigen/Dense> #include <iostream> using namespace std; int main() { Eigen::ArrayXf v(6); v << 1, 2, 3, 4, 5, 6; cout << "v.head(3) =" << endl << v.head(3) << endl << endl; cout << "v.tail<3>() = " << endl << v.tail<3>() << endl << endl; v.segment(1,4) *= 2; cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl; //这里选出第2和第5个数来分别的*2 Eigen::VectorXf v1(6); //可以看出来是一样的 v1 << 1 ,2 ,2 , 4, 5, 6; cout << "v1.head(3) =" << endl << v1.head(3) << endl << endl; cout << "v.tail<3>() = " << endl << v1.tail<3>() << endl << endl; v1.segment(1,4) *= 2; cout << "after 'v.segment(1,4) *= 2', v =" << endl << v1 << endl; }
下来看一下基本的赋值情况:
#include <Eigen/Core> #include <iostream> #define SIZE 2 using namespace std; using namespace Eigen; void main() { MatrixXi m(SIZE,SIZE+1); // a (size)x(size+1)-matrix of int's cout<<m.cols()<<endl; m<<1,2,3,4,5,6; // 这样的赋值 m(1,2,3,4,5,6)是不正确的Vector定义时才是这样的 cout << m <<endl; cout<<m.col(1)<<endl; VectorXf v(4); // a vector of 4 float's v[0] = 1; v[1] = 2; v(2) = 3; v(3) = 4; cout << "v:" << v << std::endl; Vector4f v2(0,9,8,2);//这个地方写成VectorXf v2(0,9,8,2)就会报错。 cout << "v2:" << v2 << std::endl; }
下面的这张赋值方式:注意之间用的是逗号“ ,”
#include <Eigen/Core> #include <iostream> #define SIZE 2 using namespace std; using namespace Eigen; void main() { int rows=5, cols=5; MatrixXf m(rows,cols); m << (Matrix3f() << 1, 2, 3, 4, 5, 6, 7, 8, 9).finished(),//不一样的赋值方式e .finished() is used to get the actual matrix object once the comma initialization of our temporary submatrix is done. MatrixXf::Zero(3,cols-3),//用0来填补 MatrixXf::Zero(rows-3,3), MatrixXf::Identity(rows-3,cols-3);//用单位矩阵来填补最下面的矩阵 cout << m<<endl; }
C++图像辅助工具包Eigen入门代码学习(很好的配合Opencv)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。