ROS 2 camera_info Stereo Baseline: Tx in P Matrix, B = -Tx/fx'

What happened when we ran it
If you put the left and right camera_info messages of a ROS 2 stereo camera side by side, you will not find a baseline field. The two messages differ in one number: the fourth entry of the projection matrix p. That entry is 0 on the left camera and a negative number on the right camera.
We tested this on ROS 2 Jazzy with two rclpy nodes. One node published left and right sensor_msgs/CameraInfo messages for a rectified stereo pair. It built the right camera's P matrix exactly as the message definition says, with Tx = -fx' * B. The second node subscribed and did every calculation from the messages it received. Our test camera had a 0.12 m baseline and a rectified focal length of 700 px. In the received right message, p[3] was −84 and p[0] was 700. -p[3] / p[0] gave 0.12 back. A point 2 m in front of the camera projected 42 px apart in the two images, and -p[3] / 42 gave 2 m.
We also ran the common mistakes on the same messages, and each one fails in a recognizable way:
- Using
Txas the baseline gives 84 instead of 0.12, a factor of fx' too large. - Dividing by the raw-image
Kfocal length instead of the rectifiedPfocal length gives 0.112 m, a quiet error of a few percent. - Reading the left
camera_infogives 0. - A calibration done in millimetres gives 120, which is 1000 times too large.
- Swapping the left and right topics gives a baseline of 0 and a negative depth.
The short answer:
B = -p[3] / p[0] # use the RIGHT (second) camera_info, both values from P
Z = -p[3] / d # d = x_left - x_right in rectified pixels, cx' equal on both sides
What we did not run: a real stereo camera, a camera driver, stereo_image_proc, image_geometry (it is not in the ros-base image), or Gazebo. The camera values are made up to keep the arithmetic easy to follow. They do not come from a real device. The library behaviour described further down comes from reading the source code, and we say so where it matters.
Environment
- Image:
ros:jazzy-ros-base, pinned asros@sha256:00b8d47a6e6384a2834e3d01fa87ae767876543baa574f2d64a75c04ea33c698 - ROS distro:
jazzy. Python 3.12.3, rclpy7.1.12-1noble.20260912.162354 - RMW:
rmw_fastrtps_cpp (default) - No network, 1 CPU, 1 GiB memory
- The same script (
script_sha256991bca04b557080cca2a7cfbeca5314618b6cdc0128fab3d4f00cfb948672646) ran 5 times. All 5 exited with code 0, wrote nothing to stderr, and took 1.41–1.7 s each. - Both nodes ran in one process on a
SingleThreadedExecutor. QoS was RELIABLE + TRANSIENT_LOCAL with depth 1. Discovery took 0.136–0.14 s. Publish-to-callback latency was 0.927–1.756 ms across all topics and runs. - Except for those timings, every output line was identical in all 5 runs.
Inputs fixed in experiment.py: 640×480 images, rectified fx' = fy' = 700 px, cx' = 320 px, cy' = 240 px, raw-image K fx = 750 px (deliberately different from fx'), design baseline 0.12 m, worked point (0.5, 0, 2.0) m in the left optical frame. The driver also published three variants: a right message calibrated in millimetres, a right message with cx' = 330, and a stereo_msgs/DisparityImage.
Predicted vs. computed from the received messages
We wrote the predictions into plan.md before the runs.
| Check | Prediction (plan.md) | Computed from received messages |
|---|---|---|
| CameraInfo fields whose name contains "base" | none | [] |
Sent p equals received p, all 12 entries |
— | true for all four CameraInfo topics; rclpy delivers p as an ndarray of length 12 |
Left p[3], p[7] / right p[3], p[7] |
0, 0 / −84, 0 | 0.0, 0.0 / −84.0, 0.0 |
| B = −p[3]/p[0] (right) | 0.12 m | 0.12 |
| Wrong: raw Tx as baseline | 84 (700×) | 84.0 (700.0×) |
| Wrong: divide by K fx = 750 | 0.112 m (about −6.7%) | 0.112 (−0.008 m, −6.6667%) |
| Wrong: read the left camera_info | 0 | 0.0 |
| Calibration in mm | 120 (1000×) | 120.0 (1000.0×) |
| Point (0.5, 0, 2.0) m: x_left / x_right | 495 / 453 px | 495.0 / 453.0 |
| Point (0.5, 0, 2.0) m: y_left / y_right | 240 / 240 px | 240.0 / 240.0 |
| Disparity and Z = −Tx/d | 42 px, 2 m | 42.0, 2.0 |
| Binning 2: fx', Tx, B | 350, −42, 0.12 m | 350.0, −42.0, 0.12 |
| Binning 2: disparity 21 px with the unbinned Tx (−84) | 4 m (wrong) | 4.0 |
| Right cx' = 330, no cx' correction | 32 px, 2.625 m | 32.0, 2.625 |
| Right cx' = 330, after subtracting (cx'_l − cx'_r) | 42 px, 2 m | 42.0, 2.0 |
| Left/right topics swapped | B 0, disparity −42 px, Z −2 m | 0.0, −42.0, −2.0 |
DisparityImage float32 t |
not exactly 0.12, depth error below a micrometre | t = 0.11999999731779099; max depth error vs float64 = 7.152557373046875e-07 m |
| Depth range from min/max disparity 1 / 128 px | 84/128 to 84/1 | 0.65625 to 83.999998 m |
image_geometry in ros-base |
not installed | ModuleNotFoundError |
Every computed value matched the prediction. Because image_geometry could not be imported, the binning and cx' rows use rules copied from its source into the script: under binning, scale the fx', cx' and Tx entries of P by 1/binning, and subtract the cx' difference from the disparity.
Raw output (run 1, selected lines)
{"metric": "p_fields", "left_p0_fx": 700.0, "left_p3_tx": 0.0, "left_p7_ty": 0.0, "right_p0_fx": 700.0, "right_p2_cx": 320.0, "right_p3_tx": -84.0, "right_p7_ty": 0.0, "right_k0_fx": 750.0, "fields_named_baseline": []}
{"metric": "baseline", "correct_minus_p3_over_p0": 0.12, "wrong_raw_tx": 84.0, "wrong_raw_over_correct": 700.0, "wrong_k_fx": 0.112, "wrong_k_fx_error_m": -0.008, "wrong_k_fx_error_pct": -6.6667, "wrong_left_info": 0.0, "mm_calibration": 120.0, "mm_over_m": 1000.0}
{"metric": "worked_point", "point_m": [0.5, 0.0, 2.0], "x_left": 495.0, "x_right": 453.0, "y_left": 240.0, "y_right": 240.0, "disparity_px": 42.0, "z_from_minus_tx_over_d": 2.0, "z_from_fx_b_over_d": 2.0, "z_true": 2.0}
{"metric": "unequal_cx", "left_cx": 320.0, "right_cx": 330.0, "raw_disparity_px": 32.0, "z_raw_no_cx_correction": 2.625, "corrected_disparity_px": 42.0, "z_cx_corrected": 2.0}
{"metric": "swapped_topics", "baseline_from_believed_right": 0.0, "baseline_from_believed_left": 0.12, "disparity_px": -42.0, "z_using_real_tx": -2.0}
{"metric": "image_geometry", "available": false, "error": "ModuleNotFoundError"}
Depth sweep
Point (0.3, 0, Z) projected through the received left and right P. The "rounding" columns give the largest error you get when the reported disparity is rounded to a step of 1 px or 1/16 px (d ± step/2, far side). The last column is the first-order estimate Z²/(−Tx) for a 1 px change.
| True Z (m) | Disparity (px) | Z if d read 1 px low (m) | Difference (m) | 1 px rounding, far side (m) | 1/16 px rounding, far side (m) | First-order Z²/(−Tx) (m) |
|---|---|---|---|---|---|---|
| 0.5 | 168.0 | 0.502994 | 0.002994 | 0.001493 | 9.3e-05 | 0.002976 |
| 1.0 | 84.0 | 1.012048 | 0.012048 | 0.005988 | 0.000372 | 0.011905 |
| 2.0 | 42.0 | 2.04878 | 0.04878 | 0.024096 | 0.001489 | 0.047619 |
| 4.0 | 21.0 | 4.2 | 0.2 | 0.097561 | 0.005961 | 0.190476 |
| 8.0 | 10.5 | 8.842105 | 0.842105 | 0.4 | 0.023881 | 0.761905 |
| 16.0 | 5.25 | 19.764706 | 3.764706 | 1.684211 | 0.095808 | 3.047619 |
| 32.0 | 2.625 | 51.692308 | 19.692308 | 7.529412 | 0.385542 | 12.190476 |
The rounding error is not symmetric. At 32 m the near-side bound for 1 px rounding is −5.12 m, against +7.529412 m on the far side.
Trial-run notes
Our first trial run measured integer-pixel disparity as round(x_l) - round(x_r). With these inputs, x often lands exactly on .5. Python's round sends .5 to the nearest even number, so at Z = 4 m disparity 21 turned into 20 and produced a 0.2 m error. That error came from the rounding rule and had nothing to do with stereo geometry, so we switched to the half-step bound shown above. The left-camera baseline also printed as -0.0 (−0.0/700), and we changed the output to print 0.0. The value is the same.
What the numbers tell you
The arithmetic is simple. The useful part is that the values survive a real ROS 2 topic: the 12 entries of p arrive bit-for-bit as sent, and baseline and depth can be recovered from them alone. The wrong answers are also far enough apart to work as a diagnosis. Zero, fx'-times, 1000-times, a few percent off and a negative depth each point to a different mistake.
The depth sweep shows why stereo depth gets worse with distance. At 2 m, reading the disparity one pixel low moves the depth by about 5 cm. At 32 m the same pixel moves it by almost 20 m. Disparity falls as 1/Z, so one pixel covers a wider depth band the farther away you look, and that band grows roughly with Z². The first-order formula in DisparityImage.msg, delta_Z = (Z^2/fT)*delta_d, follows the exact value closely up close. At 32 m it gives only about 62% of the exact one-pixel step (12.19 vs 19.69 m). Use the approximation to explain the trend, and use the exact -Tx/(d - Δd) when you need an error bound at long range.
Sub-pixel disparity helps a lot. With 1/16 px steps (the delta_d we set in the test's DisparityImage), the rounding bound is one sixteenth of the 1 px bound or smaller. Keep in mind that this is a bound on rounding only. It says nothing about how often a matcher picks the wrong pixel.
Where the baseline lives in CameraInfo
CameraInfo.msg (lines 81–105) defines the projection matrix of the rectified image as
[fx' 0 cx' Tx]
P = [ 0 fy' cy' Ty]
[ 0 0 1 0]
and says: "The first camera always has Tx = Ty = 0. For the right (second) camera of a horizontal stereo pair, Ty = 0 and Tx = -fx' * B, where B is the baseline between the cameras." The field is float64[12] p # 3x4 row-major matrix, so the entries you need are:
| Index | Symbol | Meaning |
|---|---|---|
p[0] |
fx' | focal length of the rectified image, pixels |
p[2] |
cx' | principal point x of the rectified image, pixels |
p[3] |
Tx | −fx'·B on the right camera, 0 on the left |
p[5], p[6] |
fy', cy' | focal length and principal point y |
p[7] |
Ty | 0 for a horizontal pair |
The left camera is the origin of the stereo frame. Its offset from itself is zero, so a left Tx of 0 is correct and does not mean your calibration is broken. Only the right camera carries the "B metres to the side" information, multiplied by fx' and with its sign flipped. In a normal horizontal pair, Tx is negative and B is positive.
Tx is pixels × metres, not metres
Since Tx = -fx' * B, its unit is the pixel unit of fx' times the length unit of B. In the example, 700 px × 0.12 m = 84, so Tx = −84. Treat that −84 as a distance and you are off by a factor of fx'.
Divide by the fx' from P, not by K. CameraInfo.msg says K is the "Intrinsic camera matrix for the raw (distorted) images" (lines 66–73), and that the fx', cx' of P "may differ from the values in K". Tx was multiplied by the rectified fx', so dividing by K's fx gives a baseline that looks plausible and is slightly wrong. In our test it was 0.112 m instead of 0.12 m.
The length unit of B is whatever unit the calibration used. ROS's camera_calibration takes --square as the "chessboard square size in meters" (cameracalibrator.py), and REP 103 makes the metre the ROS length unit. If the square size went in as millimetres, every downstream consumer that expects metres, such as depth images and point clouds, will be off by a factor of 1000. The equations are linear, so the unit carries straight through to DisparityImage.t ("Baseline, world units") and to depth.
From disparity to depth: Z = −Tx / d
CameraInfo.msg defines projection as [u v w]' = P * [X Y Z 1]', x = u / w, for both images of the pair. Project the same point through both matrices:
x_l = fx'·X/Z + cx'_l (left, Tx = 0)
x_r = fx'·X/Z + cx'_r + Tx/Z (right)
d = x_l - x_r = (cx'_l - cx'_r) - Tx/Z
When both cx' are equal, d = -Tx/Z = fx'·B/Z, so Z = -Tx/d. For the worked point: x_l = (700·0.5 + 320·2)/2 = 495, x_r = (700·0.5 + 320·2 − 84)/2 = 453, d = 42, Z = 84/42 = 2 m.
Disparity and Tx are both "fx' times something", so fx' cancels out. For depth you only need p[3] and the disparity. You only need to divide by fx' when you want the baseline itself in metres. The X coordinate cancels as well, which is why the sweep used X = 0.3 m without affecting the disparity. With Ty = 0 and equal fy', cy', the point lands on the same row in both images (y = 240 in the run). That is what rectification means.
When cx' differs: C++ getZ, Python get_z and DisparityImage
The equation above assumes equal cx' on both sides. With a verged pair, or with a calibration tool that does not force equal cx', you must first subtract (cx'_l − cx'_r) from the raw disparity. In our run, skipping that step turned 2 m into 2.625 m. The confusing part is that the libraries handle this differently. We read the following in the source and could not execute it, because image_geometry is not in the image we used:
- image_geometry, C++. In stereo_camera_model.hpp,
baseline()is-right_.Tx() / right_.fx()andgetZ(d)is-right_.Tx() / (d - (left().cx() - right().cx())). Both read from P. stereo_camera_model.cpp asserts that fx, fy and cy match and notes "cx may differ for verged cameras". - stereo_image_proc. stereo_processor.cpp subtracts the cx difference before it publishes the disparity, and fills
f = right.fx(),t = baseline(). DisparityImage.msg definesd = x_l - x_r - (cx_l - cx_r)andZ = fT/d. For this message, usef * t / das is and do not correct again. - image_geometry, Python.
get_zin cameramodels.py divides the right-P[0,3]by the disparity and does not subtract the cx difference. If you feed it a raw disparity from a camera with unequal cx', it computes the "no cx' correction" row of our table. With equal cx', or with an already corrected DisparityImage value, it agrees with C++.
ROS's stereo calibrator calls cv2.stereoRectify without flags (stereo_calibrator.py). OpenCV's default is CALIB_ZERO_DISPARITY, which makes both cx equal (calib3d.hpp). OpenCV also defines the fourth entry of the right projection matrix as T_x·f (lines 2076–2102). So a ROS-calibrated pair usually has equal cx'. We inferred that from the source and did not run a calibration. For a vendor-supplied camera_info, compare the two p[2] values yourself.
Two more traps: binning and Gazebo
Binning. P is defined at calibration resolution. image_geometry scales fx', cx' and Tx together when binning is on (pinhole_camera_model.cpp). The baseline does not change, because fx' and Tx shrink by the same factor. But if you measure disparity on the binned image and divide the message's original Tx by it, depth comes out off by the binning factor. In our run, binning 2 turned 2 m into 4 m.
Gazebo. In gz-sim, Sensors.cc treats the second camera on the same parent link as the right camera and sets the baseline from the difference in Y. Then gz-sensors' CameraSensor.cc writes p(3) = -fx * baseline. Going by the source, two cameras on different links can leave the right Tx at 0 too. We read this on the main branch and did not run Gazebo or check specific releases.
Check your own camera_info
Read p[0] and p[3] from the right camera's camera_info, for example with ros2 topic echo --once on that topic (topic names vary by driver). Compute B = -p[3] / p[0] and compare it with the spec sheet or your CAD drawing. If it does not match:
| What you get | Suspect first | Basis |
|---|---|---|
| B = 0 | You read the left message, or the driver or simulator left the right Tx empty | Definition (first camera Tx = 0); our left and swapped rows; Gazebo source |
| B ≈ spec × fx' | You forgot to divide by fx' | Our "raw Tx" row |
| B a few percent off | You divided by k[0] instead of p[0] |
Our "K fx" row |
| B ≈ spec × 1000 | The calibration square size was given in millimetres | Our mm row; --square is in metres |
| B negative | The driver filled Tx with the opposite sign (our inference from the definition, not tested) | Tx = -fx' * B |
| B correct, but disparity and depth are negative | Left and right image topics are swapped | Our swapped row |
Note that swapped topics did not show up as a negative baseline. The message believed to be "right" gave B = 0, and the disparity became negative. Checking B and the sign of the disparity separately narrows the cause faster.
Two helpers that do the right thing on received messages. We checked them in plain Python 3.12.3 with stand-in objects holding the example values: they return 0.12, 2.0 for a 42 px disparity, and 2.0 for the cx' = 330 case with a raw 32 px disparity. rclpy's p indexes the same way.
def stereo_baseline(right_info):
"""Baseline from the right camera_info P (calibration length unit; metres in ROS)."""
fx_rect, tx = right_info.p[0], right_info.p[3]
return -tx / fx_rect
def stereo_depth(left_info, right_info, disparity_px):
"""Depth from a raw rectified disparity (x_l - x_r). Subtracts the cx' difference first."""
d = disparity_px - (left_info.p[2] - right_info.p[2])
return -right_info.p[3] / d
If you receive a DisparityImage, its disparity is already corrected. Use msg.f * msg.t / d and skip stereo_depth. Correcting twice breaks depth on cameras with unequal cx'.
Limits of this test
- The publisher was a synthetic node that fills P by the definition. The test shows that a correctly filled message survives transport and that baseline and depth follow from it. It cannot tell you whether your driver fills Tx correctly, how accurate your calibration is, or how often your matcher picks the wrong pixel.
- The rounding columns are a model (disparity rounded to a fixed step). They are not measured noise from any matcher.
- All formulas assume a rectified horizontal pair (Ty = 0) with equal cx', or a disparity already corrected for the cx' difference. Raw, unrectified pixels need K and the distortion D (and R), not P.
- The library behaviour (C++ vs Python
image_geometry,stereo_image_proc, Gazebo) comes from reading specific development-branch commits on 2026-11-22. We did not run it, and the Jazzy binary packages may differ. - Before you feed computed depth into obstacle avoidance or grasping, check it against an object at a known distance.
Reproduce it
The script, the plan with predictions, and the raw output of all five runs are in the reproduction code directory: experiment.py, plan.md, results.json. With Docker installed, from an empty directory:
curl -fsSLO https://raw.githubusercontent.com/sungpyo9053/blog/main/experiments/ros-stereo-camera-info-p-matrix-tx-baseline-disparity-depth/experiment.py
docker run --rm --network none --cpus 1 --memory 1g -v "$PWD":/work ros@sha256:00b8d47a6e6384a2834e3d01fa87ae767876543baa574f2d64a75c04ea33c698 bash -c "source /opt/ros/jazzy/setup.bash && python3 /work/experiment.py"
The download and the image pull need a network. The container itself runs with --network none. Each line of output is one JSON object with a metric key, matching the raw lines above.
References
All checked on 2026-11-22. Commit-pinned links refer to that commit.
- sensor_msgs/CameraInfo.msg (jazzy branch); pinned: P definition, lines 81–105, K for raw images, lines 66–73
- stereo_msgs/DisparityImage.msg, lines 5–29: corrected disparity,
Z = fT/d, search range, delta_Z - image_geometry: stereo_camera_model.hpp, stereo_camera_model.cpp, cameramodels.py, pinhole_camera_model.cpp
- image_pipeline: stereo_processor.cpp, stereo_calibrator.py, cameracalibrator.py, Camera Info documentation
- OpenCV stereoRectify: P1/P2 definition, default flags
- REP 103: Standard Units of Measure and Coordinate Conventions
- Gazebo: gz-sim Sensors.cc, gz-sensors CameraSensor.cc
- Worked-example arithmetic: example.py, verification.json