Tutorial - Using Spatial Mapping
Tutorial - Using Spatial Mapping
This tutorial shows how to use a ZED stereo camera to capture a live 3D reconstruction of your environment. The code starts spatial mapping for 500 frames, extracts a mesh, filters it and saves it as an OBJ file.
Getting Started
- First, download the latest version of the ZED SDK.
- Download the Spatial Mapping sample code in C++, Python or C#.
Code Overview
Open the camera
Let’s initialize the camera. In this example, we use HD720 mode at 60 fps (which also has a wide FOV) to make sure camera tracking is reliable. We also select a right-handed coordinate system with the Y axis up, since it is the most common system used by 3D viewing software (Meshlab for example).
Enable positional tracking
Spatial mapping needs position tracking to be enabled first with enablePositionalTracking().
Enable spatial mapping
Spatial mapping setup works the same way as positional tracking: we create a SpatialMappingParameters settings and call enableSpatialMapping() with this parameter.
It is not the purpose of this tutorial to go into the details of SpatialMappingParameters class. For more information, read the API documentation.
Run live 3D reconstruction
To start mapping an area, there is no need to call any function in the grab() loop. The ZED SDK checks in the background that a new image, depth and position is available and automatically builds a 3D map asynchronously from this data.
In this tutorial, we simply grab 500 frames, check the 3D mapping state and then stop the loop to extract the resulting mesh.
Extract mesh
After capturing a 3D map during 500 frames, we can now retrieve the mesh stored in the Mesh object using extractWholeSpatialMap(). This function will be blocking until the full mesh is available.
It is possible to asynchronously retrieve a mesh during mapping. See the Using Spatial Mapping API section.
This mesh can be filtered (if needed) to remove duplicate vertices and unneeded faces. Then we save the mesh as an OBJ file for external use.
Disable modules and exit
Once the mesh is extracted and saved, don’t forget to disable the mapping and tracking modules (in this order) and close the camera before exiting your application.
Advanced Example
To learn how to capture a live 3D mesh of the environment and display it as an overlay on the camera image, check the Live 3D Reconstruction sample code.

