v9.1.1
Menu

Plane Sweep Stereo

hardPROMulti-view StereoCVCV: Depth Estimation

Implement a plane sweep stereo algorithm for multi-view stereo, a technique used in depth estimation to calculate the depth of a scene from multiple images. This task involves sweeping a series of depth planes through the scene to estimate the depth at each point.

The concept of plane sweep stereo relies on the idea of homography, which describes the transformation between two images of the same planar scene. For a given depth plane, the homography matrix HH can be used to warp all images to a reference view. The photo-consistency of the warped images is then computed, typically using the variance of the pixel values.

Inputs

  • images: a list of NN images of the same size; the first one is the reference view.
  • poses: the NN camera pose matrices TiT_i (4×44 \times 4); the first one belongs to the reference view.
  • K: the 3×33 \times 3 intrinsic matrix shared by all views.
  • depths: the candidate plane depths dd to sweep.

For view ii, write the relative pose T0−1TiT_0^{-1} T_i as a rotation RR and translation tt, and use the fronto-parallel plane normal n=(0,0,1)⊤n = (0, 0, 1)^\top. The plane-induced homography at depth dd is

Hi(d)=K(R+t n⊤d)K−1H_i(d) = K \left( R + \frac{t\, n^\top}{d} \right) K^{-1}

The warped view ii at reference pixel (x,y)(x, y) takes the pixel of view ii at Hi(d) (x,y,1)⊤H_i(d)\,(x, y, 1)^\top after dividing by the third coordinate and truncating to integer pixel coordinates. Locations that fall outside the image read 0. The reference image is used as is.

Here are the general steps to follow:

  1. Define a series of depth planes to sweep through the scene
  2. For each depth plane, compute the homography matrix HH and warp all images to the reference view
  3. Compute the photo-consistency of the warped images: the per-pixel variance across the NN images
  4. For every pixel, return the depth whose variance is smallest (the first such depth on ties)
σ2=1N∑i=1N(Ii−Iˉ)2\sigma^2 = \frac{1}{N} \sum_{i=1}^{N} (I_i - \bar{I})^2

This technique is widely used in computer vision applications, such as 3D reconstruction and robotics.

Example

Input
Multiple images and poses
Output
Depth map
Reasoning
  • Inputs (illustrative): two 1×61 \times 6 grayscale views, reference (10,20,30,40,50,60)(10, 20, 30, 40, 50, 60) and second view (0,0,10,20,30,40)(0, 0, 10, 20, 30, 40). Focal length f=100f = 100, principal point 0, second camera R=IR = I, t=(0.04,0,0)t = (0.04, 0, 0), depths 1,2,41, 2, 4.
  • Homography: with R=IR = I and n=(0,0,1)n = (0,0,1), HH is a horizontal shift of f tx/df\,t_x / d: 44, 22, 11 pixels for d=1,2,4d = 1, 2, 4.
  • Cost at d=2d = 2: the warped view is (10,20,30,40,0,0)(10, 20, 30, 40, 0, 0), since the last two samples fall outside the image. With two views σ2=(I0−I12)2\sigma^2 = \left(\tfrac{I_0 - I_1}{2}\right)^2, giving (0,0,0,0,625,900)(0, 0, 0, 0, 625, 900).
  • Other planes: d=1d = 1 gives (100,100,225,400,625,900)(100, 100, 225, 400, 625, 900); d=4d = 4 gives (25,25,25,25,25,900)(25, 25, 25, 25, 25, 900).
  • Output: each pixel takes its lowest-variance depth, so the depth map is (2,2,2,2,4,1)(2, 2, 2, 2, 4, 1), same height and width as the images.
  • Notice: only interior pixels recover the true depth 2. Pixel 6 ties at 900 everywhere, so the first depth wins; pixel 5 drifts to 4 because its match left the image.

Constraints

  • Input images: 3D numpy arrays (color or grayscale) with shape (height, width, channels) and pixel values in [0, 255]
  • Input poses: 2D numpy arrays with shape (num_views, 4, 4) representing the camera transformation matrices
  • Valid depth range: [0, 100] meters, with a minimum and maximum depth plane distance of 0.1 and 10 meters respectively
  • Output depth map: 2D numpy array with shape (height, width) and pixel values representing the estimated depth in meters, returned as float32 numpy array
  • Assumption: All input images are already undistorted and have the same intrinsic camera parameters

Learn this in

More from CV: Depth Estimation

PRO

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

Sign in to run your code against the test cases.