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 (for small disparity changes of ) and (for larger changes), compute the aggregated cost using the SGM recurrence.
For each pixel at disparity :
where:
- is the raw matching cost at pixel for disparity
- is the aggregated cost at the previous pixel
- The subtraction of prevents values from growing unboundedly
For the first pixel (), .
Return the aggregated cost volume as a 2D list. Round values to 4 decimal places.
Example
cost_volume = [[10, 20, 30],
[15, 10, 25],
[20, 15, 10]]
P1 = 5
P2 = 10[[10, 20, 30], [15, 15, 35], [20, 15, 15]]
- We initialize the aggregated cost volume with the first row of the cost volume, since , resulting in .
- For the second pixel (), we calculate using the SGM recurrence. For example, at disparity , we have .
- We repeat this process for all pixels and disparities, using the previously computed values of to calculate the next row.
- The final aggregated cost volume is , 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