0
0
Computer Visionml~5 mins

Stereo vision concept in Computer Vision

Choose your learning style9 modes available
Introduction

Stereo vision helps computers see depth by using two images from slightly different views, like our eyes do.

To measure how far objects are in a scene using two cameras.
In robots to help them avoid obstacles by understanding distance.
For 3D reconstruction of environments from images.
In self-driving cars to detect nearby objects and their positions.
To create depth maps for augmented reality applications.
Syntax
Computer Vision
1. Capture two images from cameras placed side-by-side.
2. Find matching points between the two images.
3. Calculate the difference in position (disparity) of these points.
4. Use disparity to estimate depth using geometry.

The key idea is that closer objects shift more between the two images.

Disparity is inversely related to depth: bigger disparity means closer object.

Examples
This shows the main steps: capture, match, disparity, then depth.
Computer Vision
left_image = capture_camera_left()
right_image = capture_camera_right()
matches = find_feature_matches(left_image, right_image)
disparity_map = compute_disparity(matches)
depth_map = convert_disparity_to_depth(disparity_map)
Formula to convert disparity to depth using camera parameters.
Computer Vision
disparity = x_left - x_right
depth = (focal_length * baseline) / disparity
Sample Model

This code loads two images, computes the disparity map using OpenCV's StereoBM, and prints basic info about the disparity map.

Computer Vision
import numpy as np
import cv2

# Load left and right images (grayscale)
left_img = cv2.imread('left.png', cv2.IMREAD_GRAYSCALE)
right_img = cv2.imread('right.png', cv2.IMREAD_GRAYSCALE)

# Create stereo block matcher object
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)

# Compute disparity map
disparity = stereo.compute(left_img, right_img)

# Normalize disparity for display
disp_norm = cv2.normalize(disparity, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
disp_norm = np.uint8(disp_norm)

# Print some stats
print(f"Disparity map shape: {disparity.shape}")
print(f"Disparity min: {disparity.min()}")
print(f"Disparity max: {disparity.max()}")
OutputSuccess
Important Notes

Good lighting and camera calibration improve stereo vision accuracy.

Disparity maps can be noisy; smoothing or filtering helps.

Baseline is the distance between the two cameras; larger baseline improves depth accuracy but can cause matching difficulty.

Summary

Stereo vision uses two images to find depth by comparing positions of points.

Disparity is the key measure that relates to how far objects are.

It is useful in robotics, 3D mapping, and autonomous vehicles.