Frame Detector

samples/advanced/frame_detector.cpp
 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
/**
 * Sample code for custom frame detection.
 */
#include <cepton_sdk_api.hpp>

int main(int argc, char **argv) {
  std::string capture_path;
  if (argc >= 2) capture_path = argv[1];

  // Initialize
  auto options = cepton_sdk::create_options();
  CEPTON_CHECK_ERROR(cepton_sdk::api::initialize(options, capture_path));
  cepton_sdk::api::SensorImageFrameCallback callback;
  CEPTON_CHECK_ERROR(callback.initialize());

  // Get sensor
  while (cepton_sdk::get_n_sensors() == 0)
    CEPTON_CHECK_ERROR(cepton_sdk::api::wait(0.1f));
  cepton_sdk::SensorInformation sensor_info;
  CEPTON_CHECK_ERROR(
      cepton_sdk::get_sensor_information_by_index(0, sensor_info));

  // Create detector
  cepton_sdk::util::FrameDetector<> detector(sensor_info);
  auto frame_options = cepton_sdk::create_frame_options();
  frame_options.mode = CEPTON_SDK_FRAME_COVER;
  CEPTON_CHECK_ERROR(detector.set_options(frame_options));
  const int stride = sensor_info.segment_count * sensor_info.return_count;
  CEPTON_CHECK_ERROR(callback.listen(
      [&](cepton_sdk::SensorHandle handle, std::size_t n_points,
          const cepton_sdk::SensorImagePoint *const c_image_points) {
        if (handle != sensor_info.handle) return;

        for (int i = 0; i < (int)n_points; i += stride) {
          auto &image_point = c_image_points[i];
          if (detector.update(image_point)) {
            auto &result = detector.previous_result();
            // `detector.period()` Frame period [seconds].
            // `result.timestamp` Frame timestamp [microseconds].

            // Handle frame...
          }
        }
      }));

  // Run
  CEPTON_CHECK_ERROR(cepton_sdk::api::wait(1.0f));

  // Deinitialize
  cepton_sdk::deinitialize().ignore();
}