Tutorial - Depth Perception

This tutorial shows how to retrieve the depth from stereo image and point cloud of a scene with the ZED SDK, and print the distance of a given point in the terminal. The program will loop until 50 frames are grabbed. We assume that you have followed the previous tutorials: Hello ZED and Image Capture.

Getting Started

  • First, download the latest version of the ZED SDK.
  • Download the C++ or Python sample code.
  • Follow the instructions on how to build your project in C++ or Python on Windows and Linux.

Code Overview

Create a camera

As in other tutorials, we create, configure and open the ZED. We set the ZED in HD720 mode at 60fps and enable depth in PERFORMANCE mode. For more information on this setting, see Depth Modes.

// Create a ZED camera
Camera zed;

// Create configuration parameters
InitParameters init_params;
init_params.sdk_verbose = true; // Enable the verbose mode
init_params.depth_mode = DEPTH_MODE_PERFORMANCE; // Set the depth mode to Performance (fastest)


// Open the camera
ERROR_CODE err = zed.open(init_params);
if (err!=SUCCESS)
  exit(-1);

Note: By default, Default parameter for depth mode is DEPTH_MODE_PERFORMANCE. In practice, it is not necessary to set the depth mode in InitParameters.

Capture data

Now that the ZED is opened, we can capture images and depth. Here we loop until we have successfully captured 50 images. Retrieving the depth map is as simple as retrieving an image: * We create a Mat to store the depth map. * We call retrieveMeasure() to get the depth map.

// Capture 50 images and depth, then stop
int i = 0;
sl::Mat image, depth;
while (i < 50) {
    // Grab an image
    if (zed.grab(runtime_parameters) == SUCCESS) {

        // A new image is available if grab() returns ERROR_CODE::SUCCESS
        zed.retrieveImage(image, VIEW::LEFT); // Get the left image
        zed.retrieveMeasure(depth, MEASURE_DEPTH); // Retrieve depth Mat. Depth is aligned on the left image
        i++;
    }
}

Now that we have retrieved the depth map, we may want to get the depth at a specific pixel. In the example, we extract the distance of the point at the center of the image (width/2, height/2)

// Get and print distance value in mm at the center of the image
// We measure the distance camera - object using Euclidean distance
int x = image.getWidth() / 2;
int y = image.getHeight() / 2;
sl::float4 point_cloud_value;
point_cloud.getValue(x, y, &point_cloud_value);

float distance = sqrt(point_cloud_value.x*point_cloud_value.x + point_cloud_value.y*point_cloud_value.y + point_cloud_value.z*point_cloud_value.z);
printf("Distance to Camera at (%d, %d): %f mm\n", x, y, distance);

Once 50 frames have been grabbed, we close the camera.

// Close the camera
zed.close();

You are now using the ZED as a depth sensor. Let’s move on to the next tutorial to learn how to use the ZED as a positional tracker device .