v9.1.1
Menu

Winner-Take-All Disparity

mediumPROStereo MatchingStereoCV: Depth Estimation

Find the disparity that minimizes matching cost along an epipolar line.

In stereo matching, we search along the horizontal scanline (after rectification) to find the best matching pixel. The winner-take-all (WTA) strategy selects the disparity with minimum cost:

d∗=arg⁡min⁡dC(x,d)d^* = \arg\min_d C(x, d)

where C(x,d)C(x, d) is the matching cost (e.g., SAD) at position xx for disparity dd.

This function implements a basic scanline stereo algorithm:

  1. For each candidate disparity dd from 0 to max_disparity
  2. Extract a block around (x,y)(x, y) in left image
  3. Extract corresponding block at (x−d,y)(x-d, y) in right image
  4. Compute SAD and track minimum

Example

Input
find_disparity([0,0,0,100,100,100,0,0], [100,100,100,0,0,0,0,0], 3, 5, 4)
Output
3
Reasoning

Searching for best disparity at x=4, block_size=3: Left block around x=4: [100, 100, 100] (indices 3,4,5)

  • Disparity 0: Right block [0, 0, 0] → SAD = 300
  • Disparity 1: Right block [100, 0, 0] → SAD = 200
  • Disparity 2: Right block [100, 100, 0] → SAD = 100
  • Disparity 3: Right block [100, 100, 100] → SAD = 0 ← minimum! Disparity 4: Right block [100, 100, 100] (would go to index 0)

Best disparity = 3 (perfect match)

Constraints

  • left_row: intensity values from left image scanline
  • right_row: intensity values from right image scanline
  • block_size: width of matching window (odd number)
  • max_disparity: maximum disparity to search
  • x: center position in left image
  • Return disparity with minimum SAD

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.