> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.stereolabs.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.stereolabs.com/_mcp/server.

# Tutorial - Retrieve Tensor

This tutorial shows how to use `Camera::retrieveTensor()` to get a pre-processed image tensor directly from the ZED camera (resized, normalized and laid out in NCHW format), ready to feed into a deep learning inference pipeline without writing custom pre-processing code.

## Getting Started

* First, download the latest version of the [ZED SDK](https://www.stereolabs.com/developers/).
* Download the [Retrieve Tensor](https://github.com/stereolabs/zed-sdk/tree/master/tutorials/tutorial%2012%20-%20retrieve%20tensor) sample code in C++.

## Code Overview

### Open the camera

As in previous tutorials, we create, configure and open the ZED. Depth is not needed for this tutorial, so we set `DEPTH_MODE::NONE`.

**`C++`**

```cpp C++
// Create a ZED camera object
sl::Camera zed;

// Set configuration parameters
sl::InitParameters init_parameters;
init_parameters.camera_resolution = sl::RESOLUTION::HD720;
init_parameters.camera_fps = 30;
init_parameters.depth_mode = sl::DEPTH_MODE::NONE;

// Open the camera
sl::ERROR_CODE err = zed.open(init_parameters);
if (err > sl::ERROR_CODE::SUCCESS) {
    std::cout << "Error " << err << ", exit program." << std::endl;
    return EXIT_FAILURE;
}
```

### Configure the tensor parameters

`TensorParameters` describes the output format expected by your inference pipeline: target resolution, batch size, memory layout (`NCHW`/`NHWC`), pixel type, color format, and the normalization to apply (`scale`, `mean`, `std`). Here we target a 501x501 RGB float tensor on GPU memory, scaled to `[0, 1]` with no additional mean/std normalization.

**`C++`**

```cpp C++
// Prepare Tensor buffer
sl::Tensor tensor;

// Tensor parameters - Configure the output format for deep learning inference
sl::TensorParameters tensor_params;
tensor_params.target_size = sl::Resolution(501, 501);
tensor_params.batch_size = 1;
tensor_params.layout = sl::TensorParameters::LAYOUT::NCHW;
tensor_params.pixel_type = sl::TensorParameters::PIXEL_TYPE::FLOAT;
tensor_params.color_format = sl::TensorParameters::COLOR_FORMAT::RGB;
tensor_params.memory_type = sl::MEM::GPU;
tensor_params.scale = sl::float3(1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f);
tensor_params.stretch = true;
// The default normalization uses ImageNet statistics (mean={0.485, 0.456, 0.406}, std={0.229, 0.224, 0.225});
// here we disable normalization instead by using a neutral mean/std
tensor_params.mean = sl::float3(0.0f, 0.0f, 0.0f);
tensor_params.std = sl::float3(1.0f, 1.0f, 1.0f);
```

For the full list of available options, see the [`TensorParameters`](https://www.stereolabs.com/docs/api/structsl_1_1TensorParameters.html) API documentation.

### Retrieve the tensor

On each `grab()`, `retrieveTensor()` fills the `Tensor` object with the pre-processed image data, ready for inference. The sample captures 50 frames and prints the tensor dimensions every 10 frames.

**`C++`**

```cpp C++
// Main loop
int frame_count = 0;
while (frame_count < 50) {
    if (zed.grab() <= sl::ERROR_CODE::SUCCESS) {
        // Retrieve pre-processed tensor for inference
        sl::ERROR_CODE res = zed.retrieveTensor(tensor, tensor_params);

        if (res <= sl::ERROR_CODE::SUCCESS) {
            if (frame_count % 10 == 0) {
                std::vector<size_t> dims = tensor.getDims();
                std::cout << "Frame " << frame_count << ": Dims: [" << dims[0] << ", " << dims[1] << ", " << dims[2] << ", " << dims[3]
                          << "]" << std::endl;
            }
        } else {
            std::cout << "Error retrieving tensor: " << res << std::endl;
        }
        frame_count++;
    }
}
```

> **Note**
>
> The full sample also includes a helper function that downloads the tensor from GPU to CPU and reverses the normalization to save it back as a PNG image, which is a convenient way to visually check your pre-processing pipeline. See the [source code](https://github.com/stereolabs/zed-sdk/tree/master/tutorials/tutorial%2012%20-%20retrieve%20tensor) for the full implementation.

### Close the camera

**`C++`**

```cpp C++
zed.close();
```