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
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
0
- The 3×3 window centered at covers all pixels in both and (indices to ).
- With , each pixel in is compared to the pixel at the same position in .
- Every corresponding pair is vs , so each absolute difference is .
- Summing all 9 differences gives , which is the final output.