Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
tutorial-grabber-realsense-T265.cpp
1 
2 #include <visp3/core/vpImage.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/sensor/vpRealSense2.h>
7 #include <visp3/io/vpImageStorageWorker.h>
8 
12 int main(int argc, char **argv)
13 {
14 #if defined(VISP_HAVE_REALSENSE2) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0)) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
15  try {
16  std::string opt_seqname_left = "left-%04d.png", opt_seqname_right = "right-%04d.png";
17  int opt_record_mode = 0;
18  int opt_fps = 30;
19 
20  for (int i = 0; i < argc; i++) {
21  if (std::string(argv[i]) == "--record")
22  opt_record_mode = std::atoi(argv[i + 1]);
23  else if (std::string(argv[i]) == "--fps")
24  opt_fps = std::atoi(argv[i + 1]);
25  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
26  std::cout << "\nUsage: " << argv[0]
27  << " [--record <0: continuous | 1: single shot (default: 0)>]"
28  " [--help] [-h]\n"
29  << "\nExample to visualize images:\n"
30  << " " << argv[0] << "\n"
31  << "\nExamples to record single shot images:\n"
32  << " " << argv[0] << " --record 1\n"
33  << "\nExamples to record a sequence of images:\n"
34  << " " << argv[0] << " --record 0\n"
35  << std::endl;
36  return 0;
37  }
38  }
39 
40  std::cout << "Framerate : " << opt_fps << std::endl;
41 
42  std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
43 
44  std::cout << text_record_mode << std::endl;
45  std::cout << "Left record name: " << opt_seqname_left << std::endl;
46  std::cout << "Right record name: " << opt_seqname_right << std::endl;
47 
48  vpImage<unsigned char> I_left, I_right;
49 
50  vpRealSense2 g;
51  rs2::config config;
52  config.enable_stream(RS2_STREAM_FISHEYE, 1);
53  config.enable_stream(RS2_STREAM_FISHEYE, 2);
54  g.open(config);
55 
56  g.acquire(&I_left, &I_right);
57 
58  std::cout << "Image size : " << I_left.getWidth() << " " << I_right.getHeight() << std::endl;
59 
60 #ifdef VISP_HAVE_X11
61  vpDisplayX display_left(I_left, 10, 10, "Left image");
62  vpDisplayX display_right(I_right, I_left.getWidth(), 10, "Right image");
63 #elif defined(VISP_HAVE_GDI)
64  vpDisplayGDI display_left(I_left);
65  vpDisplayGDI display_right(I_right);
66 #elif defined(VISP_HAVE_OPENCV)
67  vpDisplayOpenCV display_left(I_left);
68  vpDisplayOpenCV display_right(I_right);
69 #else
70  std::cout << "No image viewer is available..." << std::endl;
71 #endif
72 
73  vpImageQueue<unsigned char> image_queue_left(opt_seqname_left, opt_record_mode);
74  vpImageQueue<unsigned char> image_queue_right(opt_seqname_right, opt_record_mode);
75  vpImageStorageWorker<unsigned char> image_left_storage_worker(std::ref(image_queue_left));
76  vpImageStorageWorker<unsigned char> image_right_storage_worker(std::ref(image_queue_right));
77  std::thread image_left_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_left_storage_worker);
78  std::thread image_right_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_right_storage_worker);
79 
80  bool quit = false;
81  while (!quit) {
82  double t = vpTime::measureTimeMs();
83 
84  g.acquire(&I_left, &I_right);
85 
86  vpDisplay::display(I_left);
87  vpDisplay::display(I_right);
88 
89  quit = image_queue_left.record(I_left);
90  quit |= image_queue_right.record(I_right, NULL, image_queue_left.getRecordingTrigger(), true);
91 
92  std::stringstream ss;
93  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
94  vpDisplay::displayText(I_left, I_left.getHeight() - 20, 10, ss.str(), vpColor::red);
95  vpDisplay::flush(I_left);
96  vpDisplay::flush(I_right);
97  }
98  image_queue_left.cancel();
99  image_queue_right.cancel();
100  image_left_storage_thread.join();
101  image_right_storage_thread.join();
102  } catch (const vpException &e) {
103  std::cout << "Catch an exception: " << e << std::endl;
104  }
105 #else
106  (void) argc;
107  (void) argv;
108 #if !(defined(VISP_HAVE_REALSENSE2) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0)))
109  std::cout << "Install librealsense version > 2.31.0, configure and build ViSP again to use this example" << std::endl;
110 #endif
111 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
112  std::cout << "This turorial should be built with c++11 support" << std::endl;
113 #endif
114 #endif
115 }
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:134
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void flush(const vpImage< unsigned char > &I)
bool open(const rs2::config &cfg=rs2::config())
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
static const vpColor red
Definition: vpColor.h:217
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
unsigned int getHeight() const
Definition: vpImage.h:188
unsigned int getWidth() const
Definition: vpImage.h:246
void acquire(vpImage< unsigned char > &grey, double *ts=NULL)