Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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 
9 #include "record_helper.h"
10 
14 int main(int argc, char **argv)
15 {
16 #if defined(VISP_HAVE_REALSENSE) || defined(VISP_HAVE_REALSENSE2)
17  try {
18  std::string opt_seqname;
19  int opt_record_mode = 0;
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]) == "--help" || std::string(argv[i]) == "-h") {
27  std::cout << "\nUsage: " << argv[0]
28  << " [--seqname <sequence name (default: empty>] [--record <0: continuous | 1: single shot (default: 0)>]"
29  " [--help] [-h]\n"
30  << "\nExample to visualize images:\n"
31  << " " << argv[0] << "\n"
32  << "\nExamples to record a sequence:\n"
33  << " " << argv[0] << " --seqname I%04d.png \n"
34  << " " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
35  << "\nExamples to record single shot images:\n"
36  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
37  << " " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
38  << std::endl;
39  return 0;
40  }
41  }
42 
43  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
44 
45  std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
46 
47  if (! opt_seqname.empty()) {
48  std::cout << text_record_mode << std::endl;
49  std::cout << "Record name: " << opt_seqname << std::endl;
50  }
52 
53 #ifdef VISP_HAVE_REALSENSE2
54  std::cout << "SDK: Realsense 2" << std::endl;
55  vpRealSense2 g;
56  rs2::config config;
57  config.disable_stream(RS2_STREAM_DEPTH);
58  config.disable_stream(RS2_STREAM_INFRARED);
59  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
60  g.open(config);
61 #else
62  std::cout << "SDK: Realsense 1" << std::endl;
63  vpRealSense g;
64  unsigned int width = 640, height = 480;
65  g.setStreamSettings(rs::stream::color, vpRealSense::vpRsStreamParams(width, height, rs::format::rgba8, 60));
66  g.open();
67 #endif
68  g.acquire(I);
69 
70  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
71 
72 #ifdef VISP_HAVE_X11
73  vpDisplayX d(I);
74 #elif defined(VISP_HAVE_GDI)
75  vpDisplayGDI d(I);
76 #elif defined(VISP_HAVE_OPENCV)
77  vpDisplayOpenCV d(I);
78 #else
79  std::cout << "No image viewer is available..." << std::endl;
80 #endif
81 
82  bool quit = false;
83  while (! quit) {
84  double t = vpTime::measureTimeMs();
85  g.acquire(I);
86 
88 
89  quit = record_helper(opt_seqname, opt_record_mode, I);
90 
91  std::stringstream ss;
92  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
93  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
95  }
96  } catch (const vpException &e) {
97  std::cout << "Catch an exception: " << e << std::endl;
98  }
99 #else
100  (void) argc;
101  (void) argv;
102  std::cout << "Install librealsense, configure and build ViSP again to use this example" << std::endl;
103 #endif
104 }
unsigned int getWidth() const
Definition: vpImage.h:239
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
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:151
error that can be emited by ViSP classes.
Definition: vpException.h:71
void acquire(std::vector< vpColVector > &pointcloud)
void open(const rs2::config &cfg=rs2::config())
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
static const vpColor red
Definition: vpColor.h:180
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:178