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 https://uk.mathworks.com/help/imaq/image-acquisition-support-packages-for-hardware-adaptors.html
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 https://uk.mathworks.com/help/vision/ref/disparity.html
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’’.
Salaheddin Hosseinzadeh (2023). Depth Estimation with Stereo Vision Using Two Single Cameras (https://www.mathworks.com/matlabcentral/fileexchange/72532-depth-estimation-with-stereo-vision-using-two-single-cameras), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!