add a depth output
Publish sl::MEASURE::DEPTH as float32 [H, W] millimetres on every frame that carries image_bgr, with the intrinsics, frame_id and timestamps the other outputs carry. The map is copied out rather than published in place: sl::Mat rows can be padded, so the buffer is not necessarily H * W floats. inf and nan are cleared to 0 in the same pass, matching the reference driver — a hole is meaningful and a consumer cannot back-project a non-finite value. Consumers that need 3D can now back-project depth with the published intrinsics instead of receiving a point cloud. Also drops the stale objects entry from the dataflow outputs, missed when detection was removed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y7g2cBJ5e3r15ZbJ9WZn8c
This commit is contained in:
+47
@@ -5,6 +5,7 @@
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <initializer_list>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
@@ -58,6 +59,29 @@ void set_frame_stamps(Metadata& metadata, int64_t frame_id, int64_t device_ns, i
|
||||
metadata.set_timestamp("host_timestamp_ns", host_ns);
|
||||
}
|
||||
|
||||
// Copy the depth map out into a tightly packed [H, W] buffer of millimetres.
|
||||
//
|
||||
// The rows of an sl::Mat can be padded, so the buffer is not necessarily H * W
|
||||
// floats and cannot be published in place. inf and nan mark depth the SDK could
|
||||
// not measure; they are cleared to 0 here, as the reference driver does, because
|
||||
// a hole is meaningful and a consumer cannot back-project a non-finite value.
|
||||
std::vector<float> pack_depth(sl::Mat& depth_mat) {
|
||||
const int height = depth_mat.getHeight();
|
||||
const int width = depth_mat.getWidth();
|
||||
const size_t step = depth_mat.getStep(sl::MEM::CPU);
|
||||
const float* src = depth_mat.getPtr<sl::float1>();
|
||||
|
||||
std::vector<float> out(static_cast<size_t>(height) * static_cast<size_t>(width));
|
||||
for (int y = 0; y < height; y++) {
|
||||
const float* row = src + static_cast<size_t>(y) * step;
|
||||
float* dst = out.data() + static_cast<size_t>(y) * static_cast<size_t>(width);
|
||||
for (int x = 0; x < width; x++) {
|
||||
dst[x] = std::isfinite(row[x]) ? row[x] : 0.0f;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
int64_t host_timestamp_ns() {
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
@@ -100,6 +124,7 @@ int main() {
|
||||
}
|
||||
|
||||
sl::Mat& image_mat = camera.image();
|
||||
sl::Mat& depth_mat = camera.depth();
|
||||
sl::Mat& point_cloud_mat = camera.point_cloud();
|
||||
|
||||
int width = image_mat.getWidth();
|
||||
@@ -132,6 +157,28 @@ int main() {
|
||||
std::cerr << "Error sending image_bgr: " << std::string(image_result.error) << std::endl;
|
||||
}
|
||||
|
||||
// --- depth ---
|
||||
int depth_width = depth_mat.getWidth();
|
||||
int depth_height = depth_mat.getHeight();
|
||||
|
||||
std::vector<float> depth_buf = pack_depth(depth_mat);
|
||||
rust::Slice<const uint8_t> depth_slice{
|
||||
reinterpret_cast<const uint8_t*>(depth_buf.data()), depth_buf.size() * sizeof(float)};
|
||||
|
||||
auto depth_metadata = new_metadata();
|
||||
depth_metadata->set_string("dtype", "float32");
|
||||
depth_metadata->set_string("layout", "HW");
|
||||
depth_metadata->set_string("units", "mm");
|
||||
depth_metadata->set_list_int("shape", make_vector_int({depth_height, depth_width}));
|
||||
set_intrinsics(*depth_metadata, calib);
|
||||
set_frame_stamps(*depth_metadata, current_frame_id, device_ns, host_ns);
|
||||
|
||||
auto depth_result = send_output_with_metadata(
|
||||
dora_node.send_output, "depth", depth_slice, std::move(depth_metadata));
|
||||
if (!std::string(depth_result.error).empty()) {
|
||||
std::cerr << "Error sending depth: " << std::string(depth_result.error) << std::endl;
|
||||
}
|
||||
|
||||
// --- point_cloud ---
|
||||
int pc_width = point_cloud_mat.getWidth();
|
||||
int pc_height = point_cloud_mat.getHeight();
|
||||
|
||||
Reference in New Issue
Block a user