将TUM数据集按时间命名的图片修改为000~999

文件结构

方法一:fstream文件流法

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 #include <opencv2/opencv.hpp>
 5 
 6 using namespace std;
 7 using namespace cv;
 8 
 9 void load_images(const string &strfile, vector<string> &images_name);
10 string get_num(int num);
11 
12 int main(int argc, char **argv)
13 {
14 if(argc !=2)
15 {
16 cerr << endl << "path_to_rgb_error"<< endl;
17 return 1;
18 }
19 vector<string> images_names;
20 string strfiles = string(argv[1])+"/rgb.txt";
21 
22 cout << strfiles<<endl;
23 load_images(strfiles,images_names);
24 
25 int images_num = images_names.size();
26 cout << images_num <<endl;
27 Mat im;
28 for(int i=0; i<images_num;i++)
29 {
30     im=imread(string(argv[1])+"/"+images_names[i],1);
31     if(im.empty())
32     { 
33     cerr << endl << "Failed to load image at: "
34                  << string(argv[1]) << "/" << images_names[i] << endl;
35         return 1;
36     }
37     string num_str=get_num(i);
38     imwrite(string(argv[1])+"/nrgb/"+num_str+".png",im);
39 }
40     cout << "sucessful" <<endl;
41 return 0;
42 }
43 
44 void load_images(const string &strfile,vector<string> &images_name)
45 {
46 ifstream rgb_txt;
47 rgb_txt.open(strfile.c_str());
48 string s0;
49 getline(rgb_txt,s0);
50 getline(rgb_txt,s0);
51 getline(rgb_txt,s0);
52 
53 while(!rgb_txt.eof())//判断是否到达末尾
54     {
55       string str;
56       getline(rgb_txt,str);
57         if(!str.empty())
58         {
59         stringstream ss;
60         ss << str;
61         string kong,im_name;
62         ss >> kong;//时间戳如果需要可以将其加入函数参数中
63         ss >> im_name;
64         images_name.push_back(im_name);
65 
66         }
67     }
68 
69 
70 }
71 string get_num(int num)
72 {
73     string n_str, str_int="0";
74     if(num<10)
75     n_str = str_int+str_int+to_string(num);
76     else if(num>=10&&num<100)
77     n_str = str_int+to_string(num);
78     else
79     n_str = to_string(num);
80     
81     return n_str;
82 }

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(exchange_name)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../bin)
set(CMAKE_CXX_FLAGS "-std=c++11 -O2 ${SSE_FLAGS} -g -march=native")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(exchange_images_names src/exchange_name.cpp)
target_link_libraries(exchange_images_names ${OpenCV_LIBS})

  这样就在nrgb下面生成了000~613的png图片

 
 
方法二:opencv的glob()函数
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <opencv2/opencv.hpp>
 4 
 5 using namespace std;
 6 using namespace cv;
 7 
 8 
 9 int main(int argc, char **argv)
10 {
11     if(argc !=2)
12     {
13         cerr << endl << "path_to_rgb_error"<< endl;
14         return 1;
15     }
16     char new_name[10];
17     String input_path=string(argv[1])+"/rgb/";
18     vector<String> images_names;
19     glob(input_path,images_names,false);
20     int images_num = images_names.size();
21     cout << "images_num :"<< images_num <<endl;
22     Mat im;
23     for(int i=0; i<images_num;i++)
24     {
25 
26         if(images_names[i].find(".png")!=String::npos)//判断是否到达最后一个文件
27         {
28             im=imread(images_names[i],1);
29             if(im.empty())
30             {
31                 cerr << endl << "Failed to load image at: "
32                      << input_path << images_names[i] << endl;
33                 return 1;
34             }
35             sprintf(new_name,"%03d.png",i);
36             imwrite(string(argv[1])+"/nrgb/"+new_name,im);
37         }
38 
39     }
40     cout << "sucessful" <<endl;
41     return 0;
42 }

1、函数解释

string::find()函数:是一个字符或字符串查找函数,该函数有唯一的返回类型,即string::size_type,即一个无符号整形类型,可能是整数也可能是长整数。如果查找成功,返回按照查找规则找到的第一个字符或者子串的位置;

如果查找失败,返回string::npos,即-1(当然打印出的结果不是-1,而是一个很大的数值,那是因为它是无符号的)。

string::npos静态成员常量:是对类型为size_t的元素具有最大可能的值。当这个值在字符串成员函数中的长度或者子长度被使用时,该值表示“直到字符串结尾”。作为返回值他通常被用作表明没有匹配。
string::npos是这样定义的:static const size_type npos = -1;
2、sprintf函数解释
//把整数123 打印成一个字符串保存在s 中。
sprintf(s, "%d", 123); //产生"123"
可以指定宽度,不足的左边补空格:
sprintf(s, "%8d%8d", 123, 4567); //产生:" 123 4567"
当然也可以左对齐:
sprintf(s, "%-8d%8d", 123, 4567); //产生:"123 4567"
也可以按照16 进制打印:
sprintf(s, "%8x", 4567); //小写16 进制,宽度占8 个位置,右对齐
sprintf(s, "%-8X", 4568); //大写16 进制,宽度占8 个位置,左对齐 这样,一个整数的16 进制字符串就很容易得到,但我们在打印16 进制内容时,通常想要一
种左边补0 的等宽格式,那该怎么做呢?很简单,在表示宽度的数字前面加个0 就可以了。
sprintf(s, "%08X", 4567); //产生:"000011D7"
上面以”%d”进行的10 进制打印同样也可以使用这种左边补0 的方式。
原文地址:https://www.cnblogs.com/fuzhuoxin/p/12853183.html