C + + Image aid Kit Eigen Getting Started code learning (good fit OpenCV)

Source: Internet
Author: User

The first thing to say is that the toolkit is very handy and can be extracted. Add the path to this folder in the project.


Here are some code exercises:


#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); This is a 3*3 matrix with a value of 1. 2 cout << "n =" << endl << n << Endl; m = (M + matrixxd::constant (3,3,1.2)) *;  cout << "m =" << Endl << m << Endl;  Vectorxd V (3);  V << 1, 2, 3;  The following results indicate that the column vectors are 3*1. cout << "M * v =" << Endl << m * v << Endl;  return 0;}  

The following transpose is used:

#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's mat*mat:\n" << mat*mat << Std::endl;  Std::cout << "Here's mat*u:\n" << mat*u << Std::endl;  Std::cout << "Here is u^t*mat:\n" << u.transpose () *mat << Std::endl;  can be seen. Transpose () is used to seek 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;  }  

The following example removes a sub-matrix from a matrix (note that the two methods are the same.)The paper that was reproduced is wrong here.。 )


#include <Eigen/Dense>  #include <iostream>  using namespace std;  int main ()  {  eigen::matrixxf m (+);  M << 1, 2, 3, 4,            5, 6, 7, 8,            9,10,11,12,            13,14,15,16;  cout << "Block in the Middle" << Endl;  Two different ways cout << m.block<2,3> << 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 << end l;  cout << M.block (0,0,i,i) << Endl << Endl;  }  }  

Here are some operations of the vector:

#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; This is where the 2nd and 5th numbers are chosen respectively EIGEN::VECTORXF v1 (6);  Can be seen to be the same 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;  

Come down and look at the basic assignment situation:

#include <Eigen/Core> #include <iostream> #define SIZE 2using 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; Such an assignment m (1,2,3,4,5,6) is an incorrect vector definition when this is the case    cout << m <<endl;cout<<m.col (1) <<endl; VECTORXF V (4); A vector of 4 float ' sv[0] = 1; V[1] = 2;v (2) = 3; V (3) = 4;cout << "V:" << v << Std::endl; VECTOR4F v2 (0,9,8,2);//This place is written VECTORXF v2 (0,9,8,2) will be an error. cout << "v2:" << v2 << Std::endl;}

Here's how to assign the following values:Note that a comma is used between the”

#include <Eigen/Core> #include <iostream> #define SIZE 2using 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 (),//different assignment mode E. finished () is use D to get the actual matrix object once the comma initialization of our temporary submatrix are done. Matrixxf::zero (3,cols-3),//Matrixxf::zero (rows-3,3), matrixxf::identity (rows-3,cols-3);// Use the unit matrix to fill the bottom matrix cout << M<<endl;}



C + + Image aid Kit Eigen Getting Started code learning (good fit OpenCV)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.