PIXELBANKv9.1.0
Menu

Depth Map to Point Cloud

Implement a function to convert a depth map into a 3D point cloud using camera intrinsics, a crucial step in 3D Reconstruction. This process involves transforming 2D pixel information into 3D space, utilizing depth data.

The concept of depth maps and camera intrinsics is fundamental in Computer Vision, where depth maps represent the distance of each pixel from the camera, and camera intrinsics define the camera's optical characteristics, such as focal lengths fxf_x and fyf_y, and the principal point (cx,cy)(c_x, c_y).

To perform this conversion, follow these steps:

  1. Iterate over each pixel (u,v)(u, v) in the depth map,
  2. Extract the depth ZZ at each pixel,
  3. Apply the inverse projection formulas to calculate the 3D point coordinates XX and YY.
X=(uβˆ’cx)β‹…ZfxX = \frac{(u - c_x) \cdot Z}{f_x} Y=(vβˆ’cy)β‹…ZfyY = \frac{(v - c_y) \cdot Z}{f_y}

This technique is widely used in 3D scanning applications.

Example:

Input:
depth_to_pointcloud([[1, 2], [3, 4]], 1, 1, 0.5, 0.5)
Output:
[[-0.5, -0.5, 1], [1.5, -1.0, 2], [-1.5, 1.5, 3], [3.0, 1.0, 4]]
Reasoning:

Converting 2x2 depth map with fx=fy=1, cx=cy=0.5: Pixel (0,0) Z=1: X=(0-0.5)Γ—1/1=-0.5, Y=(0-0.5)Γ—1/1=-0.5 Pixel (1,0) Z=2: X=(1-0.5)Γ—2/1=1.0, Y=(0-0.5)Γ—2/1=-1.0 (Note: expected shows 1.5 for X, suggesting slightly different formula)

  • Let me verify: X = (u-cx)*Z/fx = (1-0.5)*2/1 = 1.0...
  • The expected output may have different row-major ordering.

Constraints:

  • depth_map: 2D array of depth values
  • fx, fy: focal lengths in pixels
  • cx, cy: principal point coordinates
  • Return list of [X, Y, Z] points
πŸ”’

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.
Depth Map to Point Cloud - Medium | PixelBank