• DepthAI-v2
  • Host Spatials in C++ giving random values

I'm trying to get the depth values in C++ without opencv. This is the function:

inline auto CalcSpatials(const std::shared_ptr<dai::ImgFrame> &frame)
    -> uint16_t {
  const uint32_t BLOCK_SIZE = 10;

  const auto centerX = frame->getWidth() / 2;
  const auto centerY = frame->getHeight() / 2;
  const auto startX = centerX - BLOCK_SIZE / 2;
  const auto endX = centerX + BLOCK_SIZE / 2;
  const auto startY = centerY - BLOCK_SIZE / 2;
  const auto endY = centerY + BLOCK_SIZE / 2;

  uint16_t sum = 0;
  // Convert ImgFrame's uint8_t raw buffer to a uint16_t buffer
  std::vector<uint16_t> data;
  data.resize(frame->getData().size() / sizeof(uint16_t));
  std::memcpy(data.data(), frame->getData().data(), frame->getData().size());

  // Loop through ROI and sum values
  for (auto y = startY; y < endY; ++y) {
    for (auto x = startX; x < endX; ++x) {
      const auto index = x + frame->getWidth() * y;
      sum += data[index];
    }
  }

  return sum / (BLOCK_SIZE * BLOCK_SIZE);
}

The pipeline is setup following this experiment in Python

  • jakaskerl replied to this.
  • Welp, now I feel like an idiot, I noticed when the values did change it seemed to be inverted (closer to the camera was bigger). Was a combination of accidentally using the disparity map, not the depth map, and the region I was averaging was too large.

    Hi tmayoff
    This is a pipeline used to calculate the spatials. The depth values should already be calculated inside the ImgFrame. I'm not exactly sure what you are trying to achieve. What cv function are you trying to rewrite in cpp?

    Thanks,
    Jaka

    Right now I'm just trying to get an average depth value for a block of pixels (like the region of interest opencv seems to have), and eventually an x,y as well. The values already in the ImgFrame don't seem to be correct or I'm misinterpreting them, taken as the raw uint8 values they're way too small but then reinterpreted into the uint16 format that the Stereo Depth node says it should be in, the values are all over the place.

    I feel like I've just over complicated this somewhere, so I apologize if this is just a simple thing I'm overlooking

    This may have been worded awkwardly. I'm trying to just get the depth values out of the ImgFrame, and print them to std::cout

      Hi tmayoff
      Could you post an example output you get (the depth image values)?
      If you are using the output from depth, then the values should be UINT16.

      Thanks,
      Jaka

      Depth (mm): 51
      Depth (mm): 51
      Depth (mm): 66
      Depth (mm): 41
      Depth (mm): 12
      Depth (mm): 64
      Depth (mm): 51
      Depth (mm): 51
      Depth (mm): 48
      Depth (mm): 74
      Depth (mm): 61
      Depth (mm): 645
      Depth (mm): 61
      Depth (mm): 51
      Depth (mm): 647
      Depth (mm): 64
      Depth (mm): 51
      Depth (mm): 10
      Depth (mm): 56
      Depth (mm): 51
      Depth (mm): 33
      Depth (mm): 56
      Depth (mm): 51
      Depth (mm): 59
      Depth (mm): 43
      Depth (mm): 30
      Depth (mm): 650
      Depth (mm): 51
      Depth (mm): 48
      Depth (mm): 642
      Depth (mm): 2
      Depth (mm): 51
      Depth (mm): 56
      Depth (mm): 53
      Depth (mm): 53
      Depth (mm): 33
      Depth (mm): 12
      Depth (mm): 51
      Depth (mm): 51
      Depth (mm): 61
      Depth (mm): 30
      Depth (mm): 48
      Depth (mm): 64
      Depth (mm): 41
      Depth (mm): 46
      Depth (mm): 12
      Depth (mm): 23
      Depth (mm): 51
      Depth (mm): 56
      Depth (mm): 48
      Depth (mm): 53
      Depth (mm): 28
      Depth (mm): 5
      Depth (mm): 23
      Depth (mm): 51
      Depth (mm): 71
      Depth (mm): 645
      Depth (mm): 59
      Depth (mm): 51
      Depth (mm): 51
      Depth (mm): 59
      Depth (mm): 77
      Depth (mm): 69
      Depth (mm): 82
      Depth (mm): 48
      Depth (mm): 28
      Depth (mm): 51
      Depth (mm): 41
      Depth (mm): 51
      Depth (mm): 28
      Depth (mm): 12
      Depth (mm): 41
      Depth (mm): 51
      Depth (mm): 35
      Depth (mm): 41
      Depth (mm): 56
      Depth (mm): 51
      Depth (mm): 53
      Depth (mm): 51
      Depth (mm): 51
      Depth (mm): 64
      Depth (mm): 36
      Depth (mm): 61
      Depth (mm): 51
      Depth (mm): 59
      Depth (mm): 12
      Depth (mm): 48
      Depth (mm): 56
      Depth (mm): 61
      Depth (mm): 43
      Depth (mm): 51
      Depth (mm): 51
      Depth (mm): 38
      Depth (mm): 59
      Depth (mm): 20
      Depth (mm): 64
      Depth (mm): 43
      Depth (mm): 64
      Depth (mm): 38
      Depth (mm): 28
      Depth (mm): 53
      Depth (mm): 53
      Depth (mm): 56
      Depth (mm): 624
      Depth (mm): 51
      Depth (mm): 38
      Depth (mm): 64
      Depth (mm): 36
      Depth (mm): 43
      Depth (mm): 51
      Depth (mm): 66
      Depth (mm): 56
      Depth (mm): 77
      Depth (mm): 645
      Depth (mm): 17
      Depth (mm): 33
      Depth (mm): 51
      Depth (mm): 30
      Depth (mm): 51
      Depth (mm): 2
      Depth (mm): 53
      Depth (mm): 41
      Depth (mm): 12
      Depth (mm): 46
      Depth (mm): 28
      Depth (mm): 33
      Depth (mm): 25
      Depth (mm): 43
      Depth (mm): 25
      Depth (mm): 46
      Depth (mm): 56
      Depth (mm): 79
      Depth (mm): 51
      Depth (mm): 38
      Depth (mm): 51
      Depth (mm): 51
      Depth (mm): 25
      Depth (mm): 64
      Depth (mm): 33
      Depth (mm): 64
      Depth (mm): 51
      Depth (mm): 28
      Depth (mm): 30
      Depth (mm): 53
      Depth (mm): 48
      Depth (mm): 38
      Depth (mm): 23
      Depth (mm): 61
      Depth (mm): 43
      Depth (mm): 51
      Depth (mm): 59
      Depth (mm): 43
      Depth (mm): 38
      Depth (mm): 35
      Depth (mm): 51
      Depth (mm): 51
      Depth (mm): 69
      Depth (mm): 51
      Depth (mm): 38
      Depth (mm): 38
      Depth (mm): 51
      Depth (mm): 46
      Depth (mm): 56
      Depth (mm): 51
      Depth (mm): 48
      Depth (mm): 77
      Depth (mm): 53
      Depth (mm): 97
      Depth (mm): 25
      Depth (mm): 59
      Depth (mm): 637
      Depth (mm): 35
      Depth (mm): 51
      Depth (mm): 48
      Depth (mm): 41
      Depth (mm): 51
      Depth (mm): 7
      Depth (mm): 74
      Depth (mm): 28
      Depth (mm): 46
      Depth (mm): 38
      Depth (mm): 51
      Depth (mm): 15
      Depth (mm): 20
      Depth (mm): 59
      Depth (mm): 43
      Depth (mm): 25
      Depth (mm): 59
      Depth (mm): 59
      Depth (mm): 61
      Depth (mm): 51
      Depth (mm): 51
      Depth (mm): 64
      Depth (mm): 46
      Depth (mm): 51
      Depth (mm): 18
      Depth (mm): 28
      Depth (mm): 51
      Depth (mm): 64
      Depth (mm): 51
      Depth (mm): 41
      Depth (mm): 48
      Depth (mm): 66
      Depth (mm): 51
      Depth (mm): 64
      Depth (mm): 647
      Depth (mm): 64
      Depth (mm): 59
      Depth (mm): 51

      With the above code, the values don't seem to change much, and I'm definitely not 20-50mm from the camera, I can record a video with the depth preview as well

      Welp, now I feel like an idiot, I noticed when the values did change it seemed to be inverted (closer to the camera was bigger). Was a combination of accidentally using the disparity map, not the depth map, and the region I was averaging was too large.