Tutorial - Image Capture

Open in ClaudeOpen in ChatGPT

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 Image Capture sample code in C++, Python or C#.

Code Overview

Open the camera

As in the previous tutorial, here we create, configure and open the ZED. We set the 3D camera to dual HD 1080 resolution at 30 fps.

1// Create a ZED camera object
2Camera zed;
3
4// Set configuration parameters
5InitParameters init_params;
6init_params.camera_resolution = RESOLUTION_HD1080; // Use HD1080 video mode
7init_params.camera_fps = 30; // Set fps at 30
8
9// Open the camera
10ERROR_CODE err = zed.open(init_params);
11if (err != ERROR_CODE::SUCCESS)
12 exit(-1);

Capture Image Data

Now that the ZED is opened, we can capture the live stream coming from the camera. Let’s 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.

If grab() returns SUCCESS, a new image has been captured and is now available. You can also check the status of grab() which tells you if there is an issue during capture.

1// Grab an image
2if (zed.grab() == ERROR_CODE::SUCCESS) {
3 // A new image is available if grab() returns ERROR_CODE::SUCCESS
4}

Once the grab() method has been executed, you can retrieve the new image and its metadata. In this tutorial, we will retrieve the left image and its timestamp using retrieveImage() and getTimestamp().

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

1// Capture 50 frames and stop
2int i = 0;
3sl::Mat image;
4while (i < 50) {
5 // Grab an image
6 if (zed.grab() == ERROR_CODE::SUCCESS) {
7 // A new image is available if grab() returns ERROR_CODE::SUCCESS
8 zed.retrieveImage(image, VIEW::LEFT); // Get the left image
9 auto timestamp = zed.getTimestamp(sl::TIME_REFERENCE::IMAGE); // Get image timestamp
10 printf("Image resolution: %d x %d || Image timestamp: %llu\n", image.getWidth(), image.getHeight(), timestamp);
11 i++;
12 }
13}

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

For more information on Camera and Video parameters, read Using the Video API.

Close the Camera

Now that we have captured 50 images, let’s close the camera and exit the program.

1// Close the camera
2zed.close();

Additional Example

To learn how to adjust camera settings like Exposure, Gain, Contrast, Sharpness, etc. and display the resulting image, check the Camera Control sample code.