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