
How I built a depth image to occupancy grid pipeline and checked it with Nav2
A portfolio walkthrough of depth-occupancy-nav: a synthetic depth frame, pinhole back-projection, a height gate, a two-pass log-odds occupancy grid, A* planning, a ROS 2 node that publishes /map, and CI launch tests that hand the map to the Nav2 costmap and Navfn planner. Simulation only, synthetic depth, with the measured numbers and the honest limits.
Simulation only, synthetic depth
This is a portfolio demo. It runs on one synthetic depth frame rendered from an analytic scene: a ground plane and two boxes. It was not tested on a real depth camera or a real robot, and nothing here is a claim about real sensor data. The ROS 2 and Nav2 parts ran only in CI, inside a pinned ROS 2 Jazzy container. I did not invent occupancy grids, log-odds updates, grid traversal, A*, or the Nav2 costmap and Navfn planner. This is an original implementation, inspired by the published methods credited below.
Try the live demo
The static viewer is on GitHub Pages: https://applycoding.github.io/depth-occupancy-nav/
The page title is "depth-occupancy-nav (simulation only, synthetic depth)" and the header reads "Simulation only. Synthetic depth. Not validated on real cameras or robots." The viewer recomputes the whole pipeline in the browser in TypeScript. You can move a noise slider and the z_min and z_max height gate sliders, and pick the open, around, or shadow goal. The footer shows the build SHA and sample=false from the report.json written by CI.

Image: a fresh screenshot of the live demo on commit b12d114, around goal selected. Simulation only, synthetic depth. applycoding.com.
What the project is
depth-occupancy-nav (repo: applycoding/depth-occupancy-nav, commit b12d114) turns a depth image into a 2D occupancy grid and plans on it. It is an original implementation of pinhole back-projection, a height gate, Amanatides and Woo grid traversal, a two-pass log-odds occupancy grid, and 8-connected A*, inspired by Moravec and Elfes (1985), Thrun, Burgard and Fox (Probabilistic Robotics, chapter 9), Amanatides and Woo (1987), and the Nav2 costmap ideas.
| Layer | Role |
|---|---|
src/depth_occupancy_nav/ |
Python core (NumPy, Pillow): synthetic renderer, back-projection, height gate, ray walk, log-odds grid, A* and inflation |
ros2/depth_occupancy_nav_ros/ |
ROS 2 Jazzy node that checks the depth Image against CameraInfo and publishes the grid as /map |
web/ |
TypeScript and Vite viewer that recomputes the same numbers in the browser |
| GitHub Actions | core, ros, web and pages jobs on ubuntu-24.04, SHA-pinned Actions, hashed Python deps |
Nav2 Navfn and the Nav2 costmap StaticLayer are unmodified third-party Nav2 apt packages. They are used only in CI launch test B, to check that a standard planner accepts the published map.
How the pipeline works
- Synthetic depth. A locked 80x60 pinhole camera (60 degree field of view,
fx = fy = 69.282,cx = 39.5,cy = 29.5) sits 0.80 m above the ground and looks along map x. Each pixel stores camera-Z depth up to 10 m. 2434 pixels are valid. - Back-projection.
pixel_to_mapturns each valid pixel into a map-frame point with the pinhole model and a fixed optical-to-map rotation. - Height gate. Points with map z in
[0.15, 1.50]m are kept as obstacle hits (823 of 2434). This is a simple height gate, not RANSAC or a fitted ground plane. Ground hits still clear the cells along their rays. - Ray walk.
ray_walk_2dsteps from the camera cell to the endpoint cell with Amanatides and Woo traversal on a 0.10 m grid of 80x60 cells. Ties step x first. - Two-pass log-odds. Pass one adds the free update (
log(0.35/0.65)) to every cell a ray crosses. Pass two adds the occupied update (log(0.70/0.30)) at kept endpoints. Log-odds are clamped to[-2.0, 3.5]. Probability 0.65 or more becomes 100, 0.35 or less becomes 0, cells never touched stay -1, and anything between is rounded half to even. - Planning. 8-connected A* with diagonal cost sqrt(2), no corner cutting, and only free (0) cells as traversable. Unknown cells are not traversable.
def prob_to_occupancy_value(p: float, updated: bool) -> int:
if not updated:
return -1
if p >= 0.65:
return 100
if p <= 0.35:
return 0
return round(p * 100)
Doing all free updates before any occupied update means a ray that grazes past an obstacle face cannot erase that face. The repo keeps two one-pass variants (M1 and M2) as negative tests: both clear cell (37, 30) at the box face and end with 0 occupied cells.
Build and run locally
Core (Python 3.12 in CI):
python3 -m pip install --require-hashes -r requirements.txt
python3 -m pip install -e .
pytest
ruff check src tests
Viewer (Node 26.13.0 in CI):
cd web && npm ci && npm run typecheck && npm test && npm run build
cd web && npm run test:e2e
The ROS launch tests need Docker and the same pre-fetched Nav2 packages as CI (scripts/run_ros_tests.sh). The stock ros:jazzy-ros-base image does not include Nav2.
Results from the tests
All numbers below are asserted in tests/test_criteria.py on the locked noise-free frame:
| Quantity | Value |
|---|---|
| Valid depth pixels | 2434 |
| Kept by the height gate | 823 |
| Occupied / free / unknown / mid cells | 22 / 1698 / 3080 / 0 |
| Open goal A* path, cells and cost | 11, 10.0 |
| Around goal A* path, cells and cost | 51, 35 + 15 sqrt(2) |
| Occupied cells after 0.15 m inflation | 130 |
With seeded noise (1% relative plus 0.01 m absolute, seed 1) the depth RMSE is 0.0364728080413652 m and the grid has 41 / 1684 / 3062 / 13 cells. The around path is still 51 cells.
Three mutants must fail in the expected way:
| Mutant | Occupied / free / unknown / mid |
|---|---|
| Height gate off (every endpoint occupied) | 78 / 1247 / 3080 / 395 |
| One-pass M1 | 0 / 1709 / 3080 / 11 |
| One-pass M2 | 0 / 1714 / 3080 / 6 |
With the height gate off, the floor turns into a wall, the around goal becomes unreachable, and the open goal is still reachable.
ROS 2 and Nav2 (CI only). Launch test A checks that the node publishes the same 22 / 1698 / 3080 / 0 grid as the core for a valid frame, that switching to a bad CameraInfo never changes the published grid, and that a bad CameraInfo from the start leaves every cell unknown. Unit tests cover the node's frame id, width and height, and intrinsics checks. Launch test B starts the Nav2 planner_server with a static-layer-only global costmap (allow_unknown: false, tolerance: 0.05, robot_radius: 0.0). It first probes the costmap: (2.75, 0.00) is lethal (100), (2.95, 0.00) inside the box, which the camera never sees, is unknown (-1), and (5.50, -1.50) is free (0). Then it asks Navfn for three goals:
- Around
(5.50, -1.50): succeeds, every pose is in bounds, the path sampled every 0.05 m stays on free cells, and both ends are within 0.15 m of start and goal. - Shadow
(5.50, 0.00)and face(2.75, 0.00): both fail with error code 208 (no valid path).
On commit b12d114, GitHub Actions run 43086927700 reported: core pytest 16 passed, ruff clean, the hygiene scan and its trap fixture, and the authorship check in push mode; ros 42 tests collected, 42 passed, 0 skipped; web Vitest 6 passed and Playwright 6 passed; then the pages job deployed the viewer. The actor and triggering actor of that run are applycoding.
Honest notes
- Simulation only, synthetic depth. One rendered frame, no real camera, no real robot.
- A single frame marks only the visible surface. The box's front face is occupied, and the area behind it stays unknown.
- The around goal needs no detour. The best free-cell path to
(5.50, -1.50)already passes beside the box, so a planner that ignored obstacles would also reach it. The evidence that Navfn respects lethal and unknown cells comes from the costmap probes and the two negative goals. - Error 208, not 206. Navfn reports the shadow and face goals as 208 (no valid path), not 206 (goal occupied), because the planner tolerance is 0.05 and Nav2 raises 206 only when the tolerance is 0. On Nav2 1.3.13 the action result's error message is empty, so the test reads the planner diagnostics from the ROS log (
/rosout). - Nav2 version. The ROS release index lists navigation2 1.3.14-1, but on 2026-11-11 apt served 1.3.13, so CI pins
ros-jazzy-nav2-navfn-planner1.3.13-1noble.20260921.031752,ros-jazzy-nav2-planner1.3.13-1noble.20260921.031801 andros-jazzy-nav2-costmap-2d1.3.13-1noble.20260921.024946. - Height-gate mutant counts are measured. The plan's prototype predicted 117 occupied and 1208 free cells. The measured values with this ray walk are 78 occupied, 1247 free, 3080 unknown and 395 mid, and the test asserts those.
- Third-party planner. Navfn and the costmap are Nav2's own packages, installed unmodified from apt. This repo does not reimplement them.
- Test-time downloads. Playwright downloads Chrome for Testing and an FFmpeg build (LGPL-2.1-or-later) at test time only. They are not shipped in the Pages site or the repo.
- Hygiene scan limit. The CI hygiene scan is a fixed, case-insensitive pattern list over tracked files. It allows the CSS property form
cursor:so stylesheet rules pass, and it can only catch words on its list, so a manual search over the tree is still part of the checklist.
License and credits
Apache-2.0. Copyright 2026 Arpan Bom. LICENSE is byte-identical to the official Apache-2.0 text (sha256 cfc7749b96f63bd31c3c42b5c471bf756814053e847c10f3eb003417bc523d30).
Credits, as listed in NOTICE and REFERENCES.md:
- Navigation2 (Nav2) 1.3.13:
nav2_navfn_planner(Apache-2.0 and BSD-3-Clause),nav2_planner(Apache-2.0),nav2_costmap_2d(BSD-3-Clause and Apache-2.0),nav2_msgs,nav2_lifecycle_manager,nav2_utilandnav2_core - ROS 2 Jazzy:
rclpy,sensor_msgs,nav_msgs,geometry_msgs,launch,launch_ros,launch_testing,tf2_rosand related packages (Apache-2.0,tf2_rosBSD-3-Clause), in theros:jazzy-ros-baseimage - NumPy 2.5.4 (BSD-3-Clause) and Pillow 13.0.0 (MIT-CMU)
- pytest 9.2.0 and ruff 0.17.4 (MIT); TypeScript 7.0.2 and @playwright/test 1.65.0 (Apache-2.0); Vite 8.3.8, Vitest 5.1.0 and Node.js 26.13.0 (MIT)
- Ideas only, not copied: Moravec and Elfes, "High resolution maps from wide angle sonar", IEEE ICRA 1985; Thrun, Burgard and Fox, Probabilistic Robotics, MIT Press 2005, chapter 9; Amanatides and Woo, "A Fast Voxel Traversal Algorithm for Ray Tracing", Eurographics 1987; Hornung et al., "OctoMap", Autonomous Robots 2013, DOI 10.1007/s10514-012-9321-0; the Nav2 costmap obstacle and voxel layers; ROS
depth_image_proc; Hartley and Zisserman, Multiple View Geometry; ROS REP-103
How this was verified
I ran the standard portfolio-tutorial checks against commit b12d114a4cfdc73d2d64a0a4466eebd568750d34 on main, after four rounds of independent review on the pull request.
- Originality.
gh api repos/applycoding/depth-occupancy-navreports"fork": false.mainhas two commits, the planning commitb14467cand the squashed buildb12d114, both with author and committer Arpan Bom (applycoding@gmail.com) and noCo-authored-bytrailers. The only remote branch ismain. There are nothird_party,vendororexternaldirectories and no "ported from" or "adapted from" markers. - Wording. This page and the README describe the work as an original implementation of well-known methods, inspired by the credited references. Navfn and the costmap are named as third-party Nav2 packages.
- Credits.
NOTICElists the Python, ROS 2, Nav2, Node and Actions dependencies with versions and licences. The Debian versions inNOTICEmatch the CIapt-versionsartifact. Idea references are inREFERENCES.md. - License. The repo is Apache-2.0 and
LICENSEmatches the official text by SHA-256. No GPL or LGPL code is copied or linked.nav2_amcl(LGPL) and OpenCV wheels are deliberately not used. The LGPL FFmpeg build is only a Playwright test-time download. - Facts. Commands, paths and numbers on this page were checked against the repo at
b12d114. The counts above are asserted intests/test_criteria.py. Reviewers also killed injected mutants: a strict height comparison, corner cutting, a y-first tie-break, half-up rounding, one-pass updates, and removing the node's width and height check. The test counts come from run 43086927700. - Honesty labels. "Simulation only" and "synthetic depth" appear in the README, the demo title and header, the top of this page, and the honest notes.
- Live checks. The demo returned HTTP 200 with the honesty title, and its
report.jsonhasgit_shab12d114a4cfdc73d2d64a0a4466eebd568750d34andsamplefalse. This page, the repo, the demo and the CI run link returned 200. The lead image loads on this article, on the/tutorialscard and asog:image, and the code blocks have copy buttons.
Limits
This is a portfolio demo on one synthetic depth frame. It is not a mapping system for a real robot, there is no multi-frame fusion, and the height gate is a fixed band rather than a fitted ground plane. ROS and Nav2 behaviour was checked only in CI, in the pinned Jazzy container with Nav2 1.3.13. The around goal does not force a detour, so the negative goals carry the obstacle evidence. Not tested on real hardware.