Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
tutorial-grabber-structure-core.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/io/vpImageStorageWorker.h>
7 #include <visp3/sensor/vpOccipitalStructure.h>
8 
9 void usage(const char *argv[], int error)
10 {
11  std::cout << "SYNOPSIS" << std::endl
12  << " " << argv[0] << " [--depth-fps <6|15|30|60>]"
13  << " [--depth-fps <6|15|30|60>]"
14  << " [--sxga]"
15  << " [--no-frame-sync]"
16  << " [--record <mode>]"
17  << " [--no-display]"
18  << " [--help] [-h]" << std::endl
19  << std::endl;
20  std::cout << "DESCRIPTION" << std::endl
21  << " --visible-fps <6|15|30|60>" << std::endl
22  << " Visible camera (gray or color) frames per second." << std::endl
23  << " Default: 30." << std::endl
24  << std::endl
25  << " --depth-fps <6|15|30|60>" << std::endl
26  << " Depth camera frames per second." << std::endl
27  << " Default: 30." << std::endl
28  << std::endl
29  << " --sxga" << std::endl
30  << " If available, output 1280x960 high resolution depth array." << std::endl
31  << std::endl
32  << " --no-frame-sync" << std::endl
33  << " If available, disable frame synchronization." << std::endl
34  << std::endl
35  << " --record <mode>" << std::endl
36  << " Allowed values for mode are:" << std::endl
37  << " 0: record all the captures images (continuous mode)," << std::endl
38  << " 1: record only images selected by a user click (single shot mode)." << std::endl
39  << " Default mode: 0" << std::endl
40  << std::endl
41  << " --no-display" << std::endl
42  << " Disable displaying captured images." << std::endl
43  << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
44  << std::endl
45  << std::endl
46  << " --help, -h" << std::endl
47  << " Print this helper message." << std::endl
48  << std::endl;
49  std::cout << "USAGE" << std::endl
50  << " Example to visualize images:" << std::endl
51  << " " << argv[0] << std::endl
52  << std::endl
53  << " Example to record a sequence of images:" << std::endl
54  << " " << argv[0] << " --record 0" << std::endl
55  << std::endl
56  << " Example to record a sequence of images at different frame rates:" << std::endl
57  << " " << argv[0] << " --record 0 --depth-fps 15 --visible-fps 10 --no-frame-sync" << std::endl
58  << std::endl
59  << " Example to record single shot images:\n"
60  << " " << argv[0] << " --record 1" << std::endl
61  << std::endl;
62 
63  if (error) {
64  std::cout << "Error" << std::endl
65  << " "
66  << "Unsupported parameter " << argv[error] << std::endl;
67  }
68 }
69 
73 int main(int argc, const char *argv[])
74 {
75 #if defined(VISP_HAVE_OCCIPITAL_STRUCTURE) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
76  try {
77  std::string opt_seqname_visible = "visible-%04d.png", opt_seqname_depth = "depth-%04d.png";
78  int opt_record_mode = 0;
79  int opt_depth_fps = 30, opt_visible_fps = opt_depth_fps; // frame synchronization by default.
80  bool opt_sxga = false; // Used for high resolution depth array (true => 1280x960).
81  bool opt_frame_sync = true; // Used to set/unset frame synchronization (default: true).
82  bool opt_display = true;
83 
84  for (int i = 1; i < argc; i++) {
85  if (std::string(argv[i]) == "--depth-fps") {
86  opt_depth_fps = std::atoi(argv[i + 1]);
87  i++;
88  } else if (std::string(argv[i]) == "--visible-fps") {
89  opt_visible_fps = std::atoi(argv[i + 1]);
90  i++;
91  } else if (std::string(argv[i]) == "--sxga") {
92  opt_sxga = true;
93  } else if (std::string(argv[i]) == "--no-frame-sync") {
94  opt_frame_sync = false;
95  } else if (std::string(argv[i]) == "--record") {
96  opt_record_mode = std::atoi(argv[i + 1]);
97  i++;
98  } else if (std::string(argv[i]) == "--no-display") {
99  opt_display = false;
100  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
101  usage(argv, 0);
102  return EXIT_SUCCESS;
103  } else {
104  usage(argv, i);
105  return EXIT_FAILURE;
106  }
107  }
108 
109  if (!opt_display) {
110  opt_record_mode = 0;
111  }
112 
113  std::cout << "Depth framerate : " << opt_depth_fps << std::endl;
114  std::cout << "Visible framerate: " << opt_visible_fps << std::endl;
115  std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
116 
117  std::string text_record_mode =
118  std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
119 
120  std::cout << text_record_mode << std::endl;
121  std::cout << "Visible record name: " << opt_seqname_visible << std::endl;
122  std::cout << "Depth record name: " << opt_seqname_depth << std::endl;
123 
124  vpImage<vpRGBa> I_color, I_depth;
125  vpImage<float> I_depth_raw;
126 
128 
129  // There's an issue in the firmware when visible fps is set between 1 Hz and 2 Hz.
130  // The visible frame is damaged. The depth frame can be streamed at 1Hz without any problem.
131  if (opt_visible_fps < 2) {
132  opt_visible_fps = 2;
133  }
134 
135  ST::CaptureSessionSettings settings;
136  settings.source = ST::CaptureSessionSourceId::StructureCore;
137  settings.structureCore.visibleEnabled = true;
138  settings.frameSyncEnabled = opt_frame_sync;
139  settings.structureCore.depthFramerate = opt_depth_fps;
140  settings.structureCore.visibleFramerate = opt_visible_fps;
141  if (opt_sxga)
142  settings.structureCore.depthResolution = ST::StructureCoreDepthResolution::SXGA;
143  settings.applyExpensiveCorrection = true; // Apply a correction and clean filter to the depth before streaming.
144 
145  bool is_open = g.open(settings);
146 
147  if (is_open) {
148  // Wait some time to at least have 1 frame of each enabled stream (worst case scenario fps = 1Hz).
149  vpTime::wait(1000);
153 
154  vpDisplay *display_visible = NULL, *display_depth = NULL;
155  if (opt_display) {
156 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
157  std::cout << "No image viewer is available..." << std::endl;
158  opt_display = false;
159 #endif
160  }
161  if (opt_display) {
162 #ifdef VISP_HAVE_X11
163  display_visible = new vpDisplayX(I_color, 10, 10, "Visible image");
164  display_depth = new vpDisplayX(I_depth, 10 + I_color.getWidth(), 10, "Depth image");
165 #elif defined(VISP_HAVE_GDI)
166  display_visible = new vpDisplayGDI(I_color, 10, 10, "Visible image");
167  display_depth = new vpDisplayGDI(I_depth, 10 + I_color.getWidth(), 10, "Depth image");
168 #elif defined(HAVE_OPENCV_HIGHGUI)
169  display_visible = new vpDisplayOpenCV(I_color, 10, 10, "Visible image");
170  display_depth = new vpDisplayOpenCV(I_depth, 10 + I_color.getWidth(), 10, "Depth image");
171 #endif
172  }
173 
174  vpImageQueue<vpRGBa> image_queue_visible(opt_seqname_visible, opt_record_mode);
175  std::thread image_visible_storage_thread;
176 
177  vpImageStorageWorker<vpRGBa> image_visible_storage_worker(std::ref(image_queue_visible));
178  image_visible_storage_thread = std::thread(&vpImageStorageWorker<vpRGBa>::run, &image_visible_storage_worker);
179 
180  vpImageQueue<vpRGBa> image_queue_depth(opt_seqname_depth, opt_record_mode);
181  vpImageStorageWorker<vpRGBa> image_depth_storage_worker(std::ref(image_queue_depth));
182  std::thread image_depth_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_depth_storage_worker);
183 
184  bool quit = false;
185  double t;
186  while (!quit) {
187  t = vpTime::measureTimeMs();
188 
189  g.acquire((unsigned char *)I_color.bitmap, (unsigned char *)I_depth_raw.bitmap);
190 
191  vpDisplay::display(I_color);
192  vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
193  vpDisplay::display(I_depth);
194 
195  quit = image_queue_visible.record(I_color);
196  quit |= image_queue_depth.record(I_depth, NULL, image_queue_visible.getRecordingTrigger(), true);
197 
198  std::stringstream ss;
199  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
200  vpDisplay::displayText(I_depth, I_depth.getHeight() - 20, 10, ss.str(), vpColor::red);
201  vpDisplay::flush(I_color);
202  vpDisplay::flush(I_depth);
203  }
204  image_queue_visible.cancel();
205  image_queue_depth.cancel();
206  image_visible_storage_thread.join();
207  image_depth_storage_thread.join();
208 
209  if (display_visible) {
210  delete display_visible;
211  }
212  if (display_depth) {
213  delete display_depth;
214  }
215  }
216  } catch (const vpException &e) {
217  std::cout << "Catch an exception: " << e << std::endl;
218  }
219 #else
220  (void)argc;
221  (void)argv;
222 #if !(defined(VISP_HAVE_OCCIPITAL_STRUCTURE))
223  std::cout << "Install libStructure, configure and build ViSP again to use this example" << std::endl;
224 #endif
225 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
226  std::cout << "This tutorial should be built with c++11 support" << std::endl;
227 #endif
228 #endif
229 }
static const vpColor red
Definition: vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:132
Class that defines generic functionalities for display.
Definition: vpDisplay.h:173
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:59
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
unsigned int getWidth() const
Definition: vpImage.h:242
Type * bitmap
points toward the bitmap
Definition: vpImage.h:139
unsigned int getHeight() const
Definition: vpImage.h:184
unsigned int getHeight(vpOccipitalStructureStream stream_type)
void acquire(vpImage< unsigned char > &gray, bool undistorted=false, double *ts=NULL)
unsigned int getWidth(vpOccipitalStructureStream stream_type)
bool open(const ST::CaptureSessionSettings &settings)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()