Capturing Depth image from Stereo Vision using Matlab


The purpose of this post is to explain how to use the ZED stereo camera and its SDK to capture depth maps with Matlab. We use the ability of Matlab to execute C++ code through an intermediate file called MEXA file.

You will find the complete source code on Stereolabs’ Github page.

If you simply want to use the ZED stereo camera with Matlab (without the ZED SDK) you can refer to this article.


You must have Matlab installed on your system and of course, the ZED SDK needs to be installed too.


When downloading the source code from GitHub, you will find two subfolders. The first one, ‘matlab’, contains a file named ‘ZED_Camera.m’, a standard Matlab file wich will call the ZED functions. The second subfolder, ‘src’, contains the files to build the application.
In the ‘mex’ subfolder, there is a file named ‘mexZED.cpp’ which contains the ZED SDK functions and interoperability with the Matlab format. This file is referred to as a MEX file. It re-implements the majority of the ZED SDK functions. This file is provided as a basic container and can be complemented with your own functions. Since Matlab does not handle image representation the same way the ZED SDK does, we need additional formatting functions.In the file ‘ZED_Camera.m’ the ZED functions are reachable through mexZED(‘..’). Check the ‘mexZED.cpp’ to see the list of functions available.

Build procedure

You now want to compile the mexZED.cpp file in a MEXA file format which can be used by Matlab. Usually, MEX files are compiled directly by Matlab but since we need to link several libraries (ZED, OpenCV) it is better to use CMake.
On Linux

Open a terminal in zed-Matlab directory and execute the following command:

export MATLAB_ROOT=/usr/local/MATLAB/R2012b
mkdir build
cd build
cmake ../src
make install
On Windows

In cmake GUI, set ‘where is the source code’ as ‘zed-matlab\src’ and ‘Where to build the binairies ‘ as ‘zed-matlab\build’.

Then be sure to specify a x64 version of Visual Studio, the ZED SDK is only compatible with x64 compiler. Click ‘Configure’, you may encounter an error saying ‘MATLAB not found…nothing will be built.’, don’t worry, you juste need to specify the variable ‘MATLAB_ROOT’ (ex : MATLAB_ROOT=C:/Program Files/MATLAB/R2014b).

Click, ‘Generate’, then go to the folder build and open ‘MEX.sln’ with visual studio. Set the Solution Configuration to Release and build mexZED, then build INSTALL.

Run the sample

You can now find the generated MEXA file in the matlab directory (e.g. ‘\zed-matlab\matlab’), you can launch Matlab and run the ZED_Camera.m file.



Read More