Skip to content

fix: parse ROS image trailers and expose RealSense depth units - #155

Closed
theluckystrike wants to merge 1 commit into
petercorke:mainfrom
theluckystrike:fix/rosbag-realsense-trailer
Closed

theluckystrike wants to merge 1 commit into
petercorke:mainfrom
theluckystrike:fix/rosbag-realsense-trailer

Conversation

@theluckystrike

Copy link
Copy Markdown

Closes #151

ROSBag guessed how many trailing bytes to strip from each image message, so RealSense bags lost their depth units footer and a RealSense bag with no trailer (Intel's single_depth_color_640x480.bag layout) would have had 4 real bytes chopped off.

What changed, following the checklist in the issue

  • Image and CompressedImage are parsed by hand up to the end of the data array (header, height, width, encoding, is_bigendian, step, then the array length), for both ROS1 and CDR. Only raw[:end] goes to the type store, n == height * step is checked, and legal CDR alignment padding is accepted. The old 0/4/8 probe is kept only as a fallback for other message types.
  • RealSense style fixtures (with a 4-byte footer and with none) are generated in tests/create_ros_fixture.py, with a regression test for a bag that has StreamInfo and 0 extra bytes.
  • A 4-byte footer is kept as float32 depth_units on the sample (depth frames carry the scale, colour frames 0.0).
  • Non image connections such as StreamInfo and CameraInfo go through the fallback trailer check.
  • print in this path is replaced with logging.

Depth units API

ROSBag(..., depth_units=None | "auto" | float, invalid_is_nan=True), where depth_units is metres per native unit as in librealsense. The docstring notes that this is the reciprocal of the Open3D and TUM RGB-D depth_scale (1000 and 5000).

The scale comes from, in order, a user float, the per message footer, the bag's .../option/Depth_Units/value topic, the REP 118 encoding (16UC1 is millimetres, 32FC1 is metres), then unknown.

  • None (default) returns the native dtype unchanged with depth_units attached as metadata.
  • "auto" or a float converts to float32 metres, and raw 0 becomes NaN when invalid_is_nan is true. For 32FC1 a user float acts as a gain.

Type registration for bag connections now distinguishes ROS1 .msg from ROS2 IDL definitions, registers only missing types and skips incompatible typestore candidates during auto detection, so an unrelated or custom IDL type can't abort reading the images.

Tests

pytest -q tests/test_ros.py

301 tests pass in tests/test_ros.py and the full suite passes locally. Ruff reports no new findings in the changed files.

One related thing I left alone because it behaves the same on main. The per message handlers catch KeyError and AssertionError but not rosbags' SerdeError, so a bag with an undecodable non image message still stops iteration. I can follow up in #150 if you want that handled.

ROSBag no longer guesses how many trailing bytes to strip. Image and CompressedImage messages are parsed up to the end of their data array for ROS1 and CDR, legal CDR padding is accepted, and a 4-byte RealSense footer is decoded as depth_units instead of being dropped. ROSBag gains depth_units (None, 'auto' or a float, metres per unit) and invalid_is_nan, with the precedence described in the issue.

Closes petercorke#151
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 64 complexity · 0 duplication

Metric Results
Complexity 64
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke

Copy link
Copy Markdown
Owner

Thank you for your interest in MVTB, and for this contribution. We have adopted your idea for building synthetic RealSense bags in the tests (#163), and credited you as co-author. We have left out the type-registration changes and the topic-name depth detection, and the depth_units= conversion option will follow as its own change, so we will close this as superseded once the first PR merges. Thanks for the careful work.

petercorke added a commit that referenced this pull request Oct 22, 2026
…a guess (#163)

The Intel RealSense SDK follows every image message in a bag with 4 extra bytes
(a float32 depth scale). The reader handled that by assuming 4 trailing bytes on
every message of any bag that has a realsense_msgs connection, and only when
release="auto". Tested on real bags this had three problems:

- ROSBag(path) with the default release="ROS1_NOETIC" read no frames at all from
  the RealSense recording: SerdeError "ROS1 message size mismatch". Current
  rosbags raises SerdeError, the code only caught AssertionError.
- Intel's own sample bag (single_depth_color_640x480.bag) has realsense_msgs
  connections but no extra bytes. With release="auto" the reader removed 4 real
  bytes from every image and silently returned no frames.
- The depth scale was discarded.

Now sensor_msgs/Image and CompressedImage (ROS 1 and CDR, either byte order) are
parsed up to the end of their data array, checking that the array length is
height * step. Only those bytes are deserialized and anything after them is the
footer. Pixels are unchanged.

What to make of the footer is decided by context, because the bytes cannot
decide it: about half of all 4-byte patterns are a positive, finite float32.
In a bag recognised as RealSense (a realsense_msgs connection), a positive finite
float32 is attached to the image as `depth_units` (metres per unit) and 4 zero
bytes (a color image) mean no scale. Anything else after an image, such as the
same 4 bytes in a bag that is not recognised, another length, or a negative or
non-finite value, is ignored with a RuntimeWarning, once per topic. CDR padding
(up to 3 zero bytes) is silent.

Every other message type must be exactly the length its type says, as a
serialization format requires, and is no longer tried with 0, 4 and 8 bytes
removed. That guess was unnecessary: all 68,889 non-image messages in the 13 real
bags tested decode at their exact length.

Verified on every bag in bags/ (14 files, old reader vs new): output is
byte-identical for all except the two cases above, which now work; the RealSense
bag gives depth_units == 0.001 on its 37 depth frames and no warnings.

Adds tests/test_ros_realsense.py: the layout parser and footer decoder, synthetic
RealSense-style bags written to a temporary directory by
tests/create_ros_fixture.py (ROS 1 and CDR, with and without footers and
realsense_msgs), unexpected trailing bytes, exact-length decoding of other
messages, and checks against the real bags when they are present.

Refs #151. The depth_units=/invalid_is_nan= conversion options are not in this
change. The layout-parsing approach follows #154 and the technique for building
the synthetic bag follows #155.

Co-authored-by: TianHengZhuang <39034691+TianHengZhuang@users.noreply.github.com>
Co-authored-by: theluckystrike <51033404+theluckystrike@users.noreply.github.com>
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
@petercorke

Copy link
Copy Markdown
Owner

Closing as superseded by #163, which has now merged with your synthetic RealSense bag technique credited on the commit. The depth_units= option will follow as its own change. Thank you again for the careful work.

@petercorke petercorke closed this Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ROSBag: RealSense bags have a 4-byte depth-units trailer that is guessed, not parsed

2 participants