Using ZED and OpenCV in a Docker Container

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

In the next section, you will learn how to create a Docker image with ZED SDK and OpenCV. However, the present tutorial presumes that you are running a Docker container with all the necessary libraries to present an overview of using OpenCV in Docker.

Download and run the Docker Image #

To build an application using OpenCV you need to pull and run a Docker image with ZED SDK and OpenCV capabilities. If you wish to run applications that use display windows make sure your container also supports OpenGL.

$ docker pull <container_tag> # pull a docker container with ZED SDK , OpenCV and OpenGL support  
$ xhost +si:localuser:root  # allows container to communicate with X server
$ docker run  --gpus all --runtime nvidia --privileged -e DISPLAY -v /tmp/.X11-unix:/=tmp/.X11-unix <container_tag> # Run the Docker container 
#Remember: Replace <container tag> with the tag of your container image
  • --gpus all command adds all the available GPUs
  • --privileged grants permission to the container to access the camera connected to USB
  • --runtime nvidia will use the NVIDIA® container runtime.
  • -v /tmp/.X11-unix:/tmp/.X11-unix is required for the container to access the display.
  • -e DISPLAY makes your DISPLAY environment variable available in the container.

To know more about docker run usage and command options, read docker run description here.

Run a sample OpenCV application #

Once there is a container running you are good to get started. Download a sample OpenCV example, zed-opencv sample is the perfect place to get started.

The sample implements the following tasks:

  • Adjust several parameters: depth sensing mode, image resolution, and units.
  • Capture image, depth and point cloud from the ZED.
  • Convert image and depth map to compatible 32-bits float OpenCV matrix.
  • Display video and depth with OpenCV.
  • Save side-by-side images, depth images and point clouds in various formats.

For further explanation of the code refer to the ZED OpenCV documentation.

Install the zed-opencv sample code 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 go ahead and build other examples that use openCV in a similar way explained above. Program-specific explanations and build instructions can be found in their respective GitHub repository.

Next Steps #

Read the next section to learn how to actually Create Docker images with OpenCV.