File: tutorial-grabber-structure-core.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (229 lines) | stat: -rw-r--r-- 9,834 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*! \example tutorial-grabber-structure-core.cpp */
#include <visp3/core/vpImage.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpImageStorageWorker.h>
#include <visp3/sensor/vpOccipitalStructure.h>

void usage(const char *argv[], int error)
{
  std::cout << "SYNOPSIS" << std::endl
            << "  " << argv[0] << " [--depth-fps <6|15|30|60>]"
            << " [--depth-fps <6|15|30|60>]"
            << " [--sxga]"
            << " [--no-frame-sync]"
            << " [--record <mode>]"
            << " [--no-display]"
            << " [--help] [-h]" << std::endl
            << std::endl;
  std::cout << "DESCRIPTION" << std::endl
            << "  --visible-fps <6|15|30|60>" << std::endl
            << "    Visible camera (gray or color) frames per second." << std::endl
            << "    Default: 30." << std::endl
            << std::endl
            << "  --depth-fps <6|15|30|60>" << std::endl
            << "    Depth camera frames per second." << std::endl
            << "    Default: 30." << std::endl
            << std::endl
            << "  --sxga" << std::endl
            << "    If available, output 1280x960 high resolution depth array." << std::endl
            << std::endl
            << "  --no-frame-sync" << std::endl
            << "    If available, disable frame synchronization." << std::endl
            << std::endl
            << "  --record <mode>" << std::endl
            << "    Allowed values for mode are:" << std::endl
            << "      0: record all the captures images (continuous mode)," << std::endl
            << "      1: record only images selected by a user click (single shot mode)." << std::endl
            << "    Default mode: 0" << std::endl
            << std::endl
            << "  --no-display" << std::endl
            << "    Disable displaying captured images." << std::endl
            << "    When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
            << std::endl
            << std::endl
            << "  --help, -h" << std::endl
            << "    Print this helper message." << std::endl
            << std::endl;
  std::cout << "USAGE" << std::endl
            << "  Example to visualize images:" << std::endl
            << "    " << argv[0] << std::endl
            << std::endl
            << "  Example to record a sequence of images:" << std::endl
            << "    " << argv[0] << " --record 0" << std::endl
            << std::endl
            << "  Example to record a sequence of images at different frame rates:" << std::endl
            << "    " << argv[0] << " --record 0 --depth-fps 15 --visible-fps 10 --no-frame-sync" << std::endl
            << std::endl
            << "  Example to record single shot images:\n"
            << "    " << argv[0] << " --record 1" << std::endl
            << std::endl;

  if (error) {
    std::cout << "Error" << std::endl
              << "  "
              << "Unsupported parameter " << argv[error] << std::endl;
  }
}

/*!
  Grab images from an Occipital Structure Core device.
 */
int main(int argc, const char *argv[])
{
#if defined(VISP_HAVE_OCCIPITAL_STRUCTURE) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
  try {
    std::string opt_seqname_visible = "visible-%04d.png", opt_seqname_depth = "depth-%04d.png";
    int opt_record_mode = 0;
    int opt_depth_fps = 30, opt_visible_fps = opt_depth_fps; // frame synchronization by default.
    bool opt_sxga = false;                                   // Used for high resolution depth array (true => 1280x960).
    bool opt_frame_sync = true;                              // Used to set/unset frame synchronization (default: true).
    bool opt_display = true;

    for (int i = 1; i < argc; i++) {
      if (std::string(argv[i]) == "--depth-fps") {
        opt_depth_fps = std::atoi(argv[i + 1]);
        i++;
      } else if (std::string(argv[i]) == "--visible-fps") {
        opt_visible_fps = std::atoi(argv[i + 1]);
        i++;
      } else if (std::string(argv[i]) == "--sxga") {
        opt_sxga = true;
      } else if (std::string(argv[i]) == "--no-frame-sync") {
        opt_frame_sync = false;
      } else if (std::string(argv[i]) == "--record") {
        opt_record_mode = std::atoi(argv[i + 1]);
        i++;
      } else if (std::string(argv[i]) == "--no-display") {
        opt_display = false;
      } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
        usage(argv, 0);
        return EXIT_SUCCESS;
      } else {
        usage(argv, i);
        return EXIT_FAILURE;
      }
    }

    if (!opt_display) {
      opt_record_mode = 0;
    }

    std::cout << "Depth framerate  : " << opt_depth_fps << std::endl;
    std::cout << "Visible framerate: " << opt_visible_fps << std::endl;
    std::cout << "Display          : " << (opt_display ? "enabled" : "disabled") << std::endl;

    std::string text_record_mode =
        std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));

    std::cout << text_record_mode << std::endl;
    std::cout << "Visible record name: " << opt_seqname_visible << std::endl;
    std::cout << "Depth record name: " << opt_seqname_depth << std::endl;

    vpImage<vpRGBa> I_color, I_depth;
    vpImage<float> I_depth_raw;

    vpOccipitalStructure g;

    // There's an issue in the firmware when visible fps is set between 1 Hz and 2 Hz.
    // The visible frame is damaged. The depth frame can be streamed at 1Hz without any problem.
    if (opt_visible_fps < 2) {
      opt_visible_fps = 2;
    }

    ST::CaptureSessionSettings settings;
    settings.source = ST::CaptureSessionSourceId::StructureCore;
    settings.structureCore.visibleEnabled = true;
    settings.frameSyncEnabled = opt_frame_sync;
    settings.structureCore.depthFramerate = opt_depth_fps;
    settings.structureCore.visibleFramerate = opt_visible_fps;
    if (opt_sxga)
      settings.structureCore.depthResolution = ST::StructureCoreDepthResolution::SXGA;
    settings.applyExpensiveCorrection = true; // Apply a correction and clean filter to the depth before streaming.

    bool is_open = g.open(settings);

    if (is_open) {
      // Wait some time to at least have 1 frame of each enabled stream (worst case scenario fps = 1Hz).
      vpTime::wait(1000);
      I_color = vpImage<vpRGBa>(g.getHeight(vpOccipitalStructure::visible), g.getWidth(vpOccipitalStructure::visible));
      I_depth = vpImage<vpRGBa>(g.getHeight(vpOccipitalStructure::depth), g.getWidth(vpOccipitalStructure::depth));
      I_depth_raw = vpImage<float>(g.getHeight(vpOccipitalStructure::depth), g.getWidth(vpOccipitalStructure::depth));

      vpDisplay *display_visible = NULL, *display_depth = NULL;
      if (opt_display) {
#if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
        std::cout << "No image viewer is available..." << std::endl;
        opt_display = false;
#endif
      }
      if (opt_display) {
#ifdef VISP_HAVE_X11
        display_visible = new vpDisplayX(I_color, 10, 10, "Visible image");
        display_depth = new vpDisplayX(I_depth, 10 + I_color.getWidth(), 10, "Depth image");
#elif defined(VISP_HAVE_GDI)
        display_visible = new vpDisplayGDI(I_color, 10, 10, "Visible image");
        display_depth = new vpDisplayGDI(I_depth, 10 + I_color.getWidth(), 10, "Depth image");
#elif defined(HAVE_OPENCV_HIGHGUI)
        display_visible = new vpDisplayOpenCV(I_color, 10, 10, "Visible image");
        display_depth = new vpDisplayOpenCV(I_depth, 10 + I_color.getWidth(), 10, "Depth image");
#endif
      }

      vpImageQueue<vpRGBa> image_queue_visible(opt_seqname_visible, opt_record_mode);
      std::thread image_visible_storage_thread;

      vpImageStorageWorker<vpRGBa> image_visible_storage_worker(std::ref(image_queue_visible));
      image_visible_storage_thread = std::thread(&vpImageStorageWorker<vpRGBa>::run, &image_visible_storage_worker);

      vpImageQueue<vpRGBa> image_queue_depth(opt_seqname_depth, opt_record_mode);
      vpImageStorageWorker<vpRGBa> image_depth_storage_worker(std::ref(image_queue_depth));
      std::thread image_depth_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_depth_storage_worker);

      bool quit = false;
      double t;
      while (!quit) {
        t = vpTime::measureTimeMs();

        g.acquire((unsigned char *)I_color.bitmap, (unsigned char *)I_depth_raw.bitmap);

        vpDisplay::display(I_color);
        vpImageConvert::createDepthHistogram(I_depth_raw, I_depth);
        vpDisplay::display(I_depth);

        quit = image_queue_visible.record(I_color);
        quit |= image_queue_depth.record(I_depth, NULL, image_queue_visible.getRecordingTrigger(), true);

        std::stringstream ss;
        ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
        vpDisplay::displayText(I_depth, I_depth.getHeight() - 20, 10, ss.str(), vpColor::red);
        vpDisplay::flush(I_color);
        vpDisplay::flush(I_depth);
      }
      image_queue_visible.cancel();
      image_queue_depth.cancel();
      image_visible_storage_thread.join();
      image_depth_storage_thread.join();

      if (display_visible) {
        delete display_visible;
      }
      if (display_depth) {
        delete display_depth;
      }
    }
  } catch (const vpException &e) {
    std::cout << "Catch an exception: " << e << std::endl;
  }
#else
  (void)argc;
  (void)argv;
#if !(defined(VISP_HAVE_OCCIPITAL_STRUCTURE))
  std::cout << "Install libStructure, configure and build ViSP again to use this example" << std::endl;
#endif
#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
  std::cout << "This tutorial should be built with c++11 support" << std::endl;
#endif
#endif
}