PIXELBANKv9.1.0
Menu

Stereo Matching: Sum of Absolute Differences (SAD)

The Sum of Absolute Differences (SAD) measures similarity between image patches for stereo matching:

SAD = Σ |I_L(r,c) - I_R(r,c+d)|

over a W×W window centered at (r,c).

Given left and right images, center coordinates (r,c), and disparity d, calculate SAD cost. Use 3×3 window. Only count pixels where both indices are valid.

Constraints:

  • W = 3 (fixed window)
  • Image values: 0-255
  • Handle boundary cases

Examples:

| Input | Output | |-------|--------| | Same 3×3 images, r=1, c=1, d=0 | 0 | | All 0s vs all 255s, r=1, c=1, d=0 | 2295 |

Example:

Input:
I_L=[[10,10,10],[10,10,10],[10,10,10]], I_R=[[10,10,10],[10,10,10],[10,10,10]], r=1, c=1, d=0
Output:
0
Reasoning:
  • The 3×3 window centered at r=1,c=1r=1, c=1 covers all pixels in both ILI_L and IRI_R (indices (0,0)(0,0) to (2,2)(2,2)).
  • With d=0d = 0, each pixel in ILI_L is compared to the pixel at the same position in IRI_R.
  • Every corresponding pair is 1010 vs 1010, so each absolute difference is ∣10−10∣=0|10 - 10| = 0.
  • Summing all 9 differences gives SAD=0SAD = 0, which is the final output.
solution.py

Test Results

0/0
Run code to see test results.
Stereo Matching: Sum of Absolute Differences (SAD) - Medium | PixelBank