视觉SLAM十四讲 ch3 Ubuntu18.04 KDevelop的使用及Eigen实践 入门笔记

视觉SLAM十四讲 ch3 Ubuntu18.04 KDevelop的使用及Eigen实践 入门笔记

一、创建KDevelop项目

你的电脑上如果还没有安装kdevelop,请输入下列命令进行安装:

sudo apt-get install kdevelop

1.1 打开KDevelop,菜单栏Project->New From Template
在这里插入图片描述

1.2 选择Standard创建项目,这里以老师项目useEigen为例创建如下图

在这里插入图片描述
路径选择之后点击右上角的open
在这里插入图片描述

1.3 下一步,默认选择None,点击完成

在这里插入图片描述

1.4 弹出窗口自动创建build文件核默认加载环境cmake,选择ok

在这里插入图片描述

1.5项目创建好了,重命名main.cpp为eigenMatrix.cpp

在这里插入图片描述

二、编写程序

2.1 这里需要用到Eigen,你的电脑如果没有安装Eigen,请在终端输入以下命令进行安装

sudo apt-get install libeigen3-dev

查找Eigen头文件的位置语句

sudo updatedb
locate eigen3

2.2 安装好Eigen后就可以编写程序了,项目useEigen主要是两部分内容CMakeLists.txt和eigenMatrix.cpp

a.CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(useEigen)

set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-O3" )

# 添加Eigen头文件,路径是Eigen头文件的位置
include_directories( "/usr/include/eigen3" )

# in osx and brew install
# include_directories( /usr/local/Cellar/eigen/3.3.3/include/eigen3 )

add_executable( eigenMatrix eigenMatrix.cpp )

b.eigenMatrix.cpp

#include <iostream>
using namespace std;

#include<ctime>

//Eigen核心部分
#include<Eigen/Core>

//稠密矩阵的代数运算(逆、特征值等)
#include<Eigen/Dense>
using namespace Eigen; //可以将Eigen::Matrix简写为Matrix

#define MATRIX_SIZE 50

//演示Eigen基本类型的使用


int main(int argc, char **argv) {
    
    //Eigen中所有向量和矩阵都是Eigen::Matrix,他是一个模板类。它的前三个参数为:数据类型,行,列
    //声明一个2×3的float矩阵
    Matrix<float, 2, 3> matrix_23;
    
    //同时,Eigen通过typedef提供了许多内置类型,不过底层仍是Eigen::Matrix
    //例如,Vector3d实质上是Eigen::Matrix<double, 3, 1>,即三维向量
    Vector3d v_3d;
    //这是一样的
    Matrix<float, 3, 1> vd_3d;
    
    //Matrix3d实质上是Eigen::Matrix<double, 3, 3>
    Matrix3d matrix_33 = Matrix3d::Zero();//初始化为零
    //如果不确定矩阵大小,可以使用动态大小的矩阵
    Matrix<double, Dynamic, Dynamic> matrix_dynamic;
    //更简单的定义矩阵方式
    MatrixXd matrix_x;
    //这种类型很多,不一一列举
    
    
    //下面是对Eigen阵的操作
    //输入数据(初始化)
    matrix_23 << 1, 2, 3, 4, 5, 6;
    //输出
    cout << "matrix 2 × 3 from 1 to 6: \n" << matrix_23 << endl;
    
    //用()+for循环访问矩阵中的元素
    cout << "print matrix 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;
    
    //但是在Eigen中不能混合两种不同类型的矩阵,以下是错误举例:     
    //Matrix<double, 2, 1> result_wrong_type = matrix_23 * v_3d;  //matrix_23是float型,v_3d是double型
    //应该显示转换成double型
    Matrix<double, 2, 1> result1 = matrix_23.cast<double>() * v_3d;
    cout << "[1,2,3;4,5,6]*[3,2,1] = " << result1.transpose() << endl;
    
    Matrix<float, 2, 1> result2 = matrix_23 * vd_3d;
    cout << "[1,2,3;4,5,6]*[4,5,6] = " << result2.transpose() << endl;
    
    //同样,你不能搞错矩阵的维度。矩阵相称只有在第一个矩阵的列数和第二个矩阵的行数相同才有意义
    //取消下面注释,看报什么错
    //Eigen::Matrix<double, 2, 3> result_wrong_dimension = matrix_23.cast<double>() * v_3d;
    
    //一些矩阵运算,四则运算直接用+-*/即可
    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 << "times 10: \n" << 10 * matrix_33 << endl;           //数乘
    cout << "inverse: \n" << matrix_33.inverse() << endl;       //逆
    cout << "det:" << matrix_33.determinant() << endl;          //行列式
    
    //特征值
    //实对称矩阵可以保证对角化成功
    SelfAdjointEigenSolver<Matrix3d> eigen_solver(matrix_33.transpose() * matrix_33);
    cout << "Eigen values = \n" << eigen_solver.eigenvalues() << endl;
    cout << "Eigen vectors = \n" << eigen_solver.eigenvectors() << endl;
    
    //解方程
    //求解matrix_NN * x = v_Nd 方程
    //N的大小在宏中定义,由随机数生成
    //直接求逆是最直接的,但是计算量大
    
    Matrix<double, MATRIX_SIZE, MATRIX_SIZE> matrix_NN = MatrixXd::Random(MATRIX_SIZE, MATRIX_SIZE);
    matrix_NN = matrix_NN * matrix_NN.transpose();   //矩阵乘它的转置,保持半正定
    Matrix<double, MATRIX_SIZE, 1> v_Nd = MatrixXd::Random(MATRIX_SIZE, 1);
    
    clock_t time_stt = clock(); // 计时
    
    //直接求逆
    Matrix<double, MATRIX_SIZE, 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;
}

2.3 代码写好后,首先点击Build,编译CMakeLists.txt成功后,自动生成可执行文件eigenMatrix
在这里插入图片描述
若未生成可执行文件,右击项目名称,reload重载
在这里插入图片描述
2.4 配置启动器。单击“Run”中的“Configure Launches…(启动器配置)”
在这里插入图片描述
进入配置页面。选中自己建立的工程之后单击Add,选下拉菜单中的工程名称,然后点击Apply以及OK。其过程如下图所示。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

选择启动器。在“Run”的下拉菜单下面将鼠标移动至第一行“Current Launch Configuration”,然后在子菜单中选择与我们建立的工程名称一致的启动器useEigen:eigenMatrix。

在这里插入图片描述

2.5 配置完成后,点击菜单栏的编译“Build”,等100%完成后;点击执行“Execute”,最终结果如下:

/home/mao1/SLAM/Code/MyCode/ch3/useEigen/build/> /home/mao1/SLAM/Code/MyCode/ch3/useEigen/build/eigenMatrix
matrix 2 × 3 from 1 to 6: 
1 2 3
4 5 6
print matrix 2 × 31	2	3	
4	5	6	
[1,2,3;4,5,6]*[3,2,1] = 10 28
[1,2,3;4,5,6]*[4,5,6] = 32 77
random matrix: 
 0.680375   0.59688 -0.329554
-0.211234  0.823295  0.536459
 0.566198 -0.604897 -0.444451
transpose: 
 0.680375 -0.211234  0.566198
  0.59688  0.823295 -0.604897
-0.329554  0.536459 -0.444451
sum:1.61307
trace:1.05922
times 10: 
 6.80375   5.9688 -3.29554
-2.11234  8.23295  5.36459
 5.66198 -6.04897 -4.44451
inverse: 
-0.198521   2.22739    2.8357
  1.00605 -0.555135  -1.41603
 -1.62213   3.59308   3.28973
det:0.208598
Eigen values = 
0.0242899
 0.992154
  1.80558
Eigen vectors = 
-0.549013 -0.735943  0.396198
 0.253452 -0.598296 -0.760134
-0.796459  0.316906 -0.514998
time of normal inverse is 0.103ms
x = -55.7896 -298.793  130.113 -388.455 -159.312  160.654 -40.0416 -193.561  155.844  181.144  185.125 -62.7786  19.8333 -30.8772 -200.746  55.8385 -206.604  26.3559 -14.6789  122.719 -221.449   26.233  -318.95 -78.6931  50.1446  87.1986 -194.922  132.319  -171.78 -4.19736   11.876 -171.779  48.3047  84.1812 -104.958 -47.2103 -57.4502 -48.9477 -19.4237  28.9419  111.421  92.1237 -288.248 -23.3478  -275.22 -292.062  -92.698  5.96847 -93.6244  109.734
time of Qr decomposition is 0.049ms
x = -55.7896 -298.793  130.113 -388.455 -159.312  160.654 -40.0416 -193.561  155.844  181.144  185.125 -62.7786  19.8333 -30.8772 -200.746  55.8385 -206.604  26.3559 -14.6789  122.719 -221.449   26.233  -318.95 -78.6931  50.1446  87.1986 -194.922  132.319  -171.78 -4.19736   11.876 -171.779  48.3047  84.1812 -104.958 -47.2103 -57.4502 -48.9477 -19.4237  28.9419  111.421  92.1237 -288.248 -23.3478  -275.22 -292.062  -92.698  5.96847 -93.6244  109.734
time of ldlt decomposition is0.021ms
x = -55.7896 -298.793  130.113 -388.455 -159.312  160.654 -40.0416 -193.561  155.844  181.144  185.125 -62.7786  19.8333 -30.8772 -200.746  55.8385 -206.604  26.3559 -14.6789  122.719 -221.449   26.233  -318.95 -78.6931  50.1446  87.1986 -194.922  132.319  -171.78 -4.19736   11.876 -171.779  48.3047  84.1812 -104.958 -47.2103 -57.4502 -48.9477 -19.4237  28.9419  111.421  92.1237 -288.248 -23.3478  -275.22 -292.062  -92.698  5.96847 -93.6244  109.734
*** Finished ***

在终端中执行程序,首先找到编译生成的可执行文件eigenMatrix的路径
在这里插入图片描述
在终端输入:

./eigenMatrix 

得到输出:
在这里插入图片描述

  • 16
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在Ubuntu上还没有安装KDevelop,你可以通过执行以下命令进行安装: ``` sudo apt-get install kdevelop ``` 如果你需要安装KDevelop的语言包,你可以执行以下命令来安装: ``` sudo apt-get install kdevelop-l10n ``` 另外,如果你没有安装aptitude软件包管理器,你需要先安装aptitude,然后才能执行aptitude命令来搜索KDevelop的相关内容: ``` sudo apt-get install aptitude aptitude search kdevelop ``` 这些命令将帮助你在Ubuntu上安装KDevelop并进行相应的配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Kdevelopubuntu下的安装和汉化详细步骤](https://blog.csdn.net/qq_43147508/article/details/89177037)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [视觉SLAM十四 ch3 Ubuntu18.04 KDevelop使用Eigen实践 入门笔记](https://blog.csdn.net/qq_41339558/article/details/126023845)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值