Spatial labels on volumes
How to create Mask3D annotations on volumes in Python
Introduction
In this tutorial, you will learn how to programmatically create 3D annotations for volumes and upload them to Supervisely platform.
Supervisely supports different types of shapes/geometries for volume annotation, and now we will consider the primary type - Mask3D.
You can explore other types as well, like Mask (also known as Bitmap), Bounding Box (Rectangle), and Polygon. However, you'll find more information about them in other articles.
Learn more about Supervisely Annotation in JSON format for volumes.
Read about our enterprise-grade DICOM labeling toolbox in blog post Best DICOM & NIfTI annotation tools for Medical Imaging AI to be informed about all the advantages of our platform.
Everything you need to reproduce this tutorial is on GitHub: source code, Visual Studio Code configuration, and a shell script for creating virtual env.
Prepare data for annotations
During your work, you can create 3D annotation shapes, and here are a few ways you can do that:
NRRD files
The easiest way to create Mask3D annotation in Supervisely is to use NRRD file with 3D figure that corresponds to the dimensions of the Volume.
You can find an example NRRD file at data/mask/lung.nrrd in the GitHub repository for this tutorial.
NumPy Arrays
Another simple way to create Mask3D annotation is to use NumPy arrays, where values of 1 represent the object and values of 0 represent empty space.
On the right side, you can see a volume with a pink cuboid. Let's represent this volume as an NumPy array.
To draw a pink cuboid on it, you need to assign a value of 1 to the necessary cells. In the code below, each cell is indicated by three axes [
axis_0
,axis_1
,axis_2
].In the Python code example section, we will create a NumPy array that represents a foreign body in the lung as a sphere.
Images
You can also use flat mask annotations, such as black and white pictures, to create Mask3D from them. You just need to know which plane and slice it refers to.
You can find an example image file at data/mask/body.png in the GitHub repository for this tutorial.
If your flat annotation doesn't correspond to the dimensions of the plane, you also need to know its
PointLocation
. This will help to properly apply the mask to the image. This point indicates where the top-left corner of the mask is located, or in other words, the coordinates of the mask's initial position on the canvas or image.
Python code example
Import libraries and init API client
Create project and upload volumes
Create an empty project with the name "Volumes Demo" with one dataset "CTChest" in your workspace on the server. If a project with the same name exists in your workspace, it will be automatically renamed (Volumes Demo_001, Volumes Demo_002, etc.) to avoid name collisions.
Create annotations and upload into the volume
Auxiliary function for generating tumor NumPy array:
In the GitHub repository for this tutorial, you will find the full Python script.
How to debug this tutorial
Step 1. Prepare ~/supervisely.env
file with credentials. Learn more here.
Step 2. Clone the repository with source code and demo data and create a Virtual Environment.
Step 3. Open the repository directory in Visual Studio Code.
Step 4. Change ✅ workspace ID ✅ in local.env
file by copying the ID from the context menu of the workspace. A new project with annotated videos will be created in the workspace you define:
Step 5. Start debugging src/main.py
To sum up
In this tutorial, we learned:
What are the types of annotations for Volumes
How to create a project and dataset, upload volume
How to create 3D annotations and upload into volume
How to configure Python development for Supervisely
Last updated