# Capturing Depth image from Stereo Vision using Matlab

## Introduction

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.

## Prerequisites

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

## Code

*‘ZED_Camera.m’*, a standard Matlab file wich will call the ZED functions. The second subfolder, ‘src’, contains the files to build the application.

*‘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

##### 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 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.