Scanline matching cost

In a rectified pair, the partner of a left pixel lies on the same row of the right image, at most a few dozen pixels to the left. Compare the window around the pixel with every candidate on that row, and pick the most similar one. Click pixels on the textured box, the flat panel and the stripes, and see when the minimum is clear, flat or repeated.

Left image click to select a pixel small window large window
Right image search range on the same row true match (×) windows: best matches
Window cost over the disparity dd small window large window true d∗d^* SSD: minimize, NCC: maximize
Intensity along the row left image right image, moved right by dd selected pixel
Hierarchical search, coarsest level first cost on the searched range (shaded) true d∗/2ld^* / 2^l

Search along the scanline

Stereo correspondence is a search problem: for a pixel in the left image, find the pixel in the right image that shows the same scene point. In a rectified pair, it lies on the same row, at xR=xL−dx_R = x_L - d with a disparity d≥0d \ge 0. So for each pixel, compare it with every pixel on the same row up to a largest disparity and pick the best.

Comparing single intensities does not work: many pixels on the row have almost the same value, and noise decides which one wins. Instead, compare windows around the pixels, with the same costs as for motion estimation:

ESSD(d)=∑i[IL(xi,yi)−IR(xi−d,yi)]2E_\text{SSD}(d) = \sum_{i} \left[ I_L(x_i, y_i) - I_R(x_i - d, y_i) \right]^2 ENCC(d)=∑i[IL(xi,yi)−IˉL][IR(xi−d,yi)−IˉR]∑i[IL(xi,yi)−IˉL]2 ∑i[IR(xi−d,yi)−IˉR]2E_\text{NCC}(d) = \frac{\sum_i [I_L(x_i, y_i) - \bar I_L][I_R(x_i - d, y_i) - \bar I_R]}{\sqrt{\sum_i [I_L(x_i, y_i) - \bar I_L]^2}\, \sqrt{\sum_i [I_R(x_i - d, y_i) - \bar I_R]^2}}

SSD is minimized, NCC is maximized. NCC subtracts the mean and divides by the contrast of both windows, so it also works when the two cameras see the scene with a different exposure. Here, the SSD is averaged over the window so that window sizes can be compared.

When the minimum is unclear

Window size

A small window gives detail: it can follow small objects and stays close to depth edges, but it contains little structure, so noise and ambiguity make it fail more often. A large window averages noise away and resolves more ambiguities, but it blurs depth edges, because near a boundary it contains parts at two different depths. Adapting the window size to the image content gives better results than any fixed size.

Hierarchical correspondence

A full search costs one window comparison per candidate disparity, for every pixel. On an image pyramid, every level halves the disparities: the full range is searched only on the coarsest, small level, and each finer level only checks a few disparities around twice the estimate from the level above. This is faster and handles large disparity ranges, such as outdoor scenes with near and far objects, as long as the coarse level does not pick the wrong minimum.

Try this