Using ZED and OpenCV in a Docker Container

Open in ClaudeOpen in ChatGPT

This tutorial shows how to use ZED SDK applications together with the OpenCV library in a Docker container. It gives an overview of using ZED cameras and OpenCV in a container, and demonstrates how to build and run an OpenCV application.

In the next section, you will learn how to create your own Docker image with the ZED SDK and OpenCV. The present tutorial assumes you are already running a container that includes all the required libraries, so we can focus on using OpenCV in Docker.

Download and Run the Docker Image

To build an application with OpenCV, you need to pull and run a Docker image that includes the ZED SDK and OpenCV. If you want to run applications with display windows, make sure your container also supports OpenGL (a gl-devel image).

$docker pull <container_tag> # pull a container with ZED SDK, OpenCV, and OpenGL support
$xhost +si:localuser:root # allow the container to communicate with the X server
$# On a desktop host:
$docker run --gpus all --privileged -e NVIDIA_DRIVER_CAPABILITIES=all -e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v /dev:/dev <container_tag>
$# On NVIDIA® Jetson™, replace "--gpus all" with "--runtime nvidia"
$# Remember: replace <container_tag> with the tag of your container image
  • --gpus all exposes all available GPUs to the container (use --runtime nvidia on NVIDIA® Jetson™).
  • --privileged grants the container access to the camera connected over USB.
  • -e NVIDIA_DRIVER_CAPABILITIES=all enables the graphics capabilities required by OpenGL.
  • -v /tmp/.X11-unix:/tmp/.X11-unix lets the container access the display.
  • -e DISPLAY makes your DISPLAY environment variable available inside the container.
  • -v /dev:/dev shares the video devices with the container.

For more details on docker run and its options, see the Docker run reference.

Run a Sample OpenCV Application

Once a container is running, you are ready to get started. The zed-opencv sample is a great starting point.

The sample performs the following tasks:

  • Sets several parameters: depth sensing mode, image resolution, and units.
  • Captures image, depth, and point cloud data from the ZED.
  • Converts the image and depth map to compatible 32-bit float OpenCV matrices.
  • Displays the video and depth with OpenCV.
  • Saves side-by-side images, depth images, and point clouds in various formats.

For a detailed explanation of the code, refer to the ZED OpenCV documentation.

Clone the zed-opencv sample from GitHub (https://github.com/stereolabs/zed-opencv):

$git clone https://github.com/stereolabs/zed-opencv.git

Compile the Code and Run the Application

Build the C++ code and run the application:

$cd zed-opencv/cpp
$mkdir build && cd build
$cmake ..
$make
$./ZED_with_OpenCV

Congratulations! You have built your first ZED + OpenCV application using Docker.

You can now build other examples that use OpenCV in the same way. Program-specific explanations and build instructions can be found in their respective GitHub repositories.