Skip to content

Account for the principal point offset when projecting stereo disparities - #1216

Open
Rahul-cs50 wants to merge 2 commits into
ros-perception:rollingfrom
Rahul-cs50:fix/stereo-point-cloud-principal-point-offset
Open

Rahul-cs50 wants to merge 2 commits into
ros-perception:rollingfrom
Rahul-cs50:fix/stereo-point-cloud-principal-point-offset

Conversation

@Rahul-cs50

Copy link
Copy Markdown
Contributor

When the two rectified cameras have different principal points (cx_l != cx_r), point_cloud_node and StereoProcessor::processPoints/processPoints2 put every point at the wrong depth, and can put it behind the camera.

stereo_msgs/DisparityImage disparities are pre-adjusted for that offset, d = x_l - x_r - (cx_l - cx_r) with Z = fT/d, and StereoProcessor::processDisparity publishes exactly that. The three consumers then project the image with StereoCameraModel::projectDisparityImageTo3d(), which is cv::reprojectImageTo3D() with the model's Q. Q expects the raw x_l - x_r and carries the offset in Q(3,3) = fy * (cx_l - cx_r) (active since ros-perception/vision_opencv@833f005b, 2012), so the offset is subtracted a second time and the cloud gets Z = fT / (d - (cx_l - cx_r)).

For example, a textured plane 2.0 m away, seen by a pair with f = 300, B = 0.12 m and cx_l - cx_r = 20 px, comes out of the matcher disparity_node uses (StereoProcessor::processDisparity) with d = 18 (the right value, fT/Z), and the cloud puts it at Z = -18 m.

ROS camera_calibration rectifies with CALIB_ZERO_DISPARITY (OpenCV's default for stereoRectify), which makes cx_l == cx_r, so its calibrations aren't affected. Rectifying with flags=0, as some other tools do, keeps the offset.

This was found before in #349 (2018), which proposed dropping the pre-adjustment in processDisparity. That PR was closed in 2022 because it targeted Indigo. This PR fixes the consumers instead, so the published disparity keeps the meaning DisparityImage.msg documents, which other producers such as depth_image_proc's disparity node follow too.

Change

A new stereo_image_proc::projectAdjustedDisparityImageTo3d() projects with a copy of the model's Q that has Q(3,3) = 0, since the offset is already in the disparity. This is the same as adding cx_l - cx_r back to each disparity before calling projectDisparityImageTo3d(). Missing values are handled as before (handleMissingValues = true, MISSING_Z). point_cloud_node, processPoints and processPoints2 use it.

When cx_l == cx_r, Q(3,3) was already 0, so the output is unchanged. The output does change for anyone who feeds point_cloud_node raw x_l - x_r disparities from a camera pair with cx_l != cx_r. Such disparities don't follow DisparityImage.msg, and the old double subtraction happened to give them the right depth.

Not changed here: processDisparity still fills min_disparity/max_disparity with the raw search range, while the image holds adjusted values. I can open a separate PR for that if you'd like.

Test

New gtest test/test_stereo_processor.cpp. It builds a StereoCameraModel with cx_l - cx_r = 20 px, fx != fy, and a constant adjusted disparity of 25 px with one missing pixel. It checks X, Y and Z of every point from projectAdjustedDisparityImageTo3d(), processPoints() and processPoints2() against Z = fT/d (2.0 m), and checks that the missing pixel stays missing.

In a ros:rolling-ros-base container (image_geometry 4.1.0, OpenCV 4.10):

  • with this change, the 3 tests pass, and so do cpplint, uncrustify, cppcheck (cppcheck 2.19 is skipped by default, so I ran it with AMENT_CPPCHECK_ALLOW_SLOW_VERSIONS=1), lint_cmake, xmllint and copyright;
  • without the source change, processPoints and processPoints2 fail with Z = 10.0 m (fT / (d - 20)).
  • I also ran synthetic left/right images of a plane through the real matcher (processDisparity, then processPoints2). With this change the cloud's median Z is 2.000 m, the true depth. Without it, the median is -18.0 m.

test_disparity_node and test_point_cloud_node time out in my container both with and without this change, so I couldn't use them here.

…ties

DisparityImage disparities are already adjusted for cx_l - cx_r, but
point_cloud_node, processPoints and processPoints2 projected them with
the camera model's Q, whose Q(3,3) subtracts that offset again. With
cx_l != cx_r every point got the wrong depth, or landed behind the
camera. Project with Q(3,3) = 0 instead, so Z = fT/d as the message
specifies.

Signed-off-by: Rahul-cs50 <rahulseervi.dev@gmail.com>
@mergify

mergify Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

…loud-principal-point-offset

# Conflicts:
#	stereo_image_proc/include/stereo_image_proc/stereo_processor.hpp
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.

1 participant