Computer visionAdvanced

Semi-Global Block Matching

SGBM estimates stereo disparity by combining local matching evidence with smoothness costs accumulated along multiple image paths.

Stereo visionSGBMOpenCVDepth
Hi-C contact map framed by chromatin structure and stereo camera geometry
Generated visual worldGenomics & vision

Dense biological and visual signals resolved into structure, geometry, and interpretable layers.

Interactive model

Disparity changes with correspondence

The demo compresses the core idea: matched points shift more when the object is closer.

Live HTML simulation · adjust the controls and watch the computed output respond.

Interactive

Larger disparity means a closer object

Left camera
Right camera

Estimated depth index: 43. The exact unit depends on focal length and baseline calibration.

This is a simplified teaching model. Its displayed values are computed from the controls; the article explains where the model stops.

Site connection

The depth estimation project used OpenCV and SGBM to build stereo disparity maps from calibrated stereo images.

Correspondence First, Geometry Second

A rectified stereo pair turns correspondence into a mostly horizontal search. SGBM scores candidate disparities for each pixel, then discourages implausible disparity changes while preserving strong boundaries. Only after correspondence is estimated do calibration parameters convert disparity into metric depth.

Z=fBdZ = \frac{fB}{d}

For a rectified pair, depth ZZ is inversely proportional to disparity dd, with focal length ff expressed in pixels and baseline BB in distance units.

Rectification Defines the Search Geometry

Calibration estimates the camera matrices and lens distortion; stereo calibration also estimates the relative camera pose. Rectification warps both views so corresponding scene points lie on the same image row. SGBM can then search disparity along the horizontal axis instead of over a two-dimensional area.

The project source reports two Logitech camera views, calibration assistance, SGBM matching, parameter tuning, and disparity/depth visualization. It does not publish its final calibration matrix, baseline, image resolution, or SGBM parameter values, so this page does not infer them.

Analogy limit: sliding one transparent image over another explains horizontal disparity only after good rectification. Real cameras add distortion, exposure differences, occlusion, repeated texture, and subpixel estimates that the overlay analogy omits.

Matching Costs and Semi-Global Aggregation

For each pixel and candidate disparity, the matcher computes a local photometric cost. Pure block matching chooses from local evidence and can be unstable on weak texture. SGBM aggregates costs along several paths and adds a small penalty P1 for changing disparity by one and a larger penalty P2 for larger jumps.

The penalties express a piecewise-smooth prior, not a belief that every surface is flat. P2 must exceed P1. Larger penalties can fill noisy regions but also smear foreground boundaries; smaller penalties preserve variation but admit speckle. OpenCV's implementation exposes modes that trade path coverage, memory, and runtime.

Reference table for this concept
ParameterRoleTypical failure when mis-set
minDisparityStart of search rangeValid matches fall outside the range
numDisparitiesSearch width; OpenCV requires divisibility by 16Too small clips near objects; too large adds work and ambiguity
blockSizeLocal support windowLarge windows blur edges; tiny windows amplify noise
P1 and P2Smoothness penaltiesOver-smoothing or fragmented disparity
uniquenessRatioReject ambiguous winnersToo strict removes valid pixels; too loose accepts repeats
speckleWindowSize/rangeFilter small inconsistent regionsReal small objects may disappear

Disparity Validity and Depth Conversion

A disparity is usable only when the correspondence is credible. Occluded pixels appear in one view but not the other; uniform walls have little texture; repeating rails can produce several similar matches. Left-right consistency checks, uniqueness tests, invalid-value masks, and speckle filtering reduce false certainty but also reduce coverage.

Depth conversion requires consistent units. If focal length is in pixels, disparity must be in pixels and baseline can be in meters to produce meters. OpenCV disparity outputs may use fixed-point scaling depending on the API path, so code must inspect and convert representation before applying the formula. Near-zero disparity should be marked invalid or effectively beyond the reliable range, not divided blindly.

Worked Example

Consider an illustrative rectified rig with focal length 700 pixels and baseline 0.12 meters. If a feature appears at x=410 in the left image and x=368 in the right image, disparity is 42 pixels. The pinhole estimate is Z=(700×0.12)/42=2.0 meters.

A second match with disparity 14 pixels gives 6.0 meters. Thus a threefold smaller disparity implies a threefold larger estimated depth for the same calibrated rig. These camera values and matches are teaching numbers, not measurements reported by the project. If the 42-pixel match lies across an occlusion boundary, the arithmetic remains correct but the correspondence—and therefore the depth—is wrong.

Reference table for this concept
QuantityIllustrative valueCheck
Focal length700 pxUse rectified calibration value
Baseline0.12 mUse camera-center separation
Disparity42 pxLeft x minus corresponding right x
Estimated depth2.0 mValid only for a credible match

Evaluation and Project Interpretation

Evaluate disparity separately from metric depth. Inspect valid-pixel coverage, left-right inconsistency, boundary behavior, and error versus known geometry across distance. Then report depth error with units, robust summaries, and failure slices for texture, lighting, and range rather than only a colored map.

The portfolio source reports that the student study found no statistically significant difference between measured distances and three model outputs, with a stereo-model p-value of 12.5%, and also reports that its stereo depth map was the noisiest visually. That project-specific hypothesis test is not proof of equivalence or production accuracy; sample size, power, calibration uncertainty, residual structure, and effect sizes would be needed for stronger claims.

Common Pitfalls

  • Running correspondence on unrectified or poorly calibrated images.
  • Treating every numeric disparity as valid, especially at occlusions and blank walls.
  • Forgetting fixed-point disparity scaling before depth conversion.
  • Searching too narrow a disparity range for the nearest expected object.
  • Increasing smoothness until true object boundaries disappear.
  • Interpreting a non-significant test as proof that methods are equivalent.

Sources and Further Reading

Related Explainers