Depth Estimation with Stereo Vision Using Two Single Cameras

Depth estimation using two single cameras. Make your own stereo camera
Updated 26 Aug 2019

View License

Current stereo cameras are expensive which makes them less accessible. I decided to make my own version, rather than paying for one. I did this by purchasing two relatively very cheap cameras ($4 each). I attached the cameras onto a ridged marker using a hot glue gun. The result is what you can see, and it works reasonable well for what I spent on it. This was the proof of concept for me, if you are doing this, I recommend maybe spend a little more on the cameras to get better results.
You need a solid structure to mount the cameras onto, and once cameras are calibrated (explained as follows) they should not be moved or even touched, otherwise, you need to calibrate them again by taking images. So to make things easier make sure they are fixed well.
This code takes you through the stereo camera calibration and image processing to get essentially a depth map.
There are a few tips that may help along the line, I tried to include some of these in the code and here in this description.

Use the “SFStereo.m” file

Calibration: If you want to start with the calibration, set the “calibrationFlag” to 1, otherwise set it to 0.
The calibration starts with taking 10 images from each camera, a total of 20 images. These images are used to calibrate the cameras. You need to make a couple of folders for these images to be saved into. Make the folders somewhere on your PC and put the address in this line of code. As an example I saved them on my C drive in 2 separate folders, “cam1” for camera number 1, and “cam2” for camera number 2. The camera numbering does not matter just now.

cam1Address = 'C:\cam1';
cam2Address = 'C:\cam2';

Depending on what sort of drivers your cameras are using you may have to change this part of the code: get the adaptors and additional files to recognize your cameras from MATLAB support packages and for add-ons. On the MATLAB Home tab, in the Environment section, click Add-Ons > Get Hardware Support Packages. Check this link for further info
adaptorInfo = imaqhwinfo;
devices = imaqhwinfo('winvideo')

Once this is done, set the resolution of the cameras. Your cameras may support different resolution than the one I have used. This line of code “deviceInfo1.SupportedFormats” shows you the supported resolutions. Use the highest resolution for the beginning (it will be slower to process but more accurate).

deviceInfo = imaqhwinfo('winvideo',1);
deviceInfo = imaqhwinfo('winvideo',2);

It is quite important to take the first picture with the right camera. Try to figure out which one is the right-side camera and that should be the camera number 1. If first picture is taken by the left side camera then set the “swapCameras” to 1. Which changes the cameras around. You may have to check this each time you connect and disconnect cameras from the PC. Please see more detail about this and how that will impact the results in the MATLAB documentation that I linked here. This is an excerpt from the MATLAB documentation “If the camera used to take I1 was to the right of the camera used to take I2, then MinDisparity must be negative”. Further info on

Finally, depending on the baseline distance of your setup, and the distance of the objects to the camera, you have to tweak the “disparityRange = [-16*1 256*1]”

Needless to say, both cameras should be connected for calibration, I have included a checkerboard as well.

I was writing this as a GUI to make things easier. It’s almost done except that there is no post-processing included. I’ll share that too, it may help. I suggest using the “SFStereo.m’’.

Good luck

Cite As

Salaheddin Hosseinzadeh (2024). Depth Estimation with Stereo Vision Using Two Single Cameras (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2017b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Stereo Vision

Stereo Vision/Stereo_with_GUI_incomplete

Version Published Release Notes

Typos corrected.