v9.1.1
Menu

Block Matching Disparity

hardPROStereo MatchingCVCV: Depth Estimation

Implement block matching for stereo disparity estimation, a fundamental technique in stereo vision. This task involves finding the corresponding points between two images of the same scene taken from different viewpoints.

The concept of disparity is crucial here, as it represents the difference in the position of a point between the left and right images, given by xleft−xrightx_{left} - x_{right}. This disparity is inversely proportional to the depth of the point in the scene.

  1. For each pixel in the left image, find the best matching block in the right image along the epipolar line, which for rectified images, is the same row.
  2. Calculate the disparity for each pixel based on the position of the best match.

The matching cost is the sum of squared differences (SSD) between the b×bb \times b block centred at (y,x)(y, x) in the left image and the block centred at (y,x−d)(y, x - d) in the right image, for each candidate d=0,1,…,max_disp−1d = 0, 1, \dots, \text{max\_disp} - 1. The disparity is the dd with the smallest SSD (the smallest dd wins a tie).

Pixels within ⌊b/2⌋\lfloor b/2 \rfloor of the top, bottom or right edge, and pixels with x<⌊b/2⌋+max_dispx < \lfloor b/2 \rfloor + \text{max\_disp}, are left at disparity 00.

d=xleft−xrightd = x_{left} - x_{right}

This technique is widely used in depth estimation and 3D reconstruction applications.

Example

Input
Stereo pair
Output
Disparity map
Reasoning
  • Inputs: left and right are a rectified grayscale pair, block_size is the square patch side bb, and max_disp counts the candidate shifts d∈{0,…,max_disp−1}d \in \{0, \dots, \text{max\_disp}-1\}.
  • Worked example: a 3×83 \times 8 pair with every row [10,20,80,30,60,40,90,50][10, 20, 80, 30, 60, 40, 90, 50] on the left and [80,30,60,40,90,50,10,20][80, 30, 60, 40, 90, 50, 10, 20] on the right (scene shifted 2 pixels left), b=3b = 3, 3 shifts.
  • At the centre-row pixel in column 5, the left block rows are [60,40,90][60, 40, 90]; compare with the right block centred on column 5−d5 - d:
    • d=0d=0: 3 (302+102+802)=222003\,(30^2 + 10^2 + 80^2) = 22200
    • d=1d=1: 3 (202+502+402)=135003\,(20^2 + 50^2 + 40^2) = 13500
    • d=2d=2: 00, a perfect match, so the disparity is 22.
  • Output: a disparity map the input's shape. Columns 4 to 6 of the middle row get 22; columns 0 to 3 stay 00 because x<⌊3/2⌋+3=4x < \lfloor 3/2 \rfloor + 3 = 4 (the description's rule); column 7 and rows 0 and 2 stay 00 because the 3×33 \times 3 block would cross the right, top or bottom edge.
  • Notice: larger disparity means a closer object.

Constraints

  • Input images: 2D numpy arrays (grayscale) with shape (height, width) and pixel values in [0, 255]
  • Block size: odd integer between 3 and 31
  • Disparity range: integer values between 0 and width // 2
  • Output disparity map: 2D numpy array with shape (height, width) and dtype uint16
  • The input images are assumed to be rectified, with the epipolar line being the same row for corresponding pixels

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.