/Depth-Using-Stereo

Python code to estimate depth using stereo vision.

Primary LanguageJupyter Notebook

Depth estimation using Stereo Cameras

Overview

CALIBRATION

1. Feature Detection and Matching

The result is obtained using the Brute-Force matcher in OpenCV. It can be seen that there are a few wrong matches obtained. These can affect the results. To filter these wrong feature pairs, we will use RANSAC in the next step.
alt text

2. Estimation of Fundamental Matrix and RANSAC

The fundamental matrix is calculated using the 8-point algorithm. If the F matrix estimation is good, then terms x T 2 .F.x 1 should be close to 0, where x 1 and x 2 are features from image1 and image2. Using this criterion, RANSAC can be used to filter the outliers.
alt text

3. Estimation of Essential Matrix

Since we know the camera calibration matrix, we can use it to obtain the essential matrix.

4. Estimation of Camera Pose

camera pose(R and C) can be estimated using the essential matrix E. We will be estimating the pose for camera 2 with respect to camera 1 which is assumed to be of world origin. We will get four solutions for which we will use the Chirality condition to choose the correct set.

RECTIFICATION

Using the fundamental matrix and the feature points, we can obtain the epipolar lines for both images. The epipolar lines need to be parallel for further computations to obtain depth. This can be done by reprojecting image planes onto a common plane parallel to the line between camera centers.
alt text

CORRESPONDENCE

For every pixel in image 1, we try to find a corresponding match along the epipolar line in image 2. We will consider a a window of a predefined size for this purpose, so this method is called block matching. Essentially, we will be taking a small region of pixels in the left image, and searching for the closest matching region of pixels in the right. Following methods can be used for block comparison:

  1. Sum of Absolute Differences (SAD)
  2. Sum of Squared Differences (SSD)
  3. Normalized Cross-Correlation (NCC)

Depth computation

1. Disparity Map

After we get the matching pixel location, the disparity can be found by take the absolute of the difference between the source and matched pixel location

alt-text-1 alt-text-1 alt-text-1

2. Depth Map

If we know the focal length(f) and basline(b), the depth can be calculated. alt-text-1 alt-text-1 alt-text-1

How to run the code

  • Change the location to the root directory
  • Run the following command:
python3 Code/stereo.py --DataPath ./Data/Project\ 3/Dataset\ 3 --DataNumber 3 

Parameters:

  1. DataPath: Absolute path where the images are located. Be careful about the spaces.
  2. DataNumber: The dataset number. used to choose the intrinsic parameters.