Using the ZED Stereo Camera with Matlab

Note: This is for ZED SDK 1.2 only. Please see the latest Matlab guide here.

Introduction

This tutorial will explain how to use the ZED 3D camera with Matlab. You will learn how to capture images from the ZED and adjust camera parameters in Matlab. Note that viewing and manipulating depth map data is not covered in this tutorial.

NOTE : when using directly the ZED camera (as a webcam), the images are not rectified.

For capturing rectified images and depth maps in Matlab with the ZED, please refer to “How to use the ZED SDK with Matlab”

Prerequisites

You must have a MathWorks account (free) and Matlab installed on your system. Of course, the ZED SDK has to be installed as well. Even if you’re new to Matlab, this tutorial should be easy to follow.

Getting started

Open Matlab and in the Command Window, enter this command:

>> webcamlist

You should encounter an error if you’ve never used Matlab with a webcam before:

Error using webcamlist (line 20)

MATLAB Support Package for Webcams has not been installed. Open Support Package Installer to install the Webcam Support Package.

If you’ve encountered the error, click the link in the error log. A dialog window appears:

Tuto1_6

Click Next > and Log In. Matlab will ask for your MathWorks account credential. Log in.

Then, read and accept the MathWorks auxiliary software license agreement. Click Next > and Install. This might take a while (around 10-15 seconds).

When the setup process is finished, uncheck the “Show support package examples” check box and click on Finish.

Now, in the Matlab Command Window, re-enter:

>> webcamlist

and that should give you:

ans ='ZED'

Perfect! The ZED camera is properly detected. 🙂

Now grab an instance of the ZED camera with the following command line:

>> cam = webcam

and the cam variable will return the current parameters of the camera:

cam =

webcam with properties:

Name: 'ZED'
Resolution: '2560x720'
AvailableResolutions: {1x4 cell}
WhiteBalanceMode: 'auto'
Sharpness: 4
Saturation: 5
Hue: 0
Gain: 4
WhiteBalance: 4600
Contrast: 4
Brightness: 4
Exposure: 2
ExposureMode: 'auto'

Note that you can check whether the ZED camera works properly by using this command:

>> preview(cam)

You can now grab ZED frames with:

>> img = snapshot(cam);

You’ve just grabbed your first ZED frame in Matlab. If you need real-time video capture, set up a loop. Fairly straightforward, isn’t it ? 😛

The ZED Camera will stay active as long as you keep the cam variable in Matlab’s environment. To turn off the ZED camera, use this command:

>> clear cam

Here is a snippet that demonstrate how to open the ZED , grab the side by side images and split them :

clear all;close all;clc;
 
% get access to the ZED camera
zed = webcam('ZED')
% set the desired resolution
zed.Resolution = zed.AvailableResolutions{1};
% get the image size
[height width channels] = size(snapshot(zed))
 
% Create Figure and wait for keyboard interruption to quit
f = figure('keypressfcn','close','windowstyle','modal');
ok = 1;
% loop over frames
while ok
    %capture the current image
    img = snapshot(zed);
     
    % split the side by side image image into two images
    im_Left = img(:, 1 : width/2, :);
    im_Right = img(:, width/2 +1: width, :);
     
    % display the left and right images
    subplot(1,2,1);
    imshow(im_Left);
    title('Image Left');
    subplot(1,2,2);
    imshow(im_Right);
    title('Image Right');
     
    drawnow; %this checks for interrupts
    ok = ishandle(f); %does the figure still exist
end
 
% close the camera instance
clear cam

ZEDMatlab

  • Kishan Reddy

    I have never used a webcam with MATLAB, so my question is is it possible to save the video output and image output obtained in the MATLAB window separately as a video file??

    • Hi,
      Yes you can save a video directly from Matlab by using the class VideoWriter : http://fr.mathworks.com/help/matlab/ref/videowriter.html.

      in the previous snippet you just need to add the following couple of lines before the loop :
      v = VideoWriter(‘newfile.avi’);
      open(v)

      In the loop, you have to provide the image to record :
      writeVideo(v,im_Right)

      and at the end, close the videoWriter:
      close(v)

  • ahmad seyfi

    Hi,
    I am using Matlab R2016a on Ubuntu 14.04 and when I type the command “cam = webcam(‘ZED’)”, I get this the following output and the only available resolution is 4416×1242. Would you please help me fix this issue?

    Thank You

    >
    cam=webcam
    cam =
    webcam with properties:
    Name: ‘ZED’
    Resolution: ‘4416×1242’
    AvailableResolutions: {‘4416×1242’}
    >

  • HeeYong

    Hi,
    I am using R2013b on window 8.1 and when I type the command “webcmalist” , I get the “undefined function or variable”
    R2013b is not supported??

  • Kurosh Sal

    Hi, after I enter >> preview(cam), i just get a black image, why?

    greedings from germany

  • prithvi raj

    i need to rectify the live images for 3D reconstruction. i am using matlab camera stereo calibration app.
    can you say me how to use the app for rectification(especially stereoparameters for rectifing) purpose.
    can you send me the code my id is mailprithvi17@gmail.com