Overview
Stereo vision uses two cameras to compute depth information by triangulation. OpenCV provides functions for:- Calibrating stereo camera systems
- Computing rectification transformations
- Stereo correspondence algorithms (StereoBM, StereoSGBM)
- 3D reconstruction from disparity maps
Stereo Calibration
stereoCalibrate
Calibrates a stereo camera setup by finding intrinsic parameters for each camera and extrinsic parameters between them.Vector of vectors of calibration pattern points. Both cameras need to see the same object points. Structure same as calibrateCamera.
Vector of vectors of projections of calibration pattern points observed by the first camera.
Vector of vectors of projections of calibration pattern points observed by the second camera.
Input/output camera intrinsic matrix for the first camera.
Input/output vector of distortion coefficients for the first camera.
Input/output camera intrinsic matrix for the second camera.
Input/output vector of distortion coefficients for the second camera.
Size of the image used only to initialize camera intrinsic matrices.
Output rotation matrix between the first and second camera coordinate systems. This matrix brings points from the first camera’s coordinate system to the second camera’s coordinate system.
Output translation vector between the coordinate systems of the cameras. Equivalent to the position of the first camera with respect to the second camera.
Output essential matrix.
Output fundamental matrix.
Different flags for stereo calibration (see Stereo Calibration Flags below).
Termination criteria for the iterative optimization algorithm.
[T]_x is the skew-symmetric matrix of T.
And the fundamental matrix F:
Extended Version
Output vector of rotation vectors (Rodrigues) estimated for each pattern view in the coordinate system of the first camera.
Output vector of translation vectors estimated for each pattern view.
Output vector of the RMS re-projection error estimated for each pattern view.
Stereo Calibration Flags
Flags control the calibration behavior (in addition to single camera flags):| Flag | Value | Description |
|---|---|---|
CALIB_FIX_INTRINSIC | 0x00100 | Fix cameraMatrix1/2 and distCoeffs1/2 so that only R, T, E, F are estimated |
CALIB_USE_INTRINSIC_GUESS | 0x00001 | Optimize some or all intrinsic parameters according to specified flags |
CALIB_USE_EXTRINSIC_GUESS | 1 << 22 | R and T contain valid initial values that are optimized further |
CALIB_FIX_PRINCIPAL_POINT | 0x00004 | Fix principal points during optimization |
CALIB_FIX_FOCAL_LENGTH | 0x00010 | Fix fx and fy for both cameras |
CALIB_FIX_ASPECT_RATIO | 0x00002 | Optimize fy, fix ratio fx/fy |
CALIB_SAME_FOCAL_LENGTH | 0x00200 | Enforce fx^(0) = fx^(1) and fy^(0) = fy^(1) |
CALIB_ZERO_TANGENT_DIST | 0x00008 | Set tangential distortion coefficients to zero for each camera |
CALIB_FIX_K1 … CALIB_FIX_K6 | Various | Do not change corresponding radial distortion coefficient |
CALIB_RATIONAL_MODEL | 0x04000 | Enable k4, k5, k6 coefficients (8 coefficients total) |
CALIB_THIN_PRISM_MODEL | 0x08000 | Enable s1, s2, s3, s4 coefficients (12 coefficients total) |
CALIB_FIX_S1_S2_S3_S4 | 0x10000 | Thin prism distortion coefficients are not changed |
CALIB_TILTED_MODEL | 0x40000 | Enable tauX and tauY coefficients (14 coefficients) |
CALIB_FIX_TAUX_TAUY | 0x80000 | Tilted sensor model coefficients are not changed |
It’s usually reasonable to restrict some parameters, e.g., pass CALIB_SAME_FOCAL_LENGTH and CALIB_ZERO_TANGENT_DIST flags.
Stereo Rectification
stereoRectify
Computes rectification transforms for each head of a calibrated stereo camera.First camera intrinsic matrix.
First camera distortion parameters.
Second camera intrinsic matrix.
Second camera distortion parameters.
Size of the image used for stereo calibration.
Rotation matrix from the coordinate system of the first camera to the second camera (from stereoCalibrate).
Translation vector from the coordinate system of the first camera to the second camera (from stereoCalibrate).
Output 3x3 rectification transform (rotation matrix) for the first camera. Performs change of basis from unrectified to rectified first camera’s coordinate system.
Output 3x3 rectification transform (rotation matrix) for the second camera.
Output 3x4 projection matrix in the new (rectified) coordinate systems for the first camera. Projects points given in the rectified first camera coordinate system into the rectified first camera’s image.
Output 3x4 projection matrix in the new (rectified) coordinate systems for the second camera.
Output 4x4 disparity-to-depth mapping matrix (see reprojectImageTo3D).
Operation flags:
CALIB_ZERO_DISPARITY(0x00400): Makes principal points of each camera have the same pixel coordinates in rectified views
Free scaling parameter between 0 and 1:
alpha=0: Rectified images are zoomed and shifted so only valid pixels are visible (no black areas)alpha=1: Rectified images are decimated and shifted so all pixels from original images are retained-1: Default scaling
New image resolution after rectification. When (0,0), it’s set to the original imageSize. Setting to larger value helps preserve details.
Optional output rectangle inside the rectified first image where all pixels are valid.
Optional output rectangle inside the rectified second image where all pixels are valid.
The first three columns of P1 and P2 are the new “rectified” camera matrices. Pass these with R1 and R2 to initUndistortRectifyMap to initialize rectification maps.
stereoRectifyUncalibrated
Computes a rectification transform for an uncalibrated stereo camera.Array of feature points in the first image.
Corresponding points in the second image.
Input fundamental matrix. Can be computed from the same point pairs using findFundamentalMat.
Size of the image.
Output rectification homography matrix for the first image.
Output rectification homography matrix for the second image.
Optional threshold to filter outliers. If >0, point pairs not complying with epipolar geometry are rejected. Otherwise all points are considered inliers.
Utility Functions
getOptimalNewCameraMatrix
Returns the new camera intrinsic matrix based on the free scaling parameter.Input camera intrinsic matrix.
Input vector of distortion coefficients. If NULL/empty, zero distortion is assumed.
Original image size.
Free scaling parameter between 0 (only valid pixels) and 1 (retain all source pixels). See stereoRectify for details.
Image size after rectification. By default, set to imageSize.
Optional output rectangle outlining all-good-pixels region in undistorted image.
Optional flag indicating whether the principal point should be at image center or chosen to best fit source image (determined by alpha).
rectify3Collinear
Computes rectification transforms for 3-head camera where all heads are on the same line.Stereo Matching Classes
StereoBM
Class for computing stereo correspondence using the block matching algorithm.Maximum disparity minus minimum disparity. Must be divisible by 16. Typical value: 16, 32, 48, 64, etc.
Matched block size. Must be odd number ≥1. Typical values: 5-21. Larger blocks produce smoother but less detailed disparity maps.
Type of the prefilter:
PREFILTER_NORMALIZED_RESPONSE: Normalized responsePREFILTER_XSOBEL: Sobel prefilter
Prefilter window size (5-255, must be odd).
Truncation value for prefiltered image pixels (1-63).
Minimum texture for disparity computation. Areas with low texture are filtered out.
Margin in percentage by which best computed cost function value should “win” second best value. Typically 5-15.
StereoSGBM
Class for computing stereo correspondence using Semi-Global Block Matching algorithm.Minimum possible disparity value. Typically 0, but can be adjusted.
Maximum disparity minus minimum disparity. Must be divisible by 16. Values: 16, 32, 48, 64, 96, 128, etc.
Matched block size. Must be odd number ≥1. Values: 3, 5, 7, etc. SGBM works well with smaller blocks than BM.
First parameter controlling disparity smoothness. Penalty for disparity change by ±1. If 0, default is
8 * channels * blockSize^2.Second parameter controlling disparity smoothness. Penalty for disparity change by more than 1. If 0, default is
32 * channels * blockSize^2. P2 > P1.Maximum allowed difference in left-right disparity check. Set to negative value to disable check.
Truncation value for prefiltered image pixels. Default: 63.
Margin by which best cost function value should “win” second best. Typically 5-15.
Maximum size of smooth disparity regions to consider noise speckles and invalidate. Set to 0 to disable. Typical: 50-200.
Maximum disparity variation within connected component. Typical: 1-2.
Algorithm mode:
MODE_SGBM: Standard Semi-Global Block MatchingMODE_HH: Hirschmuller algorithmMODE_SGBM_3WAY: Modified SGBMMODE_HH4: Full-scale two-pass algorithm
SGBM is more suitable for real-time applications and produces better results than BM, especially in textured regions. Consider using MODE_SGBM_3WAY or MODE_HH4 for best quality.
See Also
- Camera Calibration - calibrateCamera for obtaining camera intrinsics
- Pose Estimation - solvePnP for 3D-2D correspondences
- OpenCV samples:
stereo_calib.cpp,stereo_match.cpp
