-
Notifications
You must be signed in to change notification settings - Fork 154
Add depth_to_point_cloud operator and demo #1588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
bbb99f2
b6954b1
5dc0068
70b9e2b
db6f37c
6f9b7f0
cd6dce1
81ff4a8
617bda6
ef3758d
1f9d8f5
78e2c1e
65fcbbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| project(depth_to_point_cloud_demo NONE) | ||
|
|
||
| find_package(holoscan 4.0.0 REQUIRED CONFIG | ||
| PATHS "/opt/nvidia/holoscan" "/workspace/holoscan-sdk/install") | ||
|
|
||
| if(BUILD_TESTING) | ||
| add_test(NAME depth_to_point_cloud_demo_python_test | ||
| COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/depth_to_point_cloud_demo.py --frames 10 | ||
| WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) | ||
|
|
||
| set_property(TEST depth_to_point_cloud_demo_python_test PROPERTY ENVIRONMENT | ||
| "PYTHONPATH=${GXF_LIB_DIR}/../python/lib:${CMAKE_BINARY_DIR}/python/lib") | ||
|
|
||
| set_tests_properties(depth_to_point_cloud_demo_python_test | ||
| PROPERTIES | ||
| PASS_REGULAR_EXPRESSION "valid=" | ||
| FAIL_REGULAR_EXPRESSION "(^|[^a-z])Error;ERROR;Failed") | ||
|
snknitheesh marked this conversation as resolved.
|
||
| endif() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Depth to Point Cloud Demo | ||
|
|
||
| A minimal, hardware-free demo of the [`depth_to_point_cloud`](../../operators/depth_to_point_cloud) | ||
|
snknitheesh marked this conversation as resolved.
Outdated
|
||
| operator. It generates a synthetic organized depth image (a gently tilting plane) plus an aligned | ||
| RGB image entirely on the GPU, deprojects it into an organized `H x W x 3` point cloud, and reports | ||
| the valid-point count and Z range each frame. No camera or dataset is required. | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add a splash image for this application? It could be anything that can represent this application, such as diagram, screenshot, or any elaborated illustration. Thanks
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add it. |
||
| ## Run | ||
|
|
||
| ```bash | ||
| ./holohub run depth_to_point_cloud_demo | ||
| # or directly: | ||
| python3 applications/depth_to_point_cloud_demo/depth_to_point_cloud_demo.py --frames 100 | ||
| ``` | ||
|
|
||
| Expected output (per frame): | ||
|
|
||
| ```text | ||
| [depth_to_point_cloud_demo] points=307200 valid=307200 z=[1.xxx, 2.xxx] m | ||
| ``` | ||
|
|
||
| ## Pipeline | ||
|
|
||
| ```text | ||
| SyntheticDepthGeneratorOp --depth--> DepthToPointCloudOp --point_cloud--> PointCloudStatsOp | ||
| --color--> | ||
| ``` | ||
|
|
||
| ## Using a real Intel RealSense camera | ||
|
|
||
| Replace the synthetic generator with the [`realsense_camera`](../../operators/realsense_camera) | ||
| operator. It applies librealsense's `units_transform` internally, so `depth_buffer` is emitted as | ||
| **`GRAY32F` float32 already in meters** (in a `VideoBuffer`), and `color_buffer` as `RGBA8`. Because | ||
| the depth is metric, use `depth_scale=1.0` — **not** `0.001`; the `0.001` (uint16 millimeters) value | ||
| is only for raw `Z16` sources that have not been unit-transformed: | ||
|
|
||
| ```python | ||
| from holohub.realsense_camera import RealsenseCameraOp | ||
|
|
||
| camera = RealsenseCameraOp(self, name="camera") | ||
| cloud = DepthToPointCloudOp( | ||
| self, name="point_cloud", allocator=..., | ||
| fx=fx, fy=fy, cx=cx, cy=cy, # from the camera's depth_camera_model intrinsics | ||
| depth_scale=1.0, # depth_buffer is float32 meters (units_transform applied) | ||
| depth_min=0.1, depth_max=10.0, | ||
| ) | ||
| self.add_flow(camera, cloud, {("depth_buffer", "depth")}) | ||
| self.add_flow(camera, cloud, {("color_buffer", "color")}) # color_channels = 4 (RGBA) | ||
| ``` | ||
|
|
||
| The camera also exposes `depth_camera_model` / `color_camera_model` outputs carrying the per-stream | ||
| intrinsics, which can drive the optional `intrinsics` input instead of static `fx/fy/cx/cy`. | ||
|
snknitheesh marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Interactive 3D visualization | ||
|
|
||
| To view the cloud, replace `PointCloudStatsOp` with `HolovizOp` configured to render the | ||
| `point_cloud` tensor as 3D points (and the `colors` tensor for per-point color). Reshaping the | ||
| organized `H x W x 3` cloud to `N x 3` and dropping invalid (NaN) points first is recommended. | ||
|
snknitheesh marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Requirements | ||
|
|
||
| - Holoscan SDK ≥ 4.0.0, CUDA, CuPy. Builds the `depth_to_point_cloud` operator (declared as a | ||
| dependency). Platforms: `x86_64`, `aarch64`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Demo for DepthToPointCloudOp. | ||
|
|
||
| Generates a synthetic organized depth image (a gently tilting plane) plus an aligned RGB | ||
| image entirely on the GPU, deprojects it into an organized point cloud with | ||
| DepthToPointCloudOp, and validates the result. No camera or dataset is required, so the | ||
| app runs in CI. See README.md for wiring a real Intel RealSense camera or adding Holoviz | ||
| 3D rendering. | ||
| """ | ||
|
|
||
| import argparse | ||
|
|
||
| import cupy as cp | ||
| from holoscan.conditions import CountCondition | ||
| from holoscan.core import Application, Operator, OperatorSpec | ||
| from holoscan.resources import BlockMemoryPool, CudaStreamPool, MemoryStorageType | ||
|
|
||
| from holohub.depth_to_point_cloud import DepthToPointCloudOp | ||
|
|
||
|
|
||
| class SyntheticDepthGeneratorOp(Operator): | ||
| """Emit a synthetic float32 depth image (meters) and an aligned uint8 RGB image.""" | ||
|
|
||
| def __init__(self, fragment, *args, width=640, height=480, **kwargs): | ||
| self.width = width | ||
| self.height = height | ||
| self.frame = 0 | ||
| ys, xs = cp.meshgrid( | ||
| cp.arange(height, dtype=cp.float32), | ||
| cp.arange(width, dtype=cp.float32), | ||
| indexing="ij", | ||
| ) | ||
| self._xs = xs | ||
| self._ys = ys | ||
|
snknitheesh marked this conversation as resolved.
|
||
| super().__init__(fragment, *args, **kwargs) | ||
|
|
||
| def setup(self, spec: OperatorSpec): | ||
| spec.output("depth") | ||
| spec.output("color") | ||
|
|
||
| def compute(self, op_input, op_output, context): | ||
| t = self.frame * 0.05 | ||
| # A tilted plane in meters: ~1.0 m near the top-left, increasing across the frame, | ||
| # with a slow global oscillation so successive frames differ. | ||
| depth = ( | ||
| 1.0 + 0.5 * (self._xs / self.width) + 0.4 * (self._ys / self.height) + 0.3 * cp.sin(t) | ||
| ).astype(cp.float32) | ||
|
|
||
| r = (255.0 * self._xs / self.width).astype(cp.uint8) | ||
| g = (255.0 * self._ys / self.height).astype(cp.uint8) | ||
| b = cp.full_like(r, 128) | ||
| color = cp.ascontiguousarray(cp.stack([r, g, b], axis=-1)) # HxWx3 uint8 | ||
|
|
||
| op_output.emit({"depth": depth}, "depth") | ||
| op_output.emit({"color": color}, "color") | ||
| self.frame += 1 | ||
|
|
||
|
|
||
| class PointCloudStatsOp(Operator): | ||
| """Pull the point cloud and report valid-point count and Z range (CI-friendly sink).""" | ||
|
|
||
| def setup(self, spec: OperatorSpec): | ||
| spec.input("in") | ||
|
|
||
| def compute(self, op_input, op_output, context): | ||
| msg = op_input.receive("in") | ||
| pc = cp.asarray(msg["point_cloud"]) # HxWx3 float32 | ||
| z = pc[..., 2] | ||
| valid = ~cp.isnan(z) | ||
| n_valid = int(valid.sum().get()) | ||
|
|
||
| # The colored path is connected, so a colors tensor must accompany the cloud and | ||
| # share its H x W footprint (3 uint8 channels). | ||
| colors = cp.asarray(msg["colors"]) # HxWx3 uint8 | ||
| if colors.shape[:2] != pc.shape[:2] or colors.shape[2] != 3: | ||
| raise RuntimeError( | ||
| f"colors shape {colors.shape} does not match cloud {pc.shape[:2]} x 3" | ||
| ) | ||
|
|
||
| if n_valid: | ||
| zmin = float(z[valid].min().get()) | ||
| zmax = float(z[valid].max().get()) | ||
| print( | ||
| f"[depth_to_point_cloud_demo] points={pc.shape[0] * pc.shape[1]} " | ||
| f"valid={n_valid} z=[{zmin:.3f}, {zmax:.3f}] m colors={tuple(colors.shape)}" | ||
| ) | ||
| else: | ||
| print("[depth_to_point_cloud_demo] no valid points") | ||
|
|
||
|
|
||
| class DepthToPointCloudDemoApp(Application): | ||
| def __init__(self, frames=100, width=640, height=480): | ||
| super().__init__() | ||
| self._frames = frames | ||
| self._width = width | ||
| self._height = height | ||
|
|
||
| def compose(self): | ||
| generator = SyntheticDepthGeneratorOp( | ||
| self, | ||
| CountCondition(self, count=self._frames), | ||
| name="generator", | ||
| width=self._width, | ||
| height=self._height, | ||
| ) | ||
|
|
||
| # Two device tensors per frame (HxWx3 float32 point cloud + HxWx3 uint8 colors) drawn | ||
| # from this pool; size each block for the larger (float32 XYZ) output and keep enough | ||
| # blocks for both tensors plus one frame of pipelining headroom. | ||
| out_blocks = 4 | ||
| block_size = self._width * self._height * 3 * 4 # float32 XYZ is the larger output | ||
| cloud = DepthToPointCloudOp( | ||
| self, | ||
| name="point_cloud", | ||
| allocator=BlockMemoryPool( | ||
| self, | ||
| name="pool", | ||
| storage_type=MemoryStorageType.DEVICE, | ||
| block_size=block_size, | ||
| num_blocks=out_blocks, | ||
| ), | ||
| # Pinhole intrinsics for the synthetic camera: square pixels (fx == fy) with the | ||
| # principal point at the image center. A single focal length is used for both axes | ||
| # by design; the focal length is independent of the image aspect ratio. | ||
| fx=float(self._width) * 0.8, | ||
| fy=float(self._width) * 0.8, | ||
|
snknitheesh marked this conversation as resolved.
|
||
| cx=(self._width - 1) / 2.0, | ||
| cy=(self._height - 1) / 2.0, | ||
| depth_scale=1.0, # synthetic depth is already in meters | ||
| depth_min=0.1, | ||
| depth_max=10.0, | ||
| cuda_stream_pool=CudaStreamPool(self, name="stream_pool", reserved_size=4), | ||
| ) | ||
|
|
||
| sink = PointCloudStatsOp(self, name="stats") | ||
|
|
||
| self.add_flow(generator, cloud, {("depth", "depth")}) | ||
| self.add_flow(generator, cloud, {("color", "color")}) | ||
| self.add_flow(cloud, sink, {("point_cloud", "in")}) | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description="DepthToPointCloudOp synthetic demo") | ||
| parser.add_argument("--frames", type=int, default=100, help="Number of frames to process") | ||
| parser.add_argument("--width", type=int, default=640) | ||
| parser.add_argument("--height", type=int, default=480) | ||
| args = parser.parse_args() | ||
|
|
||
| for name, value in ( | ||
| ("--frames", args.frames), | ||
| ("--width", args.width), | ||
| ("--height", args.height), | ||
| ): | ||
| if value <= 0: | ||
| parser.error(f"{name} must be a positive integer (got {value})") | ||
|
|
||
| app = DepthToPointCloudDemoApp(frames=args.frames, width=args.width, height=args.height) | ||
| app.run() | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| { | ||
| "$schema": "urn:holohub:application:v1", | ||
| "application": { | ||
| "name": "Depth to Point Cloud Demo", | ||
| "description": "Hardware-free demo of DepthToPointCloudOp: a synthetic GPU depth+RGB generator is deprojected into an organized point cloud and validated each frame.", | ||
| "authors": [ | ||
| { | ||
| "name": "Nitheesh Kumar", | ||
| "affiliation": "Zobot Lab" | ||
| } | ||
| ], | ||
| "language": "Python", | ||
| "version": "0.1.0", | ||
| "changelog": { | ||
| "0.1.0": "Initial release: synthetic depth generator -> DepthToPointCloudOp -> validation sink." | ||
| }, | ||
| "holoscan_sdk": { | ||
| "minimum_required_version": "4.0.0", | ||
| "tested_versions": [ | ||
| "4.3.0" | ||
| ] | ||
| }, | ||
| "platforms": [ | ||
| "x86_64", | ||
| "aarch64" | ||
| ], | ||
| "tags": ["Computer Vision and Perception", "Robotics", "Point Cloud", "Depth", "3D"], | ||
| "ranking": 3, | ||
| "requirements": { | ||
| "python-packages": [ | ||
| { | ||
| "name": "cupy", | ||
| "version": "13.6.0" | ||
| } | ||
| ] | ||
| }, | ||
| "run": { | ||
| "command": "python3 <holohub_app_source>/depth_to_point_cloud_demo.py", | ||
| "workdir": "holohub_bin" | ||
|
snknitheesh marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| cmake_minimum_required(VERSION 3.24) | ||
|
|
||
| project(depth_to_point_cloud LANGUAGES CXX CUDA) | ||
|
|
||
| find_package(holoscan 4.0.0 REQUIRED CONFIG | ||
| PATHS "/opt/nvidia/holoscan" "/workspace/holoscan-sdk/install") | ||
|
|
||
| add_library(depth_to_point_cloud SHARED | ||
| depth_to_point_cloud.cpp | ||
| depth_to_point_cloud.hpp | ||
| deproject.cu | ||
| deproject.hpp | ||
| ) | ||
|
|
||
| set_target_properties(depth_to_point_cloud | ||
| PROPERTIES | ||
| # compile for the architecture of the current GPU | ||
| CUDA_ARCHITECTURES "native" | ||
|
snknitheesh marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| target_link_libraries(depth_to_point_cloud | ||
| PUBLIC | ||
| holoscan::core | ||
| ) | ||
|
|
||
| target_include_directories(depth_to_point_cloud | ||
| INTERFACE | ||
| ${CMAKE_CURRENT_SOURCE_DIR} | ||
| ) | ||
|
|
||
| if(HOLOHUB_BUILD_PYTHON) | ||
| add_subdirectory(python) | ||
| endif() | ||
|
|
||
| if(BUILD_TESTING) | ||
| add_subdirectory(test) | ||
| endif() | ||
|
|
||
| install(TARGETS depth_to_point_cloud) | ||
Uh oh!
There was an error while loading. Please reload this page.