PIXELBANKv9.1.0
Menu

Convert pixel coordinates to a 3D ray direction in the camera frame.

Given a pixel (u, v), we want to find the 3D direction of the ray passing through that pixel. This is the inverse of projection:

r=Kβˆ’1(uv1)\mathbf{r} = K^{-1} \begin{pmatrix} u \\ v \\ 1 \end{pmatrix}

For a simplified intrinsic matrix (no skew):

  • rx=(uβˆ’cx)/fxr_x = (u - c_x) / f_x
  • ry=(vβˆ’cy)/fyr_y = (v - c_y) / f_y
  • rz=1r_z = 1

Then normalize to get a unit ray direction: r^=r/∣∣r∣∣\hat{r} = \mathbf{r} / ||\mathbf{r}||

This ray can be used for ray casting, triangulation, or depth estimation.

Example:

Input:
pixel_to_ray([[500,0,256],[0,500,256],[0,0,1]], [256, 256])
Output:
[0.0, 0.0, 1.0]
Reasoning:

For pixel at principal point (256, 256):

  1. Compute unnormalized ray: rx = (256 - 256) / 500 = 0 ry = (256 - 256) / 500 = 0 rz = 1
  2. Normalize: ||r|| = sqrt(0 + 0 + 1) = 1 Result: [0, 0, 1] (pointing straight ahead)

Constraints:

  • K: 3x3 intrinsic matrix (assume no skew, s=0)
  • pixel: [u, v] pixel coordinates
  • Return normalized ray direction [rx, ry, rz] rounded to 4 decimal places
πŸ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Pixel to 3D Ray - Medium | PixelBank