SLAM练习题(一)—— 格式化图像名字、Eigen基本操作

从零手写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公众号

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛定猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值