mono_inertial_euroc.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 LoadImages(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. if(argc < 5)
  35. {
  36. cerr << endl << "Usage: ./mono_inertial_euroc path_to_vocabulary path_to_settings path_to_sequence_folder_1 path_to_times_file_1 (path_to_image_folder_2 path_to_times_file_2 ... path_to_image_folder_N path_to_times_file_N) " << endl;
  37. return 1;
  38. }
  39. const int num_seq = (argc-3)/2;
  40. cout << "num_seq = " << num_seq << endl;
  41. bool bFileName= (((argc-3) % 2) == 1);
  42. string file_name;
  43. if (bFileName)
  44. {
  45. file_name = string(argv[argc-1]);
  46. cout << "file name: " << file_name << endl;
  47. }
  48. // Load all sequences:
  49. int seq;
  50. vector< vector<string> > vstrImageFilenames;
  51. vector< vector<double> > vTimestampsCam;
  52. vector< vector<cv::Point3f> > vAcc, vGyro;
  53. vector< vector<double> > vTimestampsImu;
  54. vector<int> nImages;
  55. vector<int> nImu;
  56. vector<int> first_imu(num_seq,0);
  57. vstrImageFilenames.resize(num_seq);
  58. vTimestampsCam.resize(num_seq);
  59. vAcc.resize(num_seq);
  60. vGyro.resize(num_seq);
  61. vTimestampsImu.resize(num_seq);
  62. nImages.resize(num_seq);
  63. nImu.resize(num_seq);
  64. int tot_images = 0;
  65. for (seq = 0; seq<num_seq; seq++)
  66. {
  67. cout << "Loading images for sequence " << seq << "...";
  68. string pathSeq(argv[(2*seq) + 3]);
  69. string pathTimeStamps(argv[(2*seq) + 4]);
  70. string pathCam0 = pathSeq + "/mav0/cam0/data";
  71. string pathImu = pathSeq + "/mav0/imu0/data.csv";
  72. LoadImages(pathCam0, pathTimeStamps, vstrImageFilenames[seq], vTimestampsCam[seq]);
  73. cout << "LOADED!" << endl;
  74. cout << "Loading IMU for sequence " << seq << "...";
  75. LoadIMU(pathImu, vTimestampsImu[seq], vAcc[seq], vGyro[seq]);
  76. cout << "LOADED!" << endl;
  77. nImages[seq] = vstrImageFilenames[seq].size();
  78. tot_images += nImages[seq];
  79. nImu[seq] = vTimestampsImu[seq].size();
  80. if((nImages[seq]<=0)||(nImu[seq]<=0))
  81. {
  82. cerr << "ERROR: Failed to load images or IMU for sequence" << seq << endl;
  83. return 1;
  84. }
  85. // Find first imu to be considered, supposing imu measurements start first
  86. while(vTimestampsImu[seq][first_imu[seq]]<=vTimestampsCam[seq][0])
  87. first_imu[seq]++;
  88. first_imu[seq]--; // first imu measurement to be considered
  89. }
  90. // Vector for tracking time statistics
  91. vector<float> vTimesTrack;
  92. vTimesTrack.resize(tot_images);
  93. cout.precision(17);
  94. // Create SLAM system. It initializes all system threads and gets ready to process frames.
  95. ORB_SLAM3::System SLAM(argv[1],argv[2],ORB_SLAM3::System::IMU_MONOCULAR, true);
  96. float imageScale = SLAM.GetImageScale();
  97. double t_resize = 0.f;
  98. double t_track = 0.f;
  99. int proccIm=0;
  100. for (seq = 0; seq<num_seq; seq++)
  101. {
  102. // Main loop
  103. cv::Mat im;
  104. vector<ORB_SLAM3::IMU::Point> vImuMeas;
  105. proccIm = 0;
  106. for(int ni=0; ni<nImages[seq]; ni++, proccIm++)
  107. {
  108. // Read image from file
  109. im = cv::imread(vstrImageFilenames[seq][ni],cv::IMREAD_UNCHANGED); //CV_LOAD_IMAGE_UNCHANGED);
  110. double tframe = vTimestampsCam[seq][ni];
  111. if(im.empty())
  112. {
  113. cerr << endl << "Failed to load image at: "
  114. << vstrImageFilenames[seq][ni] << endl;
  115. return 1;
  116. }
  117. if(imageScale != 1.f)
  118. {
  119. #ifdef REGISTER_TIMES
  120. #ifdef COMPILEDWITHC14
  121. std::chrono::steady_clock::time_point t_Start_Resize = std::chrono::steady_clock::now();
  122. #else
  123. std::chrono::monotonic_clock::time_point t_Start_Resize = std::chrono::monotonic_clock::now();
  124. #endif
  125. #endif
  126. int width = im.cols * imageScale;
  127. int height = im.rows * imageScale;
  128. cv::resize(im, im, cv::Size(width, height));
  129. #ifdef REGISTER_TIMES
  130. #ifdef COMPILEDWITHC14
  131. std::chrono::steady_clock::time_point t_End_Resize = std::chrono::steady_clock::now();
  132. #else
  133. std::chrono::monotonic_clock::time_point t_End_Resize = std::chrono::monotonic_clock::now();
  134. #endif
  135. t_resize = std::chrono::duration_cast<std::chrono::duration<double,std::milli> >(t_End_Resize - t_Start_Resize).count();
  136. SLAM.InsertResizeTime(t_resize);
  137. #endif
  138. }
  139. // Load imu measurements from previous frame
  140. vImuMeas.clear();
  141. if(ni>0)
  142. {
  143. // cout << "t_cam " << tframe << endl;
  144. while(vTimestampsImu[seq][first_imu[seq]]<=vTimestampsCam[seq][ni])
  145. {
  146. 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,
  147. vGyro[seq][first_imu[seq]].x,vGyro[seq][first_imu[seq]].y,vGyro[seq][first_imu[seq]].z,
  148. vTimestampsImu[seq][first_imu[seq]]));
  149. first_imu[seq]++;
  150. }
  151. }
  152. #ifdef COMPILEDWITHC14
  153. std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
  154. #else
  155. std::chrono::monotonic_clock::time_point t1 = std::chrono::monotonic_clock::now();
  156. #endif
  157. // Pass the image to the SLAM system
  158. // cout << "tframe = " << tframe << endl;
  159. SLAM.TrackMonocular(im,tframe,vImuMeas); // TODO change to monocular_inertial
  160. #ifdef COMPILEDWITHC14
  161. std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
  162. #else
  163. std::chrono::monotonic_clock::time_point t2 = std::chrono::monotonic_clock::now();
  164. #endif
  165. #ifdef REGISTER_TIMES
  166. t_track = t_resize + std::chrono::duration_cast<std::chrono::duration<double,std::milli> >(t2 - t1).count();
  167. SLAM.InsertTrackTime(t_track);
  168. #endif
  169. double ttrack= std::chrono::duration_cast<std::chrono::duration<double> >(t2 - t1).count();
  170. ttrack_tot += ttrack;
  171. // std::cout << "ttrack: " << ttrack << std::endl;
  172. vTimesTrack[ni]=ttrack;
  173. // Wait to load the next frame
  174. double T=0;
  175. if(ni<nImages[seq]-1)
  176. T = vTimestampsCam[seq][ni+1]-tframe;
  177. else if(ni>0)
  178. T = tframe-vTimestampsCam[seq][ni-1];
  179. if(ttrack<T)
  180. usleep((T-ttrack)*1e6); // 1e6
  181. }
  182. if(seq < num_seq - 1)
  183. {
  184. cout << "Changing the dataset" << endl;
  185. SLAM.ChangeDataset();
  186. }
  187. }
  188. // Stop all threads
  189. SLAM.Shutdown();
  190. // Save camera trajectory
  191. if (bFileName)
  192. {
  193. const string kf_file = "kf_" + string(argv[argc-1]) + ".txt";
  194. const string f_file = "f_" + string(argv[argc-1]) + ".txt";
  195. SLAM.SaveTrajectoryEuRoC(f_file);
  196. SLAM.SaveKeyFrameTrajectoryEuRoC(kf_file);
  197. }
  198. else
  199. {
  200. SLAM.SaveTrajectoryEuRoC("CameraTrajectory.txt");
  201. SLAM.SaveKeyFrameTrajectoryEuRoC("KeyFrameTrajectory.txt");
  202. }
  203. return 0;
  204. }
  205. void LoadImages(const string &strImagePath, const string &strPathTimes,
  206. vector<string> &vstrImages, vector<double> &vTimeStamps)
  207. {
  208. ifstream fTimes;
  209. fTimes.open(strPathTimes.c_str());
  210. vTimeStamps.reserve(5000);
  211. vstrImages.reserve(5000);
  212. while(!fTimes.eof())
  213. {
  214. string s;
  215. getline(fTimes,s);
  216. if(!s.empty())
  217. {
  218. stringstream ss;
  219. ss << s;
  220. vstrImages.push_back(strImagePath + "/" + ss.str() + ".png");
  221. double t;
  222. ss >> t;
  223. vTimeStamps.push_back(t/1e9);
  224. }
  225. }
  226. }
  227. void LoadIMU(const string &strImuPath, vector<double> &vTimeStamps, vector<cv::Point3f> &vAcc, vector<cv::Point3f> &vGyro)
  228. {
  229. ifstream fImu;
  230. fImu.open(strImuPath.c_str());
  231. vTimeStamps.reserve(5000);
  232. vAcc.reserve(5000);
  233. vGyro.reserve(5000);
  234. while(!fImu.eof())
  235. {
  236. string s;
  237. getline(fImu,s);
  238. if (s[0] == '#')
  239. continue;
  240. if(!s.empty())
  241. {
  242. string item;
  243. size_t pos = 0;
  244. double data[7];
  245. int count = 0;
  246. while ((pos = s.find(',')) != string::npos) {
  247. item = s.substr(0, pos);
  248. data[count++] = stod(item);
  249. s.erase(0, pos + 1);
  250. }
  251. item = s.substr(0, pos);
  252. data[6] = stod(item);
  253. vTimeStamps.push_back(data[0]/1e9);
  254. vAcc.push_back(cv::Point3f(data[4],data[5],data[6]));
  255. vGyro.push_back(cv::Point3f(data[1],data[2],data[3]));
  256. }
  257. }
  258. }