1、时间命名的影像改为序列号命名的影像
最近在跑一个SLAM相关算法的时候遇到一个问题,被广泛测试的TUM数据集如何将以采集时间命名的图片保存为以序号命名的图片。
TUM原始数据:
assciations.txt、rgb文件、depth文件是原始数据
处理之后的TUM数据:
assciations1.txt、rgb1文件、depth1文件是处理之后的数据
#include<iostream>
#include<fstream>
#include <string>
#include<string>
#include <vector>
using namespace std;
void SplitString(const string& s, vector<string>& v, const string& c)
{
string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
}
void Rgb_Save(int Num, string Rgbdir)
{
//保存输入图像文件名和输出图像文件名
const char* InImgName;
const char* OutImgName;
//图像数据长度
int length;
//文件指针
FILE* fp;
InImgName = Rgbdir.data();
//以二进制方式打开图像
if ((fp = fopen(InImgName, "rb")) == NULL)
{
cout << "Open image failed!" << endl;
exit(0);
}
//获取图像数据总长度
fseek(fp, 0, SEEK_END);
length = ftell(fp);
rewind(fp);
//根据图像数据长度分配内存buffer
char* ImgBuffer = (char*)malloc(length * sizeof(char));
//将图像数据读入buffer
fread(ImgBuffer, length, 1, fp);
fclose(fp);
string aa = "G:/Code_Repository_lichenwgei/rgbd_dataset_freiburg1_xyz/rgb1/" + to_string(Num) + ".png";
OutImgName = aa.data();
//以二进制写入方式
if ((fp = fopen(OutImgName, "wb")) == NULL)
{
cout << "Open File failed!" << endl;
exit(0);
}
//从buffer中写数据到fp指向的文件中
fwrite(ImgBuffer, length, 1, fp);
cout << "Done! Rgb_Save"<<Num << endl;
//关闭文件指针,释放buffer内存
fclose(fp);
free(ImgBuffer);
}
void Depth_Save(int Num, string Depthdir)
{
//保存输入图像文件名和输出图像文件名
const char* InImgName;
const char* OutImgName;
//图像数据长度
int length;
//文件指针
FILE* fp;
InImgName = Depthdir.data();
//以二进制方式打开图像
if ((fp = fopen(InImgName, "rb")) == NULL)
{
cout << "Open image failed!" << endl;
exit(0);
}
//获取图像数据总长度
fseek(fp, 0, SEEK_END);
length = ftell(fp);
rewind(fp);
//根据图像数据长度分配内存buffer
char* ImgBuffer = (char*)malloc(length * sizeof(char));
//将图像数据读入buffer
fread(ImgBuffer, length, 1, fp);
fclose(fp);
string aa = "G:/Code_Repository_lichenwgei/rgbd_dataset_freiburg1_xyz/depth1/" + to_string(Num) + ".png";
OutImgName = aa.data();
//以二进制写入方式
if ((fp = fopen(OutImgName, "wb")) == NULL)
{
cout << "Open File failed!" << endl;
exit(0);
}
//从buffer中写数据到fp指向的文件中
fwrite(ImgBuffer, length, 1, fp);
cout << "Done! Depth_Save"<<Num << endl;
//关闭文件指针,释放buffer内存
fclose(fp);
free(ImgBuffer);
}
int main()
{
ifstream myfile("G:/Code_Repository_lichenwgei/rgbd_dataset_freiburg1_xyz/associations.txt");
string line;
string Rgbdir;
string Depthdir;
vector<string> v;
int Num=100000;
while (getline(myfile, line))
{
SplitString(line, v, " ");
//cout << v[0] << endl;
Rgbdir = "G:/Code_Repository_lichenwgei/rgbd_dataset_freiburg1_xyz/" + v[1];
Depthdir = "G:/Code_Repository_lichenwgei/rgbd_dataset_freiburg1_xyz/" + v[3];
Rgb_Save(Num,Rgbdir);
Depth_Save(Num, Depthdir);
v.clear();//清除所有元素
vector<string>(v).swap(v);//清除vector内存
Num++;
};
ofstream outfile("G:/Code_Repository_lichenwgei/rgbd_dataset_freiburg1_xyz/result.txt");
return 0;
}
2、以序列号开始的位姿结果改为时间开始的位姿结果
原始:
时间信息文件:
修改代码:
using namespace std;
#include<iostream>
#include<fstream>
#include <string>
#include<string>
#include <vector>
void SplitString(const string& s, vector<string>& v, const string& c)
{
string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
}
int main()
{
ifstream Trajectory("D:/SLAM_Data/TUM/freiburg1_desk/a_Trajectory.txt");
ifstream Match_time("D:/SLAM_Data/TUM/freiburg1_desk/match_time.txt");
ofstream outputfile;
outputfile.open("D:/SLAM_Data/TUM/freiburg1_desk/a_Trajectory_time.txt");
string line_Trajectory,line_Time;
vector<string> v_Trajectory,v_Time;
while (getline(Trajectory, line_Trajectory))
{
SplitString(line_Trajectory, v_Trajectory, " ");
getline(Match_time, line_Time);
SplitString(line_Time, v_Time, " ");
string Result = v_Time[0] + " " + v_Trajectory[1] + " " + v_Trajectory[2] + " " + v_Trajectory[3] + " " + v_Trajectory[4]
+ " " + v_Trajectory[5] + " " + v_Trajectory[6] + " " + v_Trajectory[7];
std::cout << Result << std::endl;
outputfile << Result << endl;
v_Trajectory.clear();
v_Time.clear();
}
outputfile.close();
return 1;
}
结果: