mono_inertial_tum_vi.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**
  2. * This file is part of ORB-SLAM3
  3. *
  4. * Copyright (C) 2017-2021 Carlos Campos, Richard Elvira, Juan J. Gómez Rodríguez, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  5. * Copyright (C) 2014-2016 Raúl Mur-Artal, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  6. *
  7. * ORB-SLAM3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * ORB-SLAM3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  12. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with ORB-SLAM3.
  16. * If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include<iostream>
  19. #include<algorithm>
  20. #include<fstream>
  21. #include<chrono>
  22. #include <ctime>
  23. #include <sstream>
  24. #include<opencv2/core/core.hpp>
  25. #include<System.h>
  26. #include "ImuTypes.h"
  27. using namespace std;
  28. void LoadImagesTUMVI(const string &strImagePath, const string &strPathTimes,
  29. vector<string> &vstrImages, vector<double> &vTimeStamps);
  30. void LoadIMU(const string &strImuPath, vector<double> &vTimeStamps, vector<cv::Point3f> &vAcc, vector<cv::Point3f> &vGyro);
  31. double ttrack_tot = 0;
  32. int main(int argc, char **argv)
  33. {
  34. const int num_seq = (argc-3)/3;
  35. cout << "num_seq = " << num_seq << endl;
  36. bool bFileName= ((argc % 3) == 1);
  37. string file_name;
  38. if (bFileName)
  39. file_name = string(argv[argc-1]);
  40. cout << "file name: " << file_name << endl;
  41. if(argc < 6)
  42. {
  43. cerr << endl << "Usage: ./mono_inertial_tum_vi path_to_vocabulary path_to_settings path_to_image_folder_1 path_to_times_file_1 path_to_imu_data_1 (path_to_image_folder_2 path_to_times_file_2 path_to_imu_data_2 ... path_to_image_folder_N path_to_times_file_N path_to_imu_data_N) (trajectory_file_name)" << endl;
  44. return 1;
  45. }
  46. // Load all sequences:
  47. int seq;
  48. vector< vector<string> > vstrImageFilenames;
  49. vector< vector<double> > vTimestampsCam;
  50. vector< vector<cv::Point3f> > vAcc, vGyro;
  51. vector< vector<double> > vTimestampsImu;
  52. vector<int> nImages;
  53. vector<int> nImu;
  54. vector<int> first_imu(num_seq,0);
  55. vstrImageFilenames.resize(num_seq);
  56. vTimestampsCam.resize(num_seq);
  57. vAcc.resize(num_seq);
  58. vGyro.resize(num_seq);
  59. vTimestampsImu.resize(num_seq);
  60. nImages.resize(num_seq);
  61. nImu.resize(num_seq);
  62. int tot_images = 0;
  63. for (seq = 0; seq<num_seq; seq++)
  64. {
  65. cout << "Loading images for sequence " << seq << "...";
  66. LoadImagesTUMVI(string(argv[3*(seq+1)]), string(argv[3*(seq+1)+1]), vstrImageFilenames[seq], vTimestampsCam[seq]);
  67. cout << "LOADED!" << endl;
  68. cout << "Loading IMU for sequence " << seq << "...";
  69. LoadIMU(string(argv[3*(seq+1)+2]), vTimestampsImu[seq], vAcc[seq], vGyro[seq]);
  70. cout << "LOADED!" << endl;
  71. nImages[seq] = vstrImageFilenames[seq].size();
  72. tot_images += nImages[seq];
  73. nImu[seq] = vTimestampsImu[seq].size();
  74. if((nImages[seq]<=0)||(nImu[seq]<=0))
  75. {
  76. cerr << "ERROR: Failed to load images or IMU for sequence" << seq << endl;
  77. return 1;
  78. }
  79. // Find first imu to be considered, supposing imu measurements start first
  80. while(vTimestampsImu[seq][first_imu[seq]]<=vTimestampsCam[seq][0])
  81. first_imu[seq]++;
  82. first_imu[seq]--; // first imu measurement to be considered
  83. }
  84. // Vector for tracking time statistics
  85. vector<float> vTimesTrack;
  86. vTimesTrack.resize(tot_images);
  87. cout << endl << "-------" << endl;
  88. cout.precision(17);
  89. /*cout << "Start processing sequence ..." << endl;
  90. cout << "Images in the sequence: " << nImages << endl;
  91. cout << "IMU data in the sequence: " << nImu << endl << endl;*/
  92. // Create SLAM system. It initializes all system threads and gets ready to process frames.
  93. ORB_SLAM3::System SLAM(argv[1],argv[2],ORB_SLAM3::System::IMU_MONOCULAR, true, 0, file_name);
  94. float imageScale = SLAM.GetImageScale();
  95. double t_resize = 0.f;
  96. double t_track = 0.f;
  97. int proccIm = 0;
  98. for (seq = 0; seq<num_seq; seq++)
  99. {
  100. // Main loop
  101. cv::Mat im;
  102. vector<ORB_SLAM3::IMU::Point> vImuMeas;
  103. proccIm = 0;
  104. cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE(3.0, cv::Size(8, 8));
  105. for(int ni=0; ni<nImages[seq]; ni++, proccIm++)
  106. {
  107. // Read image from file
  108. im = cv::imread(vstrImageFilenames[seq][ni],cv::IMREAD_GRAYSCALE); //,cv::IMREAD_GRAYSCALE);
  109. // clahe
  110. clahe->apply(im,im);
  111. // cout << "mat type: " << im.type() << endl;
  112. double tframe = vTimestampsCam[seq][ni];
  113. if(im.empty())
  114. {
  115. cerr << endl << "Failed to load image at: "
  116. << vstrImageFilenames[seq][ni] << endl;
  117. return 1;
  118. }
  119. // Load imu measurements from previous frame
  120. vImuMeas.clear();
  121. if(ni>0)
  122. {
  123. // cout << "t_cam " << tframe << endl;
  124. while(vTimestampsImu[seq][first_imu[seq]]<=vTimestampsCam[seq][ni])
  125. {
  126. vImuMeas.push_back(ORB_SLAM3::IMU::Point(vAcc[seq][first_imu[seq]].x,vAcc[seq][first_imu[seq]].y,vAcc[seq][first_imu[seq]].z,
  127. vGyro[seq][first_imu[seq]].x,vGyro[seq][first_imu[seq]].y,vGyro[seq][first_imu[seq]].z,
  128. vTimestampsImu[seq][first_imu[seq]]));
  129. // cout << "t_imu = " << fixed << vImuMeas.back().t << endl;
  130. first_imu[seq]++;
  131. }
  132. }
  133. if(imageScale != 1.f)
  134. {
  135. #ifdef REGISTER_TIMES
  136. #ifdef COMPILEDWITHC14
  137. std::chrono::steady_clock::time_point t_Start_Resize = std::chrono::steady_clock::now();
  138. #else
  139. std::chrono::monotonic_clock::time_point t_Start_Resize = std::chrono::monotonic_clock::now();
  140. #endif
  141. #endif
  142. int width = im.cols * imageScale;
  143. int height = im.rows * imageScale;
  144. cv::resize(im, im, cv::Size(width, height));
  145. #ifdef REGISTER_TIMES
  146. #ifdef COMPILEDWITHC14
  147. std::chrono::steady_clock::time_point t_End_Resize = std::chrono::steady_clock::now();
  148. #else
  149. std::chrono::monotonic_clock::time_point t_End_Resize = std::chrono::monotonic_clock::now();
  150. #endif
  151. t_resize = std::chrono::duration_cast<std::chrono::duration<double,std::milli> >(t_End_Resize - t_Start_Resize).count();
  152. SLAM.InsertResizeTime(t_resize);
  153. #endif
  154. }
  155. // cout << "first imu: " << first_imu[seq] << endl;
  156. /*cout << "first imu time: " << fixed << vTimestampsImu[first_imu] << endl;
  157. cout << "size vImu: " << vImuMeas.size() << endl;*/
  158. #ifdef COMPILEDWITHC14
  159. std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
  160. #else
  161. std::chrono::monotonic_clock::time_point t1 = std::chrono::monotonic_clock::now();
  162. #endif
  163. // Pass the image to the SLAM system
  164. // cout << "tframe = " << tframe << endl;
  165. SLAM.TrackMonocular(im,tframe,vImuMeas); // TODO change to monocular_inertial
  166. #ifdef COMPILEDWITHC14
  167. std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
  168. #else
  169. std::chrono::monotonic_clock::time_point t2 = std::chrono::monotonic_clock::now();
  170. #endif
  171. #ifdef REGISTER_TIMES
  172. t_track = t_resize + std::chrono::duration_cast<std::chrono::duration<double,std::milli> >(t2 - t1).count();
  173. SLAM.InsertTrackTime(t_track);
  174. #endif
  175. double ttrack= std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count();
  176. ttrack_tot += ttrack;
  177. // std::cout << "ttrack: " << ttrack << std::endl;
  178. vTimesTrack[ni]=ttrack;
  179. // Wait to load the next frame
  180. double T=0;
  181. if(ni<nImages[seq]-1)
  182. T = vTimestampsCam[seq][ni+1]-tframe;
  183. else if(ni>0)
  184. T = tframe-vTimestampsCam[seq][ni-1];
  185. if(ttrack<T)
  186. usleep((T-ttrack)*1e6); // 1e6
  187. }
  188. if(seq < num_seq - 1)
  189. {
  190. cout << "Changing the dataset" << endl;
  191. SLAM.ChangeDataset();
  192. }
  193. }
  194. // cout << "ttrack_tot = " << ttrack_tot << std::endl;
  195. // Stop all threads
  196. SLAM.Shutdown();
  197. // Tracking time statistics
  198. // Save camera trajectory
  199. if (bFileName)
  200. {
  201. const string kf_file = "kf_" + string(argv[argc-1]) + ".txt";
  202. const string f_file = "f_" + string(argv[argc-1]) + ".txt";
  203. SLAM.SaveTrajectoryEuRoC(f_file);
  204. SLAM.SaveKeyFrameTrajectoryEuRoC(kf_file);
  205. }
  206. else
  207. {
  208. SLAM.SaveTrajectoryEuRoC("CameraTrajectory.txt");
  209. SLAM.SaveKeyFrameTrajectoryEuRoC("KeyFrameTrajectory.txt");
  210. }
  211. sort(vTimesTrack.begin(),vTimesTrack.end());
  212. float totaltime = 0;
  213. for(int ni=0; ni<nImages[0]; ni++)
  214. {
  215. totaltime+=vTimesTrack[ni];
  216. }
  217. cout << "-------" << endl << endl;
  218. cout << "median tracking time: " << vTimesTrack[nImages[0]/2] << endl;
  219. cout << "mean tracking time: " << totaltime/proccIm << endl;
  220. /*const string kf_file = "kf_" + ss.str() + ".txt";
  221. const string f_file = "f_" + ss.str() + ".txt";
  222. SLAM.SaveTrajectoryEuRoC(f_file);
  223. SLAM.SaveKeyFrameTrajectoryEuRoC(kf_file);*/
  224. return 0;
  225. }
  226. void LoadImagesTUMVI(const string &strImagePath, const string &strPathTimes,
  227. vector<string> &vstrImages, vector<double> &vTimeStamps)
  228. {
  229. ifstream fTimes;
  230. cout << strImagePath << endl;
  231. cout << strPathTimes << endl;
  232. fTimes.open(strPathTimes.c_str());
  233. vTimeStamps.reserve(5000);
  234. vstrImages.reserve(5000);
  235. while(!fTimes.eof())
  236. {
  237. string s;
  238. getline(fTimes,s);
  239. if(!s.empty())
  240. {
  241. if (s[0] == '#')
  242. continue;
  243. int pos = s.find(' ');
  244. string item = s.substr(0, pos);
  245. vstrImages.push_back(strImagePath + "/" + item + ".png");
  246. double t = stod(item);
  247. vTimeStamps.push_back(t/1e9);
  248. }
  249. }
  250. }
  251. void LoadIMU(const string &strImuPath, vector<double> &vTimeStamps, vector<cv::Point3f> &vAcc, vector<cv::Point3f> &vGyro)
  252. {
  253. ifstream fImu;
  254. fImu.open(strImuPath.c_str());
  255. vTimeStamps.reserve(5000);
  256. vAcc.reserve(5000);
  257. vGyro.reserve(5000);
  258. while(!fImu.eof())
  259. {
  260. string s;
  261. getline(fImu,s);
  262. if (s[0] == '#')
  263. continue;
  264. if(!s.empty())
  265. {
  266. string item;
  267. size_t pos = 0;
  268. double data[7];
  269. int count = 0;
  270. while ((pos = s.find(',')) != string::npos) {
  271. item = s.substr(0, pos);
  272. data[count++] = stod(item);
  273. s.erase(0, pos + 1);
  274. }
  275. item = s.substr(0, pos);
  276. data[6] = stod(item);
  277. vTimeStamps.push_back(data[0]/1e9);
  278. vAcc.push_back(cv::Point3f(data[4],data[5],data[6]));
  279. vGyro.push_back(cv::Point3f(data[1],data[2],data[3]));
  280. }
  281. }
  282. }