Skip to content

ROSBag: parse the RealSense image footer instead of guessing it - #154

Closed
TianHengZhuang wants to merge 1 commit into
petercorke:mainfrom
TianHengZhuang:fix/realsense-image-footer
Closed

TianHengZhuang wants to merge 1 commit into
petercorke:mainfrom
TianHengZhuang:fix/realsense-image-footer

Conversation

@TianHengZhuang

Copy link
Copy Markdown
Contributor

Problem

RealSense SDK bags append four extra bytes to every image message: a little-endian float32 holding the depth scale (0.001 for 16UC1 depth). The reader currently strips four bytes from every connection as soon as one realsense_msgs connection is present, so the value is discarded instead of being exposed, and recordings whose messages are self-contained (for example Intel's published single_depth_color_640x480.bag) lose the last four bytes of every image.

Change

  • sensor_msgs Image and CompressedImage are self-describing, so the payload end is computed from the field layout and only the bytes past that boundary are treated as a footer.
  • The footer is unpacked as a little-endian float and attached to the yielded Image as depth_units.
  • Trim amounts are probed in the order 0, 4, 8 with the untrimmed buffer first, so self-contained messages are never truncated. rosbags SerdeError is retried the same way as AssertionError.
  • Connections that are not images fall back to the untrimmed buffer when a trailer was inferred from another connection.

CDR encoded messages (ROS 2 wire format inside a ROS 1 container) use the same approach with CDR alignment.

Tests

tests/create_ros_fixture.py now also writes test_ros1_realsense.bag (four byte footer on every image, 0.001 on the depth stream) and test_ros1_realsense_notrailer.bag (same topics, no footer).

TestRosBagRealSense covers topic detection, decoding of both streams, depth_units == 0.001 on depth, 0.0 on colour, and the absence of the attribute for self-contained recordings.

The rosbag tests need the ros extra and the generated fixtures. I checked the decode path of both new bags and of the existing ROS 1 and ROS 2 fixtures with rosbags directly; the full suite runs in CI.

RealSense SDK bags append a 4-byte little-endian float to every image
message, holding the depth scale.  The reader now derives the payload
boundary from the sensor_msgs layout, decodes only the message bytes and
exposes the footer value as Image.depth_units.  Recordings of the same
topics without a footer keep all their pixel data.

Adds RealSense-style ROS 1 fixtures, generated by create_ros_fixture.py,
covering both trailed and self-contained messages.

Closes petercorke#151
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 28 complexity · 2 duplication

Metric Results
Complexity 28
Duplication 2

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. Finding the end of the pixel data from the message layout is the right approach, and we have adopted it. We tested against a real RealSense recording and Intel's sample bag. We have done the work in a separate PR (#163) so that we could keep the test bags synthetic (no committed binary files) and keep line endings as LF, and your name is on the commit as co-author. We will close this as superseded once that merges. Thanks again.

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 approach to finding the end of the image data credited on the commit. Thank you again for the contribution.

@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.

2 participants