From 144f5640b524182cc290edb2771126aedec48f43 Mon Sep 17 00:00:00 2001 From: Rahul-cs50 Date: Fri, 27 Nov 2026 12:52:01 +0530 Subject: [PATCH] Account for the principal point offset when projecting stereo disparities 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 --- stereo_image_proc/CMakeLists.txt | 4 + .../stereo_image_proc/stereo_processor.hpp | 16 ++ stereo_image_proc/package.xml | 1 + .../stereo_image_proc/point_cloud_node.cpp | 3 +- .../stereo_image_proc/stereo_processor.cpp | 19 +- .../test/test_stereo_processor.cpp | 167 ++++++++++++++++++ 6 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 stereo_image_proc/test/test_stereo_processor.cpp diff --git a/stereo_image_proc/CMakeLists.txt b/stereo_image_proc/CMakeLists.txt index 69c694fc4..f2a6bdea1 100644 --- a/stereo_image_proc/CMakeLists.txt +++ b/stereo_image_proc/CMakeLists.txt @@ -40,6 +40,10 @@ if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) ament_lint_auto_find_test_dependencies() + find_package(ament_cmake_gtest REQUIRED) + ament_auto_add_gtest(test_stereo_processor test/test_stereo_processor.cpp) + target_link_libraries(test_stereo_processor ${PROJECT_NAME}) + set(_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE}") if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug") set(PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE_DEBUG}") diff --git a/stereo_image_proc/include/stereo_image_proc/stereo_processor.hpp b/stereo_image_proc/include/stereo_image_proc/stereo_processor.hpp index ce191338f..a4583ca84 100644 --- a/stereo_image_proc/include/stereo_image_proc/stereo_processor.hpp +++ b/stereo_image_proc/include/stereo_image_proc/stereo_processor.hpp @@ -42,6 +42,22 @@ namespace stereo_image_proc { +/** + * \brief Project a stereo_msgs/DisparityImage disparity image to 3d points. + * + * DisparityImage disparities are already adjusted for the x-offset between the principal + * points: d = x_l - x_r - (cx_l - cx_r). The model's reprojection matrix expects the raw + * x_l - x_r and subtracts (cx_l - cx_r) itself (in Q(3,3)), so it is applied here without + * that term, giving Z = fT/d as DisparityImage specifies. + * + * Pixels with the image's minimal disparity get Z = image_geometry::StereoCameraModel::MISSING_Z, + * as with image_geometry::StereoCameraModel::projectDisparityImageTo3d(..., true). + */ +void projectAdjustedDisparityImageTo3d( + const image_geometry::StereoCameraModel & model, + const cv::Mat & disparity, + cv::Mat & point_cloud); + struct StereoImageSet { image_proc::ImageSet left; diff --git a/stereo_image_proc/package.xml b/stereo_image_proc/package.xml index 488b18623..9ce8c15f3 100644 --- a/stereo_image_proc/package.xml +++ b/stereo_image_proc/package.xml @@ -29,6 +29,7 @@ sensor_msgs stereo_msgs + ament_cmake_gtest ament_cmake_pytest ament_lint_auto ament_lint_common diff --git a/stereo_image_proc/src/stereo_image_proc/point_cloud_node.cpp b/stereo_image_proc/src/stereo_image_proc/point_cloud_node.cpp index 63216bc6e..4c112a7e7 100644 --- a/stereo_image_proc/src/stereo_image_proc/point_cloud_node.cpp +++ b/stereo_image_proc/src/stereo_image_proc/point_cloud_node.cpp @@ -38,6 +38,7 @@ #include "message_filters/sync_policies/approximate_epsilon_time.hpp" #include "message_filters/sync_policies/exact_time.hpp" #include "rcutils/logging_macros.h" +#include "stereo_image_proc/stereo_processor.hpp" #include #include @@ -241,7 +242,7 @@ void PointCloudNode::imageCb( float * data = reinterpret_cast(const_cast(&dimage.data[0])); const cv::Mat_ dmat(dimage.height, dimage.width, data, dimage.step); - model_.projectDisparityImageTo3d(dmat, points_mat_, true); + projectAdjustedDisparityImageTo3d(model_, dmat, points_mat_); cv::Mat_ mat = points_mat_; // Fill in new PointCloud2 message (2D image-like layout) diff --git a/stereo_image_proc/src/stereo_image_proc/stereo_processor.cpp b/stereo_image_proc/src/stereo_image_proc/stereo_processor.cpp index 4c6804b51..0152880b0 100644 --- a/stereo_image_proc/src/stereo_image_proc/stereo_processor.cpp +++ b/stereo_image_proc/src/stereo_image_proc/stereo_processor.cpp @@ -27,10 +27,13 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. +#include #include #include #include +#include + #include "rcutils/logging_macros.h" #include "stereo_image_proc/stereo_processor.hpp" @@ -48,6 +51,18 @@ namespace stereo_image_proc { +void projectAdjustedDisparityImageTo3d( + const image_geometry::StereoCameraModel & model, + const cv::Mat & disparity, + cv::Mat & point_cloud) +{ + assert(model.initialized()); + // The disparity is already adjusted for cx_l - cx_r, so drop that term from Q. + cv::Matx44d Q = model.reprojectionMatrix(); + Q(3, 3) = 0.0; + cv::reprojectImageTo3D(disparity, point_cloud, Q, true); +} + bool StereoProcessor::process( const sensor_msgs::msg::Image::ConstSharedPtr & left_raw, const sensor_msgs::msg::Image::ConstSharedPtr & right_raw, @@ -160,7 +175,7 @@ void StereoProcessor::processPoints( // This is "safe" since our cv::Mat is const. float * data = reinterpret_cast(const_cast(&dimage.data[0])); const cv::Mat_ dmat(dimage.height, dimage.width, data, dimage.step); - model.projectDisparityImageTo3d(dmat, dense_points_, true); + projectAdjustedDisparityImageTo3d(model, dmat, dense_points_); // Fill in sparse point cloud message points.points.resize(0); @@ -262,7 +277,7 @@ void StereoProcessor::processPoints2( // This is "safe" since our cv::Mat is const. float * data = reinterpret_cast(const_cast(&dimage.data[0])); const cv::Mat_ dmat(dimage.height, dimage.width, data, dimage.step); - model.projectDisparityImageTo3d(dmat, dense_points_, true); + projectAdjustedDisparityImageTo3d(model, dmat, dense_points_); // Fill in sparse point cloud message points.height = dense_points_.rows; diff --git a/stereo_image_proc/test/test_stereo_processor.cpp b/stereo_image_proc/test/test_stereo_processor.cpp new file mode 100644 index 000000000..a4c5260cb --- /dev/null +++ b/stereo_image_proc/test/test_stereo_processor.cpp @@ -0,0 +1,167 @@ +// Copyright 2026 Open Source Robotics Foundation, Inc. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// * Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include + +#include + +#include "image_geometry/stereo_camera_model.hpp" +#include "stereo_image_proc/stereo_processor.hpp" + +#include +#include +#include +#include + +namespace +{ + +constexpr int kWidth = 8; +constexpr int kHeight = 6; +constexpr double kFx = 500.0; +constexpr double kFy = 520.0; +constexpr double kCxLeft = 330.0; +constexpr double kCxRight = 310.0; // principal points 20 px apart, as with verged cameras +constexpr double kCy = 240.0; +constexpr double kBaseline = 0.1; +// DisparityImage disparity, already adjusted for cx_l - cx_r. +constexpr float kDisparity = 25.0f; +constexpr float kInvalidDisparity = -1.0f; // the image's minimum, marked as missing +constexpr double kTolerance = 1e-5; + +sensor_msgs::msg::CameraInfo makeInfo(double cx, double Tx) +{ + sensor_msgs::msg::CameraInfo info; + info.width = 640; + info.height = 480; + info.k = {kFx, 0.0, cx, 0.0, kFy, kCy, 0.0, 0.0, 1.0}; + info.r = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0}; + info.p = {kFx, 0.0, cx, Tx, 0.0, kFy, kCy, 0.0, 0.0, 0.0, 1.0, 0.0}; + return info; +} + +class ProjectDisparityTest : public ::testing::Test +{ +protected: + void SetUp() override + { + ASSERT_TRUE( + model_.fromCameraInfo(makeInfo(kCxLeft, 0.0), makeInfo(kCxRight, -kFx * kBaseline))); + ASSERT_NE(model_.left().cx(), model_.right().cx()); + + disparity_.f = static_cast(model_.right().fx()); + disparity_.t = static_cast(model_.baseline()); + sensor_msgs::msg::Image & dimage = disparity_.image; + dimage.height = kHeight; + dimage.width = kWidth; + dimage.encoding = sensor_msgs::image_encodings::TYPE_32FC1; + dimage.step = kWidth * sizeof(float); + dimage.data.resize(dimage.step * kHeight); + cv::Mat_ dmat(kHeight, kWidth, reinterpret_cast(dimage.data.data())); + dmat = kDisparity; + dmat(0, 0) = kInvalidDisparity; + + color_ = cv::Mat(kHeight, kWidth, CV_8UC1, cv::Scalar(128)); + } + + // Z = fT/d, as stereo_msgs/DisparityImage specifies. + double expectedZ() const {return disparity_.f * disparity_.t / kDisparity;} + double expectedX(int u) const {return (u - kCxLeft) * expectedZ() / kFx;} + double expectedY(int v) const {return (v - kCy) * expectedZ() / kFy;} + + image_geometry::StereoCameraModel model_; + stereo_msgs::msg::DisparityImage disparity_; + cv::Mat color_; + stereo_image_proc::StereoProcessor processor_; +}; + +} // namespace + +TEST_F(ProjectDisparityTest, ProjectAdjustedDisparityImageTo3d) +{ + const cv::Mat_ dmat( + kHeight, kWidth, reinterpret_cast(disparity_.image.data.data())); + cv::Mat points; + stereo_image_proc::projectAdjustedDisparityImageTo3d(model_, dmat, points); + const cv::Mat_ pts = points; + + EXPECT_FLOAT_EQ( + pts(0, 0)[2], static_cast(image_geometry::StereoCameraModel::MISSING_Z)); + for (int v = 0; v < kHeight; ++v) { + for (int u = 0; u < kWidth; ++u) { + if (u == 0 && v == 0) { + continue; + } + EXPECT_NEAR(pts(v, u)[0], expectedX(u), kTolerance) << "u=" << u << " v=" << v; + EXPECT_NEAR(pts(v, u)[1], expectedY(v), kTolerance) << "u=" << u << " v=" << v; + EXPECT_NEAR(pts(v, u)[2], expectedZ(), kTolerance) << "u=" << u << " v=" << v; + } + } +} + +TEST_F(ProjectDisparityTest, ProcessPoints) +{ + sensor_msgs::msg::PointCloud points; + processor_.processPoints( + disparity_, color_, sensor_msgs::image_encodings::MONO8, model_, points); + + // Every pixel but the missing one; channels 1 and 2 hold the pixel's row and column. + ASSERT_EQ(points.points.size(), static_cast(kWidth * kHeight - 1)); + ASSERT_EQ(points.channels.size(), 3u); + for (size_t i = 0; i < points.points.size(); ++i) { + const int v = static_cast(points.channels[1].values[i]); + const int u = static_cast(points.channels[2].values[i]); + EXPECT_NEAR(points.points[i].x, expectedX(u), kTolerance) << "u=" << u << " v=" << v; + EXPECT_NEAR(points.points[i].y, expectedY(v), kTolerance) << "u=" << u << " v=" << v; + EXPECT_NEAR(points.points[i].z, expectedZ(), kTolerance) << "u=" << u << " v=" << v; + } +} + +TEST_F(ProjectDisparityTest, ProcessPoints2) +{ + sensor_msgs::msg::PointCloud2 points; + processor_.processPoints2( + disparity_, color_, sensor_msgs::image_encodings::MONO8, model_, points); + + ASSERT_EQ(points.height, static_cast(kHeight)); + ASSERT_EQ(points.width, static_cast(kWidth)); + sensor_msgs::PointCloud2ConstIterator it_x(points, "x"); + sensor_msgs::PointCloud2ConstIterator it_y(points, "y"); + sensor_msgs::PointCloud2ConstIterator it_z(points, "z"); + for (int v = 0; v < kHeight; ++v) { + for (int u = 0; u < kWidth; ++u, ++it_x, ++it_y, ++it_z) { + if (u == 0 && v == 0) { + EXPECT_TRUE(std::isnan(*it_z)); + continue; + } + EXPECT_NEAR(*it_x, expectedX(u), kTolerance) << "u=" << u << " v=" << v; + EXPECT_NEAR(*it_y, expectedY(v), kTolerance) << "u=" << u << " v=" << v; + EXPECT_NEAR(*it_z, expectedZ(), kTolerance) << "u=" << u << " v=" << v; + } + } +}