Visual Servoing Platform  version 3.4.0
tutorial-grabber-realsense.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/vpRealSense.h>
7 #include <visp3/sensor/vpRealSense2.h>
8 #include <visp3/io/vpImageStorageWorker.h>
9 
13 int main(int argc, char **argv)
14 {
15 #if defined(VISP_HAVE_REALSENSE) || defined(VISP_HAVE_REALSENSE2) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
16  try {
17  std::string opt_seqname;
18  int opt_record_mode = 0;
19  int opt_fps = 30;
20 
21  for (int i = 0; i < argc; i++) {
22  if (std::string(argv[i]) == "--seqname")
23  opt_seqname = std::string(argv[i + 1]);
24  else if (std::string(argv[i]) == "--record")
25  opt_record_mode = std::atoi(argv[i + 1]);
26  else if (std::string(argv[i]) == "--fps")
27  opt_fps = std::atoi(argv[i + 1]);
28  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
29  std::cout << "\nUsage: " << argv[0]
30  << " [--seqname <sequence name (default: empty>] [--record <0: continuous | 1: single shot (default: 0)>]"
31  " [--fps <6|15|30|60>] [--help] [-h]\n"
32  << "\nExample to visualize images:\n"
33  << " " << argv[0] << "\n"
34  << "\nExamples to record a sequence:\n"
35  << " " << argv[0] << " --seqname I%04d.png \n"
36  << " " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
37  << "\nExamples to record single shot images:\n"
38  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
39  << " " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
40  << std::endl;
41  return 0;
42  }
43  }
44 
45  if (opt_fps != 6 && opt_fps != 15 && opt_fps != 30 && opt_fps != 60) {
46  opt_fps = 30; // Default
47  }
48  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
49  std::cout << "Framerate : " << opt_fps << std::endl;
50 
51  std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
52 
53  if (! opt_seqname.empty()) {
54  std::cout << text_record_mode << std::endl;
55  std::cout << "Record name: " << opt_seqname << std::endl;
56  }
58 
59 #ifdef VISP_HAVE_REALSENSE2
60  std::cout << "SDK : Realsense 2" << std::endl;
61  vpRealSense2 g;
62  rs2::config config;
63  config.disable_stream(RS2_STREAM_DEPTH);
64  config.disable_stream(RS2_STREAM_INFRARED);
65  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, opt_fps);
66  g.open(config);
67 #else
68  std::cout << "SDK : Realsense 1" << std::endl;
69  vpRealSense g;
70  unsigned int width = 640, height = 480;
71  g.setStreamSettings(rs::stream::color, vpRealSense::vpRsStreamParams(width, height, rs::format::rgba8, 60));
72  g.open();
73 #endif
74  g.acquire(I);
75 
76  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
77 
78 #ifdef VISP_HAVE_X11
79  vpDisplayX d(I);
80 #elif defined(VISP_HAVE_GDI)
81  vpDisplayGDI d(I);
82 #elif defined(VISP_HAVE_OPENCV)
83  vpDisplayOpenCV d(I);
84 #else
85  std::cout << "No image viewer is available..." << std::endl;
86 #endif
87 
88  vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
89  vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
90  std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
91 
92  bool quit = false;
93  while (! quit) {
94  double t = vpTime::measureTimeMs();
95  g.acquire(I);
96 
98 
99  quit = image_queue.record(I);
100 
101  std::stringstream ss;
102  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
103  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
104  vpDisplay::flush(I);
105  }
106  image_queue.cancel();
107  image_storage_thread.join();
108  } catch (const vpException &e) {
109  std::cout << "Catch an exception: " << e << std::endl;
110  }
111 #else
112  (void) argc;
113  (void) argv;
114 #if !(defined(VISP_HAVE_REALSENSE) || defined(VISP_HAVE_REALSENSE2))
115  std::cout << "Install librealsense version > 2.31.0, configure and build ViSP again to use this example" << std::endl;
116 #endif
117 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
118  std::cout << "This turorial should be built with c++11 support" << std::endl;
119 #endif
120 #endif
121 }
unsigned int getWidth() const
Definition: vpImage.h:246
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:150
error that can be emited by ViSP classes.
Definition: vpException.h:71
void acquire(std::vector< vpColVector > &pointcloud)
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...
void setStreamSettings(const rs::stream &stream, const rs::preset &preset)
unsigned int getHeight() const
Definition: vpImage.h:188