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