1.函数背景
当我们进入单目的主函数main(在mono_tum.cc)中时。
#include<iostream> #include<algorithm> #include<fstream> #include<chrono> #include<opencv2/core/core.hpp> #include<System.h> using namespace std; void LoadImages(const string &strFile, vector<string> &vstrImageFilenames, vector<double> &vTimestamps); int main(int argc, char **argv) { if(argc != 4) { cerr << endl << "Usage: ./mono_tum path_to_vocabulary path_to_settings path_to_sequence" << endl; return 1; } // Retrieve paths to images vector<string> vstrImageFilenames; vector<double> vTimestamps; string strFile = string(argv[3])+"/rgb.txt"; LoadImages(strFile, vstrImageFilenames, vTimestamps); int nImages = vstrImageFilenames.size(); // Create SLAM system. It initializes all system threads and gets ready to process frames. ORB_SLAM2::System SLAM(argv[1],argv[2],ORB_SLAM2::System::MONOCULAR,true); // Vector for tracking time statistics vector<float> vTimesTrack; vTimesTrack.resize(nImages); cout << endl << "-------" << endl; cout << "Start processing sequence ..." << endl; cout << "Images in the sequence: " << nImages << endl << endl; // Main loop cv::Mat im; for(int ni=0; ni<nImages; ni++) { // Read image from file im = cv::imread(string(argv[3])+"/"+vstrImageFilenames[ni],CV_LOAD_IMAGE_UNCHANGED); double tframe = vTimestamps[ni]; if(im.empty()) { cerr << endl << "Failed to load image at: " << string(argv[3]) << "/" << vstrImageFilenames[ni] << endl; return 1; } #ifdef COMPILEDWITHC11 std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now(); #else std::chrono::monotonic_clock::time_point t1 = std::chrono::monotonic_clock::now(); #endif // Pass the image to the SLAM system SLAM.TrackMonocular(im,tframe); #ifdef COMPILEDWITHC11 std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now(); #else std::chrono::monotonic_clock::time_point t2 = std::chrono::monotonic_clock::now(); #endif double ttrack= std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count(); vTimesTrack[ni]=ttrack; // Wait to load the next frame double T=0; if(ni<nImages-1) T = vTimestamps[ni+1]-tframe; else if(ni>0) T = tframe-vTimestamps[ni-1]; if(ttrack<T) usleep((T-ttrack)*1e6); } // Stop all threads SLAM.Shutdown(); // Tracking time statistics sort(vTimesTrack.begin(),vTimesTrack.end()); float totaltime = 0; for(int ni=0; ni<nImages; ni++) { totaltime+=vTimesTrack[ni]; } cout << "-------" << endl << endl; cout << "median tracking time: " << vTimesTrack[nImages/2] << endl; cout << "mean tracking time: " << totaltime/nImages << endl; // Save camera trajectory SLAM.SaveKeyFrameTrajectoryTUM("KeyFrameTrajectory.txt"); return 0; }
我们先判断是否传入指定参数个数(mono_tum path_to_vocabulary path_to_settings path_to_sequence)即主函数文件位置、词典位置、配置文件位置、数据集文件,随后我们建立了两个vector容器,分别记录文件名和时间戳(因为传进来的是一个很大的数据集,我们需要将每个图像的名称和时间戳单独放到两个容器中方便后文使用)。
vector<string> vstrImageFilenames; vector<double> vTimestamps;
之后我们将传进来的rgb文件加上后缀。
string strFile = string(argv[3])+"/rgb.txt";
随后就进入了我们的函数LoadImages。
LoadImages(strFile, vstrImageFilenames, vTimestamps);
我们观察我们传进来的第四个参数的文件,文件中的内容分别是时间戳和图片文件。
2.函数功能
导入图片的功能,将参数四的数据集文件(strfile)分离存储在图片文件的容器和存放时间戳容器中。
/** * @brief 导入图片 * * @param[in] strFile 读入的文件名称 * @param[in&out] vstrImageFilenames 彩色图片名称 * @param[in&out] vTimestamps 记录时间戳 */
3.函数代码
void LoadImages(const string &strFile, vector<string> &vstrImageFilenames, vector<double> &vTimestamps) { ifstream f; f.open(strFile.c_str()); // skip first three lines // 前三行是注释,跳过 string s0; getline(f,s0); getline(f,s0); getline(f,s0); while(!f.eof()) { string s; getline(f,s); if(!s.empty()) { stringstream ss; ss << s; double t; string sRGB; ss >> t; vTimestamps.push_back(t); ss >> sRGB; vstrImageFilenames.push_back(sRGB); } } }
首先打开文件,由于文件的前三行是注释等无效信息于是跳过。
然后一直不断读取不同行的信息,直至读到终止标识符eof。用临时变量s存放读到的一行文本,如果读取成功,再用ss去接收s,新建double和string类型变量分别存储时间戳和图像文件,再将他们分别放到vTimestamps和vstrImageFilenames容器中实现分离存储。