一定把vslam拿下!!!(小白版)// 部分内容转载自他人博客!侵删
深蓝学院视觉十四讲课后作业——ch3 李群李代数
本文记录了学习的部分笔记心得与课后习题 主要用途为学习巩固、方便自己查阅
部分内容参阅互联网或者书籍知识,部分图片来自网络,欢迎批评指正
如有问题,可及时与我沟通讨论!
群的性质(1 分,约 1 小时)
请根据群定义,求解以下问题:
1、{Z,+} 是否为群?若是,验证其满足群定义;若不是,说明理由。
2、{N,+} 是否为群?若是,验证其满足群定义;若不是,说明理由。
其中 Z 为整数集,N 为自然数集。
3、解释什么是阿贝尔群。并说明矩阵及乘法构成的群是否为阿贝尔群。
验证向量叉乘的李代数性质 (2 分,约 1 小时)
推导 SE(3) 的指数映射 (1 分,约 1 小时)
伴随 (2 分,约 1 小时)
常见函数的求导应用 (2 分,约 2 小时)
暂时未做…
轨迹的描绘 (2 分,约 1 小时)
代码部分如下://模板类sophus
#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
}
}
CMakeList如下:
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})
运行结果如下:
代码实现过程中出了一些意外的问题,本身使用的是vscode来进行代码编写编译,结果一直运行不起来 显示如下图所示错误,进行了 许多修改依然这般,重新尝试了usesophus也无法运行,试着直接用cmake进行编译均运行成功 ,说明是vscode中的相关设置不成功,但仍未找到原因…
*** 轨迹的误差 (2 分,约 1 小时)**
本题为附加题。
除了画出真实轨迹以外,我们经常需要把 SLAM 估计的轨迹与真实轨迹相比较。下面说明比较的原
理,请你完成比较部分的代码实现。
2. 实际当中的轨迹比较还要更复杂一些。通常 ground-truth 由其他传感器记录(如 vicon),它的采
样频率通常高于相机的频率,所以在处理之前还需要按照时间戳对齐。另外,由于传感器坐标系不
一致,还需要计算两个坐标系之间的差异。这件事也可以用 ICP 解得,我们将在后面的课程中讲
到。
3. 你可以用上题的画图程序将两条轨迹画在同一个图里,看看它们相差多少。
运行结果如下:
代码如下:
#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;
}
CMakelist如下:
cmake_minimum_required(VERSION 3.1)
project(trajectory_error)
find_package(Pangolin REQUIRED)
include_directories(${Pangolin_INCLUDE_DIRS})
find_package(Sophus REQUIRED)
include_directories(${Sophus_INCLUDE_DIR})
add_executable(trajectory_error trajectory_error.cpp)
target_link_libraries(trajectory_error Sophus::Sophus )
target_link_libraries(trajectory_error ${Pangolin_LIBRARIES} ${Sophus_LIBRARIES})