Tutorial - Image Capture

This tutorial shows how to capture the left image of the ZED camera and print its timestamp and image size in the terminal. The program will loop until it has successfully grabbed 50 images. We assume that you have read the Hello ZED tutorial before.

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 the previous tutorial, here we create, configure and open the ZED. We set the camera to HD1080 resolution at 30 fps in this example.

// Create a ZED camera object
Camera zed;

// Set configuration parameters
InitParameters init_params;
init_params.camera_resolution = RESOLUTION_HD1080; // Use HD1080 video mode
init_params.camera_fps = 30; // Set fps at 30

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

Capture data

Now that the ZED is opened, we can capture the image stream coming from the camera. We create a loop that captures 50 images and exits.

To capture an image and process it, you need to call the Camera::grab() function. This function can take runtime parameters as well, but we leave them to default in this tutorial. Whenever you want a new image, you need to call this function. If grab() returns SUCCESS, a new image has been captured and is now available. You can also check the status of grab() which will tell you if something wrong happened.

// Grab an image
if (zed.grab() == SUCCESS) {
	// A new image is available if grab() returns SUCCESS
}

When grab is done, you can retrieve the image and its metadata. In this tutorial, we will retrieve the left image and its timestamp. To do so, we use retrieveImage() and getCameraTimestamp() functions.

retrieveImage() takes an sl::Mat as well as a VIEW mode as parameters. We first need to create the Mat before starting the loop. Note that you don’t need to allocate memory when creating a Mat. The first time retrieveImage() is called, the Mat is filled with image data and memory is allocated automatically. Note that you don’t need to allocate memory when creating a Mat. The first time retrieveImage() is called, memory is allocated automatically and the Mat is filled with image data.

// Capture 50 frames and stop
int i = 0;
sl::Mat image;
while (i < 50) {
    // Grab an image
    if (zed.grab() == SUCCESS) {
        // A new image is available if grab() returns ERROR_CODE::SUCCESS
        zed.retrieveImage(image, VIEW::LEFT); // Get the left image
        unsigned long long timestamp = zed.getCameraTimestamp(); // Get image frame timestamp
        printf("Image resolution: %d x %d  || Image timestamp: %llu\n", image.getWidth(), image.getHeight(), timestamp);
        i++;
    }
}

Note: the image timestamp is given in nanoseconds. You can compare the timestamp between two grab() : it should be close to the framerate time, if you don’t have any dropped frames.

Now that we have captured 50 images, we can close the camera and exit the program.

// Close the camera
zed.close();
return 0;

And this is it! Now let’s move on to the Depth Sensing tutorial to learn how to retrieve the depth map from the ZED.