This code implements a stereo vision pipeline to calculate the depth of an object in an image.
This code requires the following Python libraries:
- OpenCV
- NumPy
- Matplotlib
- Clone this repository.
- Navigate to the project directory in your terminal.
- Install the required libraries using the following command:
pip install opencv-python numpy matplotlib
- Run the script using the following command:
python stereoDepth.py
The code performs the following steps:
- Reads the stereo image pair: It reads the left and right images from a specified file path.
- Displays the stereo image pair: It displays the left and right images side-by-side using matplotlib.
- Reads the calibration matrices: It reads the pre-computed camera calibration matrices for the left and right cameras.
- Computes the disparity map: It computes the disparity map between the left and right images using the SGBM algorithm from OpenCV.
- Decomposes the projection matrices: It decomposes the projection matrices to obtain the intrinsic and extrinsic camera parameters.
- Calculates the depth map: It calculates the depth map from the disparity map, camera calibration parameters, and baseline distance between the cameras.
- Reads the obstacle image: It reads a reference image of the obstacle to be detected.
- Locates the obstacle in the image: It uses template matching to locate the obstacle in the left image.
- Calculates the nearest point: It calculates the closest point on the obstacle to the camera based on the depth map and obstacle location.
- Displays the results: It displays the depth map, bounding box around the detected obstacle, and the depth of the nearest point on the obstacle.
stereoDepth.py
: This is the main script that executes the stereo vision pipeline.files_management.py
: This file contains functions to read the images and calibration matrices.algo_defs.py
: This file contains functions to compute the disparity map, decompose the projection matrices, calculate the depth map, locate the obstacle in the image, and calculate the nearest point.