PIXELBANKv9.1.0
Menu

Semi-Global Matching Cost Aggregation

Implement the cost aggregation step of Semi-Global Matching (SGM) for one scanline direction.

Given a 2D cost volume for one scanline (rows = pixels along scanline, cols = disparity levels), along with penalties P1P_1 (for small disparity changes of ±1\pm 1) and P2P_2 (for larger changes), compute the aggregated cost using the SGM recurrence.

For each pixel pp at disparity dd:

Lr(p,d)=C(p,d)+min⁡{Lr(p−1,d)Lr(p−1,d−1)+P1Lr(p−1,d+1)+P1min⁡kLr(p−1,k)+P2−min⁡kLr(p−1,k)L_r(p, d) = C(p, d) + \min\begin{cases} L_r(p-1, d) \\ L_r(p-1, d-1) + P_1 \\ L_r(p-1, d+1) + P_1 \\ \min_k L_r(p-1, k) + P_2 \end{cases} - \min_k L_r(p-1, k)

where:

  • C(p,d)C(p, d) is the raw matching cost at pixel pp for disparity dd
  • Lr(p−1,d)L_r(p-1, d) is the aggregated cost at the previous pixel
  • The subtraction of min⁡kLr(p−1,k)\min_k L_r(p-1, k) prevents values from growing unboundedly

For the first pixel (p=0p = 0), Lr(0,d)=C(0,d)L_r(0, d) = C(0, d).

Return the aggregated cost volume as a 2D list. Round values to 4 decimal places.

Example:

Input:
cost_volume = [[10, 20, 30],
               [15, 10, 25],
               [20, 15, 10]]
P1 = 5
P2 = 10
Output:
[[10, 20, 30], [15, 15, 35], [20, 15, 15]]
Reasoning:
  • We initialize the aggregated cost volume LrL_r with the first row of the cost volume, since Lr(0,d)=C(0,d)L_r(0, d) = C(0, d), resulting in Lr(0,d)=[10,20,30]L_r(0, d) = [10, 20, 30].
  • For the second pixel (p=1p = 1), we calculate Lr(1,d)L_r(1, d) using the SGM recurrence. For example, at disparity d=1d = 1, we have Lr(1,1)=C(1,1)+min⁡{Lr(0,1),Lr(0,0)+5,Lr(0,2)+5,min⁡kLr(0,k)+10}−min⁡kLr(0,k)=15+min⁡{20,10+5,30+5,10+10}−10=15+min⁡{20,15,35,20}−10=15+15−10=20L_r(1, 1) = C(1, 1) + \min\{L_r(0, 1), L_r(0, 0) + 5, L_r(0, 2) + 5, \min_k L_r(0, k) + 10\} - \min_k L_r(0, k) = 15 + \min\{20, 10 + 5, 30 + 5, 10 + 10\} - 10 = 15 + \min\{20, 15, 35, 20\} - 10 = 15 + 15 - 10 = 20.
  • We repeat this process for all pixels and disparities, using the previously computed values of LrL_r to calculate the next row.
  • The final aggregated cost volume is Lr=[[10,20,30],[15,15,35],[20,15,15]]L_r = [[10, 20, 30], [15, 15, 35], [20, 15, 15]], which after rounding to 4 decimal places (no change in this case) gives the output.

Constraints:

  • cost_volume: 2D list of floats (pixels x disparities)
  • P1: float (penalty for disparity change of +/- 1)
  • P2: float (penalty for larger disparity changes, P2 >= P1)
  • Return: 2D list of aggregated costs
  • Round to 4 decimal places
  • Use pure Python
🔒

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.
Semi-Global Matching Cost Aggregation - Hard | PixelBank