文件结构
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图片
原文:https://www.cnblogs.com/fuzhuoxin/p/12853183.html