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 fxβ and fyβ, and the principal point (cxβ,cyβ).
To perform this conversion, follow these steps:
- Iterate over each pixel (u,v) in the depth map,
- Extract the depth Z at each pixel,
- Apply the inverse projection formulas to calculate the 3D point coordinates X and Y.
This technique is widely used in 3D scanning applications.
Example:
depth_to_pointcloud([[1, 2], [3, 4]], 1, 1, 0.5, 0.5)
[[-0.5, -0.5, 1], [1.5, -1.0, 2], [-1.5, 1.5, 3], [3.0, 1.0, 4]]
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
You are given exactly the key formulae in the problem statement; the task is to understand and implement them correctly over the whole image.
1. Background Knowledge (Key Concepts)
A pinhole camera model describes how 3D points in camera coordinates (X,Y,Z) project to 2D image pixels (u,v). With intrinsics matrix
K=βfxβ00β0fyβ0βcxβcyβ1ββ,a 3D point projects as:
u=ZfxβXβ+cxβ,v=ZfyβYβ+cyβ.A depth map gives, for each pixel (u,v), the distance along the cameraβs viewing ray to the observed 3D surface, usually represented as depth Z in the camera coordinate system.
βUnprojectionβ or back-projection is just inverting this mapping: given a pixel (u,v), its depth Z, and intrinsics (fxβ,fyβ,cxβ,cyβ), we solve the above equations for X and Y. That yields:
X=fxβ(uβcxβ)β Zβ,Y=fyβ(vβcyβ)β Zβ.Computing this for all pixels with valid depth produces a 3D point cloud in the camera coordinate frame.
2. Algorithm / Approach Pattern
At a high level:
- Iterate over all pixels in the depth map.
- For each pixel with valid depth:
- Use the camera intrinsics to unproject the pixel into a 3D point in camera coordinates.
- Collect all such 3D points into a point cloud array, e.g. shape (N,3) or (H,W,3).
This is a simple per-pixel transformation problem with a fixed mathematical formula.
3. Step-by-Step Strategy
- Inputs & dimensions
- Let the depth image have shape (H,W).
- Intrinsics: fxβ,fyβ,cxβ,cyβ.
- Coordinate convention
- Assume:
- u is the column index (x-coordinate in image), 0β€u<W.
- v is the row index (y-coordinate in image), 0β€v<H.
- Check whether the problem or starter code uses this same convention.
- Loop or vectorize over pixels
- For each pixel (v,u):
- Read depth Z=\text{depth}[v,u].
- If Z is invalid (e.g., 0, negative, or NaN), skip it or handle as required by the problem.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.