TUM数据集的图片名称都是以时间戳为名的。我想把它都修改为0000,0001,0002,这样类似顺序的名称。因此写了下面这个小程序,这里提供给大家,如果对您有帮助的话可以随手给我点个赞,谢谢!!!
change.cpp
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
#include <string>
#include<sstream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
using namespace std;
using namespace cv;
int parseInfoFile(
std::string &strAssociation_Path,
vector<std::pair<string,string>> &vec_info)//vec_info保存rgb和depth的路径信息
{
char * line = NULL;
size_t len = 0;
ssize_t read;
FILE *pFile = fopen(strAssociation_Path.c_str(), "r");
if(!pFile) {
return -1;
}
int iFrameCnt = 0;
while ((read = getline(&line, &len, pFile)) != -1) {
std::istringstream is(line);//istringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来。
std::string part;//保存以空格分开的每一部分
int iIdxToken = 0;
while (getline(is, part))
{
if('#' == part[0])/// Skip file comment '#"
{
continue;
}
int64_t timeSeq = 0;
std::string strDepthPath;
std::string strRgbPath;
std::istringstream iss(part);
std::string token;
while (getline(iss, token, ' '))
{
if(2 == iIdxToken) //Time rgb
{//first token which is time
//什么都不干
}
else if(1 == iIdxToken)//rgb path,fr1,fr2和fr3的第2列和第四列相反,可能需要修改这里iIdxToken为3
{
strRgbPath = token;
// std::cout<<strRgbPath<<endl;
}
else if(0 == iIdxToken)//Time depth
{
//什么都不干
}
else if(3 == iIdxToken)//depth path fr1,fr2和fr3的第2列和第四列相反,可能需要修改这里iIdxToken为1
{
strDepthPath = token;
// std::cout<<strDepthPath<<endl;
}
iIdxToken++;
}
vec_info.push_back(make_pair(strRgbPath,strDepthPath));
}
}
fclose(pFile);
return 0;
}
int main(int argc, char **argv)
{
if(argc !=2)
{
cerr << endl << "path_to_rgb_error"<< endl;
return 1;
}
char new_name[20];
string basedir=string(argv[1])+"/";
string input_associatiations=basedir+"/associations.txt";
vector<std::pair<string,string>> picInfo;
if(parseInfoFile(input_associatiations,picInfo))
cerr<<" Can‘t read file! "<<endl;
cout<<"There is "<<picInfo.size()<<" picture pair in all!!"<<endl;
//不存在就创建新目录保存
string exportRgbDir =basedir+"/nrgb/";
boost::filesystem::path eDir(exportRgbDir);
boost::filesystem::create_directory(eDir);
string exportDepthDir =basedir+"/ndepth/";
boost::filesystem::path dDir(exportDepthDir);
boost::filesystem::create_directory(dDir);
std::cout<<"Progress"<<endl;
for(unsigned int i = 0; i < picInfo.size(); ++i)//开始处理每个图片
{
Mat rgb,depth;
string rgbpath=picInfo[i].first;
rgbpath=basedir+rgbpath;
string depthpath=picInfo[i].second;
depthpath=basedir+depthpath;
rgb=imread(rgbpath,1);
depth=imread(depthpath,-1);
// cout<<"type "<<depth.type()<<endl;
if(rgb.empty()||depth.empty())
{
cerr << endl << "Failed to load image at: "
<< rgbpath << " or "<< depthpath<< endl;
return 1;
}
//打印进度
std::cout << '\r'
<< std::setw(4) << std::setfill('0') << i << " / "
<< std::setw(4) << std::setfill('0') << picInfo.size()
<< std::flush;
sprintf(new_name,"Color%04d.png",i);
imwrite(exportRgbDir+new_name,rgb);
sprintf(new_name,"Depth%04d.png",i);
imwrite(exportDepthDir+new_name,depth);
}
cout << "Sucessful!!!" <<endl;
CMakeLists.txt
set( CMAKE_CXX_FLAGS "-std=c++11" )
find_package( OpenCV REQUIRED )# 寻找 OpenCV 库
find_package(Boost REQUIRED)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
# 添加头文件
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( rgb change.cpp )
# 链接 OpenCV 库
target_link_libraries( rgb ${OpenCV_LIBS} boost_filesystem boost_system)
使用:
mkdir build
cd build
cmake -DBOOST_ROOT="/home/wei/Documents/project/maskfusion-master/deps/boost" ..
make
#注意,fr3的associations.txt和fr2,fr1的associations.txt的第二列和第四列是相反的,程序是针对fr3的,所以如果运行fr1和fr2需要修改程序,另外,程序读取和创建目录使用boost库完成,因此cmake时要指定boost库
运行:
./rgb /home/wei/Documents/dataset/TUM/rgbd_dataset_freiburg3_walking_halfsphere