Visual Servoing Platform  version 3.6.1 under development (2025-03-14)
tutorial-grabber-rgbd-D435-structurecore.cpp
1 
2 #include <iostream>
3 
4 #include <visp3/core/vpConfig.h>
5 #include <visp3/core/vpImageConvert.h>
6 #include <visp3/gui/vpDisplayFactory.h>
7 #include <visp3/sensor/vpOccipitalStructure.h>
8 #include <visp3/sensor/vpRealSense2.h>
9 
13 int main(int argc, char **argv)
14 {
15 #if defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_OCCIPITAL_STRUCTURE) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
16 #ifdef ENABLE_VISP_NAMESPACE
17  using namespace VISP_NAMESPACE_NAME;
18 #endif
19  // Both cameras can stream color and depth in 640x480 resolution.
20  unsigned int width = 640, height = 480;
21 
22  // Grabbers.
23  vpRealSense2 rs;
25 
26  // Required images to acquire and display color and depth.
27  vpImage<float> sc_I_depth_raw(height, width);
28  vpImage<uint16_t> rs_I_depth_raw(height, width);
29  vpImage<vpRGBa> I_depth_sc(height, width), I_depth_rs(height, width);
30  vpImage<vpRGBa> I_color_sc(height, width), I_color_rs(height, width);
31 
32  std::shared_ptr<vpDisplay> d_rs_depth = vpDisplayFactory::createDisplay(I_depth_rs, 10, height + 10, "RealSense Depth");
33  std::shared_ptr<vpDisplay> d_sc_depth = vpDisplayFactory::createDisplay(I_depth_sc, width + 10, height + 10, "Structure Core Depth");
34  std::shared_ptr<vpDisplay> d_color_rs = vpDisplayFactory::createDisplay(I_color_rs, 10, 10, "RealSense Color");
35  std::shared_ptr<vpDisplay> d_color_sc = vpDisplayFactory::createDisplay(I_color_sc, width + 10, 10, "Structure Core Color");
36 
37  // Configuring and opening RealSense grabber.
38  rs2::config cfg;
39  cfg.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, 30);
40  cfg.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, 30);
41  rs.open(cfg);
42 
43  // Configuring and opening Structure Core grabber.
44  ST::CaptureSessionSettings settings;
45  settings.source = ST::CaptureSessionSourceId::StructureCore;
46  settings.structureCore.visibleEnabled = true;
47  settings.applyExpensiveCorrection = true; // Apply a correction and clean filter to the depth before streaming.
48  sc.open(settings);
49 
50  // Acquiring images.
51  for (;;) {
52  rs.acquire(reinterpret_cast<unsigned char *>(I_color_rs.bitmap),
53  reinterpret_cast<unsigned char *>(rs_I_depth_raw.bitmap), nullptr, nullptr, nullptr, nullptr, nullptr);
54  sc.acquire(reinterpret_cast<unsigned char *>(I_color_sc.bitmap),
55  reinterpret_cast<unsigned char *>(sc_I_depth_raw.bitmap));
56 
57  // Converting raw depth data to color images.
58  vpImageConvert::createDepthHistogram(rs_I_depth_raw, I_depth_rs);
59  vpImageConvert::createDepthHistogram(sc_I_depth_raw, I_depth_sc);
60 
61  vpDisplay::display(I_color_rs);
62  vpDisplay::display(I_color_sc);
63  vpDisplay::display(I_depth_rs);
64  vpDisplay::display(I_depth_sc);
65 
66  vpDisplay::flush(I_color_rs);
67  vpDisplay::flush(I_color_sc);
68  vpDisplay::flush(I_depth_rs);
69  vpDisplay::flush(I_depth_sc);
70 
71  if (vpDisplay::getClick(I_color_rs, false) || vpDisplay::getClick(I_color_sc, false) ||
72  vpDisplay::getClick(I_depth_rs, false) || vpDisplay::getClick(I_depth_sc, false)) {
73  break;
74  }
75  }
76 #else
77  (void)argc;
78  (void)argv;
79 #if !(defined(VISP_HAVE_OCCIPITAL_STRUCTURE))
80  std::cout << "Install libStructure, configure and build ViSP again to use this example" << std::endl;
81 #endif
82 #if !(defined(VISP_HAVE_REALSENSE2))
83  std::cout << "Install librealsense, configure and build ViSP again to use this example" << std::endl;
84 #endif
85 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
86  std::cout << "This tutorial should be built with c++11 support" << std::endl;
87 #endif
88 #endif
89 }
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
void acquire(vpImage< unsigned char > &gray, bool undistorted=false, double *ts=nullptr)
bool open(const ST::CaptureSessionSettings &settings)
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.