Turn a camera pixel into a world ray
Compute a calibrated ray and intersect it with a known floor plane.
Camera workspace
The 3D workspace loads when it comes into view.
Raise the camera until the orange ray reaches the blue target.
Ray at 0.50 m · Target at 1.00 m
Camera 2.0 m above the floor, pixel 470, floor position 0.50 m. Blue target at 1 m. Use the camera height slider to reach the target. The translucent plane is a virtual image plane.
Opening the Python editor…
Requirements and learning goals
Before you start
- Basic Python
- Python 3.11 or 3.12 for the optional code exercise; the diagram needs only a browser
You will be able to
- Unproject a pixel
- Intersect a forward ray
- Reject parallel or backward hits
Ray casting traces a line from a camera through an image pixel into a 3D scene. The first relevant surface intersection gives a possible physical location. This is the geometric step between a box and a robot destination. Here you will calculate one floor intersection with NumPy and learn when that answer is invalid.
The warehouse project connects detections to scene geometry. A ray intersection is useful only when the camera pose and intersected surface are correct.
A pixel defines a ray
A single image point does not determine depth. With a calibrated camera, it defines a direction. A known plane, a measured depth, or a mesh intersection supplies the missing distance.
K stores the . R_world_camera rotates directions from camera axes into world axes. For an undistorted pixel p = [u,v,1], compute d_camera = inverse(K) @ p. Rotate that direction into world coordinates. The camera position is the ray origin.
Try a floor intersection
The diagram works in your browser. To run the same calculation, use Python 3.11 or 3.12 on your computer. You do not need a camera, Wendy device, or licensed simulator.
In a macOS or Linux terminal, create a project and install NumPy:
mkdir camera-ray-experiment
cd camera-ray-experiment
python3 -m venv .venv
source .venv/bin/activate
python -m pip install numpyOn Windows, create the directory and enter it, then run py -m venv .venv and .venv\Scripts\Activate.ps1 in PowerShell. Run python -m pip install numpy after activation.
Save the following code as floor_ray.py, then run python floor_ray.py. This synthetic camera is two meters above the floor, looking downward. It uses optical camera axes and a proper rotation matrix.
import numpy as np
K = np.array([[600., 0, 320], [0, 600., 240], [0, 0, 1]])
R_world_camera = np.diag([1., -1., -1.])
origin = np.array([0., 0., 2.])
pixel = np.array([470., 240., 1.])
direction = R_world_camera @ np.linalg.solve(K, pixel)
direction /= np.linalg.norm(direction)
if abs(direction[2]) < 1e-8:
raise ValueError("ray is parallel to floor")
t = -origin[2] / direction[2]
if t <= 0:
raise ValueError("floor lies behind ray")
hit = origin + t * direction
assert np.allclose(hit, [0.5, 0., 0.])
print(hit)Check the expected result
The output is the coordinate [0.5, 0, 0], printed as a NumPy array: half a meter along world x, zero along world y, and zero height above the floor. The assertion also checks this result. Change the camera origin to [0., 0., 4.] and change the assertion's expected point to [1., 0., 0.]. Keep the pixel fixed. The horizontal distance doubles because the ray angle stays the same while the floor becomes farther away.
At pixel [320., 240., 1.], the intersection is [0., 0., 0.]. Update the assertion again before running that variation. A pixel to the left of 320 produces a negative x coordinate.
Troubleshooting
If Python cannot import NumPy, activate .venv and repeat the installation command. If the file cannot be found, run the command from the directory containing floor_ray.py. An assertion error after changing the inputs means the expected point must also match your new experiment. Reset to the original values to check the baseline.
Check the assumption
The result is a floor intersection, not proof that the object touches that point. A shelf can block the ray before it reaches the floor. A raised object violates a floor-contact assumption.
The warehouse implementation instead uses a calibrated ray with geometry and rejects hits away from the floor. MuJoCo's ray API provides intersection queries; geometry selection determines what can block a ray.
Optional extension: perturb the pixel by five pixels and compare position estimates. Near-horizontal views amplify small pixel and calibration errors.
Before you move on
Can you do each of these?
- Unproject a pixel
- Intersect a forward ray
- Reject parallel or backward hits
0 of 2 checkpoints solved. Try checkpoint 1