stereo_realsense_t265.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <signal.h>
  19. #include <stdlib.h>
  20. #include <iostream>
  21. #include <algorithm>
  22. #include <fstream>
  23. #include <chrono>
  24. #include <ctime>
  25. #include <sstream>
  26. #include <opencv2/core/core.hpp>
  27. #include <librealsense2/rs.hpp>
  28. #include <System.h>
  29. using namespace std;
  30. bool b_continue_session;
  31. void exit_loop_handler(int s){
  32. cout << "Finishing session" << endl;
  33. b_continue_session = false;
  34. }
  35. int main(int argc, char **argv)
  36. {
  37. if(argc < 3 || argc > 4)
  38. {
  39. cerr << endl << "Usage: ./stereo_realsense_t265 path_to_vocabulary path_to_settings (trajectory_file_name)" << endl;
  40. return 1;
  41. }
  42. string file_name;
  43. bool bFileName = false;
  44. if (argc == 4)
  45. {
  46. file_name = string(argv[argc-1]);
  47. bFileName = true;
  48. }
  49. struct sigaction sigIntHandler;
  50. sigIntHandler.sa_handler = exit_loop_handler;
  51. sigemptyset(&sigIntHandler.sa_mask);
  52. sigIntHandler.sa_flags = 0;
  53. sigaction(SIGINT, &sigIntHandler, NULL);
  54. b_continue_session = true;
  55. // Declare RealSense pipeline, encapsulating the actual device and sensors
  56. rs2::pipeline pipe;
  57. // Create a configuration for configuring the pipeline with a non default profile
  58. rs2::config cfg;
  59. // Enable both image streams
  60. cfg.enable_stream(RS2_STREAM_FISHEYE, 1, RS2_FORMAT_Y8);
  61. cfg.enable_stream(RS2_STREAM_FISHEYE, 2, RS2_FORMAT_Y8);
  62. rs2::pipeline_profile pipe_profile = pipe.start(cfg);
  63. cout << endl << "-------" << endl;
  64. cout.precision(17);
  65. /*cout << "Start processing sequence ..." << endl;
  66. cout << "Images in the sequence: " << nImages << endl;
  67. cout << "IMU data in the sequence: " << nImu << endl << endl;*/
  68. // Create SLAM system. It initializes all system threads and gets ready to process frames.
  69. ORB_SLAM3::System SLAM(argv[1],argv[2],ORB_SLAM3::System::STEREO, true, 0, file_name);
  70. float imageScale = SLAM.GetImageScale();
  71. cv::Mat imLeft, imRight;
  72. vector<ORB_SLAM3::IMU::Point> vImuMeas;
  73. rs2::stream_profile fisheye_stream_left = pipe_profile.get_stream(RS2_STREAM_FISHEYE, 1);
  74. rs2_intrinsics intrinsics_left = fisheye_stream_left.as<rs2::video_stream_profile>().get_intrinsics();
  75. int width_left = intrinsics_left.width;
  76. int height_left = intrinsics_left.height;
  77. rs2::stream_profile fisheye_stream_right = pipe_profile.get_stream(RS2_STREAM_FISHEYE, 2);
  78. rs2_intrinsics intrinsics_right = fisheye_stream_right.as<rs2::video_stream_profile>().get_intrinsics();
  79. int width_right = intrinsics_right.width;
  80. int height_right = intrinsics_right.height;
  81. double t_resize = 0.f;
  82. double t_track = 0.f;
  83. while (b_continue_session)
  84. {
  85. //cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE(3.0, cv::Size(8, 8));
  86. // Get the stream from the device
  87. rs2::frameset frame_set = pipe.wait_for_frames();
  88. double timestamp = frame_set.get_timestamp(); //RS2_FRAME_METADATA_SENSOR_TIMESTAMP
  89. if(rs2::video_frame image_frame = frame_set.first_or_default(RS2_STREAM_FISHEYE))
  90. {
  91. rs2::video_frame frame_left = frame_set.get_fisheye_frame(1);
  92. rs2::video_frame frame_right = frame_set.get_fisheye_frame(2);
  93. imLeft = cv::Mat(cv::Size(width_left, height_left), CV_8UC1, (void*)(frame_left.get_data()), cv::Mat::AUTO_STEP);
  94. imRight = cv::Mat(cv::Size(width_right, height_right), CV_8UC1, (void*)(frame_right.get_data()), cv::Mat::AUTO_STEP);
  95. if(imageScale != 1.f)
  96. {
  97. #ifdef REGISTER_TIMES
  98. #ifdef COMPILEDWITHC14
  99. std::chrono::steady_clock::time_point t_Start_Resize = std::chrono::steady_clock::now();
  100. #else
  101. std::chrono::monotonic_clock::time_point t_Start_Resize = std::chrono::monotonic_clock::now();
  102. #endif
  103. #endif
  104. int width = imLeft.cols * imageScale;
  105. int height = imLeft.rows * imageScale;
  106. cv::resize(imLeft, imLeft, cv::Size(width, height));
  107. cv::resize(imRight, imRight, cv::Size(width, height));
  108. #ifdef REGISTER_TIMES
  109. #ifdef COMPILEDWITHC14
  110. std::chrono::steady_clock::time_point t_End_Resize = std::chrono::steady_clock::now();
  111. #else
  112. std::chrono::monotonic_clock::time_point t_End_Resize = std::chrono::monotonic_clock::now();
  113. #endif
  114. t_resize = std::chrono::duration_cast<std::chrono::duration<double,std::milli> >(t_End_Resize - t_Start_Resize).count();
  115. SLAM.InsertResizeTime(t_resize);
  116. #endif
  117. }
  118. // clahe
  119. //clahe->apply(imLeft,imLeft);
  120. //clahe->apply(imRight,imRight);
  121. #ifdef REGISTER_TIMES
  122. #ifdef COMPILEDWITHC14
  123. std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
  124. #else
  125. std::chrono::monotonic_clock::time_point t1 = std::chrono::monotonic_clock::now();
  126. #endif
  127. #endif
  128. // Pass the image to the SLAM system
  129. SLAM.TrackStereo(imLeft, imRight, timestamp);
  130. #ifdef REGISTER_TIMES
  131. #ifdef COMPILEDWITHC14
  132. std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
  133. #else
  134. std::chrono::monotonic_clock::time_point t2 = std::chrono::monotonic_clock::now();
  135. #endif
  136. #endif
  137. #ifdef REGISTER_TIMES
  138. t_track = t_resize + std::chrono::duration_cast<std::chrono::duration<double,std::milli> >(t2 - t1).count();
  139. SLAM.InsertTrackTime(t_track);
  140. #endif
  141. }
  142. }
  143. pipe.stop();
  144. // Stop all threads
  145. SLAM.Shutdown();
  146. return 0;
  147. }