Visual Servoing Platform version 3.5.0
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/sensor/vpOccipitalStructure.h>
7#include <visp3/io/vpImageStorageWorker.h>
8
12int main(int argc, char **argv)
13{
14#if defined(VISP_HAVE_OCCIPITAL_STRUCTURE) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
15 try {
16 std::string opt_seqname_visible = "visible-%04d.png", opt_seqname_depth = "depth-%04d.png";
17 int opt_record_mode = 0;
18 int opt_depth_fps = 30, opt_visible_fps = opt_depth_fps; // frame synchronization by default.
19 bool sxga = false; // Used for high resolution depth array (true => 1280x960).
20 bool frame_sync = true; // Used to set/unset frame synchronization (default: true).
21
22 for (int i = 0; i < argc; i++) {
23 if (std::string(argv[i]) == "--record")
24 opt_record_mode = std::atoi(argv[i + 1]);
25 else if (std::string(argv[i]) == "--depth_fps")
26 opt_depth_fps = std::atoi(argv[i + 1]);
27 else if (std::string(argv[i]) == "--visible_fps")
28 opt_visible_fps = std::atoi(argv[i + 1]);
29 else if (std::string(argv[i]) == "--sxga")
30 sxga = true;
31 else if (std::string(argv[i]) == "--no_frame_sync")
32 frame_sync = false;
33 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
34 std::cout << "\nUsage: " << argv[0]
35 << " [--record <0: continuous | 1: single shot (default: 0)>]"
36 " [--depth_fps <depth_frames_per_seconds> (default: 30)]"
37 " [--visible_fps <visible_frames_per_seconds> (default: 30)]"
38 " [--sxga (if exists, output 1280x960 depth array)]"
39 " [--no_frame_sync (if exists, disable frame synchronization)]"
40 " [--help] [-h]\n"
41 << "\nExample to visualize images:\n"
42 << " " << argv[0] << "\n"
43 << "\nExamples to record single shot images:\n"
44 << " " << argv[0] << " --record 1\n"
45 << "\nExamples to record a sequence of images:\n"
46 << " " << argv[0] << " --record 0\n"
47 << "\nExamples to record a sequence of images at different frame rates:\n"
48 << " " << argv[0] << " --record 0 --depth_fps 15 --visible_fps 10 --no_frame_sync\n"
49 << std::endl;
50 return 0;
51 }
52 }
53
54 std::cout << "Depth framerate : " << opt_depth_fps << std::endl;
55 std::cout << "Visible framerate : " << opt_visible_fps << std::endl;
56
57 std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
58
59 std::cout << text_record_mode << std::endl;
60 std::cout << "Visible record name: " << opt_seqname_visible << std::endl;
61 std::cout << "Depth record name: " << opt_seqname_depth << std::endl;
62
63 vpImage<vpRGBa> I_color, I_depth;
64 vpImage<float> I_depth_raw;
65
67
68 // There's an issue in the firmware when visible fps is set between 1 Hz and 2 Hz.
69 // The visible frame is damaged. The depth frame can be streamed at 1Hz without any problem.
70 if(opt_visible_fps < 2)
71 {
72 opt_visible_fps = 2;
73 }
74
75 ST::CaptureSessionSettings settings;
76 settings.source = ST::CaptureSessionSourceId::StructureCore;
77 settings.structureCore.visibleEnabled = true;
78 settings.frameSyncEnabled = frame_sync;
79 settings.structureCore.depthFramerate = opt_depth_fps;
80 settings.structureCore.visibleFramerate = opt_visible_fps;
81 if(sxga)
82 settings.structureCore.depthResolution = ST::StructureCoreDepthResolution::SXGA;
83 settings.applyExpensiveCorrection = true; // Apply a correction and clean filter to the depth before streaming.
84
85 bool is_open = g.open(settings);
86
87 if(is_open) {
88 // Wait some time to at least have 1 frame of each enabled stream (worst case scenario fps = 1Hz).
89 vpTime::wait(1000);
93
94#if defined(VISP_HAVE_X11)
95 vpDisplayX display_visible; // Visible image
96 display_visible.init(I_color, 10, 10, "Visible image");
97 vpDisplayX display_depth; // Depth image
98 display_depth.init(I_depth, 10+I_color.getWidth(), 10, "Depth image");
99#elif defined(VISP_HAVE_GDI)
100 vpDisplayGDI display_visible; // Visible image
101 display_visible.init(I_color, 10, 10, "Visible image");
102 vpDisplayGDI display_depth; // Depth image
103 display_depth.init(I_depth, 10, 10, "Depth image");
104#else
105 std::cout << "No viewer is installed..." << std::endl;
106#endif
107
108 vpImageQueue<vpRGBa> image_queue_visible(opt_seqname_visible, opt_record_mode);
109 std::thread image_visible_storage_thread;
110
111 vpImageStorageWorker<vpRGBa> image_visible_storage_worker(std::ref(image_queue_visible));
112 image_visible_storage_thread = std::thread(&vpImageStorageWorker<vpRGBa>::run, &image_visible_storage_worker);
113
114 vpImageQueue<vpRGBa> image_queue_depth(opt_seqname_depth, opt_record_mode);
115 vpImageStorageWorker<vpRGBa> image_depth_storage_worker(std::ref(image_queue_depth));
116 std::thread image_depth_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_depth_storage_worker);
117
118 bool quit = false;
119 double t;
120 while (!quit) {
122
123 g.acquire((unsigned char *)I_color.bitmap, (unsigned char *)I_depth_raw.bitmap);
124
125 vpDisplay::display(I_color);
126 vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
127 vpDisplay::display(I_depth);
128
129 quit = image_queue_visible.record(I_color);
130 quit |= image_queue_depth.record(I_depth, NULL, image_queue_visible.getRecordingTrigger(), true);
131
132 std::stringstream ss;
133 ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
134 vpDisplay::displayText(I_depth, I_depth.getHeight() - 20, 10, ss.str(), vpColor::red);
135 vpDisplay::flush(I_color);
136 vpDisplay::flush(I_depth);
137 }
138 image_queue_visible.cancel();
139 image_queue_depth.cancel();
140 image_visible_storage_thread.join();
141 image_depth_storage_thread.join();
142 }
143 } catch (const vpException &e) {
144 std::cout << "Catch an exception: " << e << std::endl;
145 }
146#else
147 (void) argc;
148 (void) argv;
149#if !(defined(VISP_HAVE_OCCIPITAL_STRUCTURE))
150 std::cout << "Install libStructure, configure and build ViSP again to use this example" << std::endl;
151#endif
152#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
153 std::cout << "This turorial should be built with c++11 support" << std::endl;
154#endif
155#endif
156}
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
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 emited by ViSP classes.
Definition: vpException.h:72
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
unsigned int getWidth() const
Definition: vpImage.h:246
Type * bitmap
points toward the bitmap
Definition: vpImage.h:143
unsigned int getHeight() const
Definition: vpImage.h:188
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()