从零手写SLAM笔记
格式化图像名字
题目:SLAM是处理序列图像的,有时候需要格式化的图像名字用作输入。前面提到的TUM的RGB-D数据集中图像是根据时间命名的,请从下面链接下载数据集fr1/desk Computer Vision Group - Dataset Download 并解压。请编程实现将文件夹/rgb下以时间命名的序列图片重新命名为0000-9999的格式。
知识点: 熟悉cmake的使用、OpenCV读写操作、C++的string操作
友情链接:https://blog.csdn.net/weixin_40023317/article/details/83752151
参考答案:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main (int argc, char** argv)
{
if (argc!=3)
{
cout<<"usage: convertIndex filepath newfilepath"<<endl;
return 1;
}
Mat img;
String filepath = argv[1], new_filepath = argv[2];
vector<String> filenames;
glob(filepath,filenames, false);//opencv中很好用的一个读取文件夹下文件的函数 false表示不递归读取
cout<<"filenames size is :"<<filenames.size()<<endl;
for (int i =0; i < filenames.size(); i++)
{
char new_name[10];
img = imread(filenames[i], CV_LOAD_IMAGE_UNCHANGED);
sprintf(new_name, "%04d.png", i);//按格式输出
// method2
//stringstream ss;
//ss<<setW(4)<<setfill('0')<<i<<".png";
//string new_name = ss.str();
imwrite(new_filepath + new_name, img);
cout<<new_filepath + new_name<<endl;
}
return 0;
}
并没有考虑文件夹不存在时的处理情况,有时间了再改进~
eigen基本操作
题目:已知相机的位姿用四元数表示为q=[0.35,0.2,0.3,0.1],顺序为x,y,z,w,请编程实现:输出四元数对应的旋转矩
阵、旋转矩阵的转置,旋转矩阵的逆矩阵,旋转矩阵乘以自身的转置,验证旋转矩阵的正交性。
知识点:熟悉cmake的使用、学习eigen的基本操作;根据实践验证旋转矩阵的约束
参考答案:
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
Eigen::Quaterniond quat = Eigen::Quaterniond(0.1, 0.35, 0.2, 0.3);//注意赋值顺序和存储顺序不同
quat.normalize(); //normalized()对原变量副本进行操作,normalize()直接对原变量进行操作
Eigen::Matrix3d rotation_matrix = quat.matrix();
cout<<"旋转矩阵:r=/n"<<rotation_matrix<<endl;
cout<<"旋转矩阵转置后:rt = \n"<<rotation_matrix.transpose() <<endl;
cout<<"旋转矩阵的逆矩阵:r.inv = \n"<<rotation_matrix.inverse() <<endl;
cout<<"旋转矩阵乘以自身的转置:r*rt =\n"<<rotation_matrix * rotation_matrix.transpose()<<endl;
return 0;
}
旋转矩阵乘以自身的转置理应为单位矩阵,但是因为计算误差的问题,结果非对角线位置不为0,但是已经很接近0了。
附:CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
PROJECT(002_practice)
set(CMAKE_BUILD_TYPE "Debug")
#set(CMAKE_CXX_FLAGS "-std=c++11")
# 添加eigen3
include_directories("/usr/include/eigen3/")
# 添加OpenCV
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# convertIndex
add_executable(convertIndex convertIndex.cpp)
target_link_libraries(convertIndex ${OpenCV_LIBS})
#eigenRotate
add_executable(eigenRotate eigenRotate.cpp)
参考:
https://blog.csdn.net/weixin_40023317/article/details/83752151
题目来自 计算机视觉life公众号