Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-grabber-realsense-T265.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpImage.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/io/vpImageStorageWorker.h>
8 #include <visp3/sensor/vpRealSense2.h>
9 
10 void usage(const char *argv[], int error)
11 {
12  std::cout << "SYNOPSIS" << std::endl
13  << " " << argv[0] << " [--fps <6|15|30|60>]"
14  << " [--record <mode>]"
15  << " [--no-display]"
16  << " [--help] [-h]" << std::endl
17  << std::endl;
18  std::cout << "DESCRIPTION" << std::endl
19  << " --fps <6|15|30|60>" << std::endl
20  << " Frames per second." << std::endl
21  << " Default: 30." << std::endl
22  << std::endl
23  << " --record <mode>" << std::endl
24  << " Allowed values for mode are:" << std::endl
25  << " 0: record all the captures images (continuous mode)," << std::endl
26  << " 1: record only images selected by a user click (single shot mode)." << std::endl
27  << " Default mode: 0" << std::endl
28  << std::endl
29  << " --no-display" << std::endl
30  << " Disable displaying captured images." << std::endl
31  << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
32  << std::endl
33  << std::endl
34  << " --help, -h" << std::endl
35  << " Print this helper message." << std::endl
36  << std::endl;
37  std::cout << "USAGE" << std::endl
38  << " Example to visualize images:" << std::endl
39  << " " << argv[0] << std::endl
40  << std::endl
41  << " Example to record a sequence of images:" << std::endl
42  << " " << argv[0] << " --record 0" << std::endl
43  << std::endl
44  << " Example to record single shot images:\n"
45  << " " << argv[0] << " --record 1" << std::endl
46  << std::endl;
47 
48  if (error) {
49  std::cout << "Error" << std::endl
50  << " "
51  << "Unsupported parameter " << argv[error] << std::endl;
52  }
53 }
54 
58 int main(int argc, const char *argv[])
59 {
60 #if defined(VISP_HAVE_REALSENSE2) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0)) && defined(VISP_HAVE_THREADS)
61 #ifdef ENABLE_VISP_NAMESPACE
62  using namespace VISP_NAMESPACE_NAME;
63 #endif
64  try {
65  std::string opt_seqname_left = "left-%04d.png", opt_seqname_right = "right-%04d.png";
66  int opt_record_mode = 0;
67  int opt_fps = 30;
68  bool opt_display = true;
69 
70  for (int i = 1; i < argc; i++) {
71  if (std::string(argv[i]) == "--fps") {
72  opt_fps = std::atoi(argv[i + 1]);
73  i++;
74  }
75  else if (std::string(argv[i]) == "--record") {
76  opt_record_mode = std::atoi(argv[i + 1]);
77  i++;
78  }
79  else if (std::string(argv[i]) == "--no-display") {
80  opt_display = false;
81  }
82  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
83  usage(argv, 0);
84  return EXIT_SUCCESS;
85  }
86  else {
87  usage(argv, i);
88  return EXIT_FAILURE;
89  }
90  }
91 
92  if (!opt_display) {
93  opt_record_mode = 0;
94  }
95 
96  std::cout << "Framerate : " << opt_fps << std::endl;
97  std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
98 
99  std::string text_record_mode =
100  std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
101 
102  std::cout << text_record_mode << std::endl;
103  std::cout << "Left record name: " << opt_seqname_left << std::endl;
104  std::cout << "Right record name: " << opt_seqname_right << std::endl;
105 
106  vpImage<unsigned char> I_left, I_right;
107 
108  vpRealSense2 g;
109  rs2::config config;
110  config.enable_stream(RS2_STREAM_FISHEYE, 1);
111  config.enable_stream(RS2_STREAM_FISHEYE, 2);
112  g.open(config);
113 
114  g.acquire(&I_left, &I_right);
115 
116  std::cout << "Image size : " << I_left.getWidth() << " " << I_right.getHeight() << std::endl;
117 
118  vpDisplay *display_left = nullptr, *display_right = nullptr;
119  if (opt_display) {
120 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
121  std::cout << "No image viewer is available..." << std::endl;
122  opt_display = false;
123 #endif
124  }
125  if (opt_display) {
126 #ifdef VISP_HAVE_X11
127  display_left = new vpDisplayX(I_left, 10, 10, "Left image");
128  display_right = new vpDisplayX(I_right, I_left.getWidth(), 10, "Right image");
129 #elif defined(VISP_HAVE_GDI)
130  display_left = new vpDisplayGDI(I_left, 10, 10, "Left image");
131  display_right = new vpDisplayGDI(I_right, I_left.getWidth(), 10, "Right image");
132 #elif defined(HAVE_OPENCV_HIGHGUI)
133  display_left = new vpDisplayOpenCV(I_left, 10, 10, "Left image");
134  display_right = new vpDisplayOpenCV(I_right, I_left.getWidth(), 10, "Right image");
135 #endif
136  }
137 
138  vpImageQueue<unsigned char> image_queue_left(opt_seqname_left, opt_record_mode);
139  vpImageQueue<unsigned char> image_queue_right(opt_seqname_right, opt_record_mode);
140  vpImageStorageWorker<unsigned char> image_left_storage_worker(std::ref(image_queue_left));
141  vpImageStorageWorker<unsigned char> image_right_storage_worker(std::ref(image_queue_right));
142  std::thread image_left_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_left_storage_worker);
143  std::thread image_right_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_right_storage_worker);
144 
145  bool quit = false;
146  while (!quit) {
147  double t = vpTime::measureTimeMs();
148 
149  g.acquire(&I_left, &I_right);
150 
151  vpDisplay::display(I_left);
152  vpDisplay::display(I_right);
153 
154  quit = image_queue_left.record(I_left);
155  quit |= image_queue_right.record(I_right, nullptr, image_queue_left.getRecordingTrigger(), true);
156 
157  std::stringstream ss;
158  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
159  vpDisplay::displayText(I_left, I_left.getHeight() - 20, 10, ss.str(), vpColor::red);
160  vpDisplay::flush(I_left);
161  vpDisplay::flush(I_right);
162  }
163  image_queue_left.cancel();
164  image_queue_right.cancel();
165  image_left_storage_thread.join();
166  image_right_storage_thread.join();
167 
168  if (display_left) {
169  delete display_left;
170  }
171  if (display_right) {
172  delete display_right;
173  }
174  }
175  catch (const vpException &e) {
176  std::cout << "Catch an exception: " << e << std::endl;
177 }
178 #else
179  (void)argc;
180  (void)argv;
181 #if !(defined(VISP_HAVE_REALSENSE2) && (RS2_API_VERSION > ((2 * 10000) + (31 * 100) + 0)))
182  std::cout << "Install librealsense version > 2.31.0, configure and build ViSP again to use this example" << std::endl;
183 #endif
184 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
185  std::cout << "This tutorial should be built with c++11 support" << std::endl;
186 #endif
187 #endif
188 }
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
VISP_EXPORT double measureTimeMs()