视觉SLAM十四讲作业练习(3)

.5 sophus的使用

书中提供的代码

useSophus.cpp

#include <iostream>
#include <cmath>
#include <Eigen/Core>
#include <Eigen/Geometry>

#include "sophus/se3.hpp"
#include "sophus/so3.hpp"

using namespace std;
using namespace Eigen;

int main(int argc, char *argv[])
{
    //沿z轴旋转90度的旋转矩阵
    Matrix3d R = AngleAxisd(M_PI / 2, Vector3d(0, 0, 1)).toRotationMatrix();

    //四元数
    Quaterniond q(R);
    Sophus::SO3d SO3_R(R);   //Sophus::SO3d可以直接从旋转矩阵构造
    Sophus::SO3d SO3_q(q);   //也可通过四元数构造

    cout << "SO(3) from matrix: \n" << SO3_R.matrix() << endl;
    cout << "SO(3) from quaternion: \n" << SO3_q.matrix() << endl;
    cout << "上述二者等价!" << endl;

    //使用对数映射获得它的李代数
    Vector3d so3 = SO3_R.log();
    cout << "so3 = " << so3.transpose() << endl;

    //hat为向量到反对称矩阵
    cout << "so3 hat = \n" << Sophus::SO3d::hat(so3) << endl;
    //vee 为反对称到向量
    cout << "so3 hat vee = \n" << Sophus::SO3d::vee(Sophus::SO3d::hat(so3)).transpose() << endl;
    
    //增量扰动模型的更新
    Vector3d updated_so3(1e-4, 0, 0);    //假设的更新量
    Sophus::SO3d SO3_updated = Sophus::SO3d::exp(updated_so3) * SO3_R;
    cout << "SO3 updated = \n" << SO3_updated.matrix() << endl;

    cout << "---------------------------------------------------------------" << endl;
    //对SE(3)操作大同小异
    Vector3d t(1, 0, 0);    //沿x周平移1
    Sophus::SE3d SE3_Rt(R, t);  //由R,t构造SE(3)
    Sophus::SE3d SE3_qt(q, t);

    cout << "SE(3) from matrix: \n" << SE3_Rt.matrix() << endl;
    cout << "SE(3) from quaternion: \n" << SE3_qt.matrix() << endl;

    //李代数为se(3)为一个六维向量
    typedef Eigen::Matrix<double, 6, 1> Vector6d;
    Vector6d se3 = SE3_Rt.log();
    cout << "se3 = " << se3.transpose() << endl;

    cout << "se3 hat = \n" << Sophus::SE3d::hat(se3) << endl;
    cout << "se3 hat vee = \n" << Sophus::SE3d::vee(Sophus::SE3d::hat(se3)).transpose() << endl;


    Vector6d update_se3;    //更新量
    update_se3.setZero();
    update_se3(0, 0) = 1e-4;
    Sophus::SE3d SE3_updated = Sophus::SE3d::exp(update_se3) * SE3_Rt;
    cout << "SE3 updated = \n" << SE3_updated.matrix() << endl;

    return 0;
}

关键:CMakeLists.txt 的编写

按照书中所写的代码,在我的电脑上会报错

1)需要添加eigen3

有点奇怪,重新试了一下发现不需要了。。。。

2)target_link_libraries(useSophus Sophus::Sophus )必需,不然会报超级多的错误。

解决来源:https://blog.csdn.net/weixin_44387324/article/details/118904871

3)target_link_libraries(useSophus ${Sophus_LIBRARIES})感觉最好加上

cmake_minimum_required(VERSION 2.8)
project(useSophus)

find_package(Sophus REQUIRED)
include_directories(${Sophus_INCLUDE_DIR})

include_directories(/usr/local/include/eigen3)
add_executable(useSophus useSophus.cpp)
target_link_libraries(useSophus Sophus::Sophus )
#target_link_libraries(useSophus ${Sophus_LIBRARIES})

7.轨迹的描绘

draw_trajectory.cpp

#include <string>
#include <iostream>
#include <fstream>
#include <unistd.h>

#include <sophus/se3.hpp>

// need pangolin for plotting trajectory
#include <pangolin/pangolin.h>

using namespace std;

// path to trajectory file
string trajectory_file = "../trajectory.txt";

// function for plotting trajectory, don't edit this code
// start point is red and end point is blue
void DrawTrajectory(vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>>);

bool ReadTrajectory(const string &trajectory_file_path, vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> &poses);

int main(int argc, char **argv) {
    /*
        vector<Sophus::SE3, Eigen::aligned_allocator< Sophus::SE3>> poses;
        //这句实际上表达的是vector< Sophus::SE3> poses的意思
        但是在Eigen管理内存和C++11中的方法是不一样的,所以需要单独强调元素的内存分配和管理。
        (特别注意Eigen库数据结构内存对齐问题)
    */
    vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> poses; //vector容器

    // 读取文件
    if (!ReadTrajectory(trajectory_file, poses)) {
        cout << "读取失败...\n";
    } 
    else {
        cout << "读取成功!\n";
    }

    // draw trajectory in pangolin
    DrawTrajectory(poses);
    return 0;
}

bool ReadTrajectory(const string &trajectory_file_path, vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> &poses){
    // 读文件
    // 2创建流对象
    ifstream reader;

    // 3.1打开文件
    reader.open(trajectory_file_path, ios::in);
    
    // 3.2判断是否打开成功
    if (!reader.is_open()) {
        cout << "文件打开失败!!! " << trajectory_file_path << '\n';
        return false;
    }

    // 4读数据
    // 将读到的数据储存在SE(3)矩阵中
    Eigen::Quaterniond q(1,0,0,0);  //四元数
    Eigen::Vector3d t(0,0,0);       //平移
    double time;

    while (!reader.eof())   //eof() 判断文件是否为空或者是否读到文件结尾
    {   //文件存储的格式为[t,tx,ty,tz,qx,qy,qz]
        reader >> time;
        // read t
        reader >> t[0];
        reader >> t[1];
        reader >> t[2];
        // read q
        reader >> q.x();
        reader >> q.y();
        reader >> q.z();
        reader >> q.w();
        Sophus::SE3d SE3 (q,t);
        poses.push_back(SE3);
    }

    reader.close();

    return true;
}

void DrawTrajectory(vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> poses) {
    if (poses.empty()) {
        cerr << "Trajectory is empty!\n";
        return;
    }

    // create pangolin window and plot the trajectory
    pangolin::CreateWindowAndBind("Trajectory Viewer", 1024, 768);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    pangolin::OpenGlRenderState s_cam(
            pangolin::ProjectionMatrix(1024, 768, 500, 500, 512, 389, 0.1, 1000),
            pangolin::ModelViewLookAt(0, -0.1, -1.8, 0, 0, 0, 0.0, -1.0, 0.0)
    );

    pangolin::View &d_cam = pangolin::CreateDisplay()
            .SetBounds(0.0, 1.0, pangolin::Attach::Pix(175), 1.0, -1024.0f / 768.0f)
            .SetHandler(new pangolin::Handler3D(s_cam));


    while (pangolin::ShouldQuit() == false) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        d_cam.Activate(s_cam);
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

        glLineWidth(2);
        for (size_t i = 0; i < poses.size() - 1; i++) {
            glColor3f(1 - (float) i / poses.size(), 0.0f, (float) i / poses.size());
            glBegin(GL_LINES);
            auto p1 = poses[i], p2 = poses[i + 1];
            glVertex3d(p1.translation()[0], p1.translation()[1], p1.translation()[2]);
            glVertex3d(p2.translation()[0], p2.translation()[1], p2.translation()[2]);
            glEnd();
        }
        pangolin::FinishFrame();
        usleep(5000);   // sleep 5 ms
    }
}

数据读取部分代码

bool ReadTrajectory(const string &trajectory_file_path, vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> &poses){
    // 读文件
    // 2创建流对象
    ifstream reader;

    // 3.1打开文件
    reader.open(trajectory_file_path, ios::in);
    
    // 3.2判断是否打开成功
    if (!reader.is_open()) {
        cout << "文件打开失败!!! " << trajectory_file_path << '\n';
        return false;
    }

    // 4读数据
    // 将读到的数据储存在SE(3)矩阵中
    Eigen::Quaterniond q(1,0,0,0);  //四元数
    Eigen::Vector3d t(0,0,0);       //平移
    double time;

    while (!reader.eof())   //eof() 判断文件是否为空或者是否读到文件结尾
    {   //文件存储的格式为[t,tx,ty,tz,qx,qy,qz]
        reader >> time;
        // read t
        reader >> t[0];
        reader >> t[1];
        reader >> t[2];
        // read q
        reader >> q.x();
        reader >> q.y();
        reader >> q.z();
        reader >> q.w();
        Sophus::SE3d SE3 (q,t);
        poses.push_back(SE3);	//在vector尾部加入一个数据
    }

    reader.close();

    return true;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(trajectory)

find_package(Pangolin REQUIRED)
include_directories(${Pangolin_INCLUDE_DIRS})

find_package(Sophus REQUIRED)
include_directories(${Sophus_INCLUDE_DIR})

add_executable(draw_trajectory draw_trajectory.cpp)
target_link_libraries(draw_trajectory Sophus::Sophus )
target_link_libraries(draw_trajectory ${Pangolin_LIBRARIES} ${Sophus_LIBRARIES})

8.轨迹的误差

与上节相比需添加轨迹误差的计算,以及将两个轨迹画到同一个图中

误差计算(课本上有):

double ComputeRMSE( const vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>>& poses_estimated,
                    const vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>>& poses_groundtruth) {
    //算法参考课本
    double rmse = 0.0;
    for (int i = 0; i < poses_estimated.size(); i++) {
        Sophus::SE3d p_true = poses_groundtruth[i];
        Sophus::SE3d p_esti = poses_estimated[i];
        // Compute norm of error
        double error = (p_true.inverse() * p_esti).log().norm();
        rmse += error * error;
    }

    rmse = sqrt(rmse / double(poses_estimated.size()));

    return rmse;
}

主程序:

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <pangolin/pangolin.h>
#include <unistd.h>
#include <sophus/se3.hpp>

using namespace std;

string estimation_file_path = "../estimated.txt";
string groundtruth_file_path = "../groundtruth.txt";

bool ReadTrajectory(const string &trajectory_file_path, std::vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> &poses){
    // 读文件
    // 2创建流对象
    ifstream reader;

    // 3.1打开文件
    reader.open(trajectory_file_path, ios::in);
    
    // 3.2判断是否打开成功
    if (!reader.is_open()) {
        cout << "文件打开失败!!! " << trajectory_file_path << '\n';
        return false;
    }

    // 4读数据
    // 将读到的数据储存在SE(3)矩阵中
    Eigen::Quaterniond q(1,0,0,0);  //四元数
    Eigen::Vector3d t(0,0,0);       //平移
    double time;

    while (!reader.eof())   //eof() 判断文件是否为空或者是否读到文件结尾
    {   //文件存储的格式为[t,tx,ty,tz,qx,qy,qz]
        reader >> time;
        // read t
        reader >> t[0];
        reader >> t[1];
        reader >> t[2];
        // read q
        reader >> q.x();
        reader >> q.y();
        reader >> q.z();
        reader >> q.w();
        Sophus::SE3d SE3 (q,t);
        poses.push_back(SE3);
    }

    reader.close();

    return true;
}

double ComputeRMSE( const vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>>& poses_estimated,
                    const vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>>& poses_groundtruth) {
    //算法参考课本
    double rmse = 0.0;
    for (int i = 0; i < poses_estimated.size(); i++) {
        Sophus::SE3d p_true = poses_groundtruth[i];
        Sophus::SE3d p_esti = poses_estimated[i];
        // Compute norm of error
        double error = (p_true.inverse() * p_esti).log().norm();
        rmse += error * error;
    }

    rmse = sqrt(rmse / double(poses_estimated.size()));

    return rmse;
}

void DrawTrajectory(vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> &poses_estimated,
                    vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> &poses_groundtruth) 
{
    if (poses_estimated.empty()) {
        cerr << "Trajectory_esti is empty!\n";
        return;
    }
    if (poses_groundtruth.empty()) {
        cerr << "Trajectory_true is empty!\n";
        return;
    }

    // create pangolin window and plot the trajectory
    pangolin::CreateWindowAndBind("Trajectory Viewer", 1024, 768);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    pangolin::OpenGlRenderState s_cam(
            pangolin::ProjectionMatrix(1024, 768, 500, 500, 512, 389, 0.1, 1000),
            pangolin::ModelViewLookAt(0, -0.1, -1.8, 0, 0, 0, 0.0, -1.0, 0.0)
    );

    pangolin::View &d_cam = pangolin::CreateDisplay()
            .SetBounds(0.0, 1.0, pangolin::Attach::Pix(175), 1.0, -1024.0f / 768.0f)
            .SetHandler(new pangolin::Handler3D(s_cam));


    while (pangolin::ShouldQuit() == false) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        d_cam.Activate(s_cam);
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

        glLineWidth(2);
        for (size_t i = 0; i < poses_estimated.size() - 1; i++) {
            glColor3f(1 - (float) i / poses_estimated.size(), 0.0f, (float) i / poses_estimated.size());
            glBegin(GL_LINES);
            auto p1 = poses_estimated[i], p2 = poses_estimated[i + 1];
            glVertex3d(p1.translation()[0], p1.translation()[1], p1.translation()[2]);
            glVertex3d(p2.translation()[0], p2.translation()[1], p2.translation()[2]);
            glEnd();
        }        
        for (size_t i = 0; i < poses_groundtruth.size() - 1; i++) {
            glColor3f(1 - (float) i / poses_groundtruth.size(), 0.0f, (float) i / poses_groundtruth.size());
            glBegin(GL_LINES);
            auto p1_t = poses_groundtruth[i], p2_t = poses_groundtruth[i + 1];
            glVertex3d(p1_t.translation()[0], p1_t.translation()[1], p1_t.translation()[2]);
            glVertex3d(p2_t.translation()[0], p2_t.translation()[1], p2_t.translation()[2]);
            glEnd();
        }
        pangolin::FinishFrame();
        usleep(5000);   // sleep 5 ms
    }
}

int main(int argc, char** argv) {
    vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> poses_estimated;
    vector<Sophus::SE3d, Eigen::aligned_allocator<Sophus::SE3d>> poses_groundtruth;

    // read estimation file
    if (!ReadTrajectory(estimation_file_path, poses_estimated)) {
        cout << "Reading estimation file fails...\n";
    } 
    else {
        cout << "Reading estimation file succeeds!\n";
    }

    // read groundtruth file
    if (!ReadTrajectory(groundtruth_file_path, poses_groundtruth)) {
        cout << "Reading groundtruth file fails...\n";
    } 
    else {
        cout << "Reading groundtruth file succeeds!\n";
    }

    cout << ComputeRMSE(poses_estimated, poses_groundtruth) << '\n';

    DrawTrajectory(poses_estimated,poses_groundtruth);

    return 0;
}

总结与反思:此程序总共分为三部分,一是将文件中的数据读入,二是通过pangolin库将读入的轨迹数据画出来,三是误差算法。欠缺的地方是使用pangoin库画出轨迹,因为看视频的时候没认真看,作业也仅仅是现在才开始练习,接下来学习一下这个可视化绘图库吧。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《视觉SLAM十四》第三章主要介绍了视觉SLAM中的关键技术——特征提取和描述子。本章首先介绍了特征点的概念和特征点的选择原则。特征点即图像中具有鲁棒性和区分度的点,可以通过对其进行检测和描述来进行特征匹配和跟踪。在进行特征提取时,作者介绍了常见的特征检测算法,如Harris角点检测、SIFT和SURF算法等,并对其进行了比较和分析。 接着,本章详细阐述了特征描述子的概念和作用。特征描述子是对特征点周围区域的图像信息进行编码,以实现特征匹配和跟踪。常见的特征描述子包括SIFT、SURF和ORB等。作者从描述子的表示形式、计算方式和匹配方法等方面进行了介绍,并对它们进行了比较和评价。同时,还提到了基于二进制描述子的方法,如BRIEF、BRISK和FREAK等。 在特征匹配方面,本章介绍了特征描述子匹配的基本原理和流程。以基于特征点的视觉SLAM为例,作者详细解释了特征点的匹配过程,包括特征点的选择、特征点描述子匹配和筛选等步骤。并介绍了如何通过验证特征点的三角化和PnP求解来估计相机的位姿。 此外,本章还介绍了一些特定场景下的特征点选择和提取策略,如动态环境下的特征点追踪和关键帧选择等。 综上所述,《视觉SLAM十四》第三章主要介绍了特征提取和描述子在视觉SLAM中的重要性和应用。通过对特征点的检测和描述,可以实现特征匹配和跟踪,为后续的相机位姿估计和建图提供基础。该章内容详细且通俗易懂,对于学习和理解视觉SLAM有着重要的指导作用。 ### 回答2: 《视觉SLAM十四-Ch3》主要介绍了视觉SLAM(同时定位与建图)技术的基本原理和实现方法。本章主要涵盖了三维几何表示和变换、相机模型和相机姿态以及特征提取与匹配等内容。 首先,本章介绍了三维几何表示和变换的概念。通过介绍欧氏空间中的点、向量和坐标变换,深入解释了相机在三维空间中的位置和朝向的表示方式。同时,引入了齐次坐标和投影矩阵的概念,为后续的相机模型和姿态估计打下了基础。 其次,本章详细解了相机模型和相机姿态的原理与应用。其中,介绍了针孔相机模型,分析了图像坐标和相机坐标之间的映射关系。通过投影矩阵的推导,给出了透视投影和仿射投影的公式,并解释了相机焦距和主点的含义。此外,还介绍了如何通过计算相机的外参矩阵来估计相机的姿态,以及如何将图像坐标转换为相机坐标。 最后,本章介绍了特征提取与匹配的技术。首先,介绍了角点和边缘点的概念,以及如何利用差分和梯度计算来检测图像中的角点和边缘点。然后,介绍了如何通过特征描述符来表示图像中的特征点,并通过特征匹配算法找到两幅图像之间的对应关系。特征提取与匹配是视觉SLAM中非常重要的步骤,对于后续的相机定位和建图至关重要。 综上所述,《视觉SLAM十四-Ch3》通过系统地介绍了视觉SLAM技术的基本概念和实现方法,包括三维几何表示和变换、相机模型和相机姿态的原理与应用,以及特征提取与匹配的技术。这些内容为读者深入理解和掌握SLAM技术提供了很好的基础。 ### 回答3: 视觉SLAM(Simultaneous Localization and Mapping)是一种通过计算机视觉技术,实现机器智能的同时实时定位和地图构建的方法。在《视觉SLAM十四》第三中,主要介绍了视觉SLAM的基本概念和关键技术。 首先,解了视觉SLAM的理论基础,包括自我运动估计和地图构建两个部分。自我运动估计是通过相邻帧之间的视觉信息,计算相机在三维空间中的运动,从而实现机器的实时定位;地图构建是通过对场景中特征点的观测和跟踪,建立起一个三维空间中的地图。这两个过程相互影响,通过不断迭代优化,实现高精度的定位和地图构建。 接着,解了基于特征的视觉SLAM算法。特征提取与描述是建立视觉SLAM系统的关键步骤,通过提取场景中的特征点,并为其生成描述子,来实现特征点的匹配和跟踪。同时,还介绍了一些常用的特征点提取和匹配算法,如FAST、SIFT等。 在SLAM框架方面,本节还介绍了基于视觉的前端和后端优化。前端主要负责实时的特征跟踪和估计相机运动,后端则是通过优化技术,对前端输出的轨迹和地图进行优化求解,从而提高系统的精度和鲁棒性。 最后,本节提到了几个视觉SLAM的应用场景,如自主导航、增强现实等。这些应用对于实时高精度的定位和地图建立都有着很高的要求,因此,视觉SLAM的技术在这些领域有着广泛的应用前景。 总的来说,《视觉SLAM十四》第三视觉SLAM的基本概念和关键技术进行了系统的介绍。理论基础、特征提取与描述、SLAM框架和应用场景等方面的内容都给出了详细的解释和案例,有助于读者更好地理解和应用视觉SLAM技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值