A minimal Python library for RT Struct manipulation
RT-Utils reads and writes the DICOM RT Structure Set, the file a treatment planning system stores contoured anatomy in. It turns an RTStruct into a NumPy mask you can compute on, and turns a mask from a segmentation model back into a conformant RTStruct that a planning system or viewer will open.
pip install rt-utilsNIfTI conversion needs SimpleITK, which is an optional extra:
pip install "rt-utils[nifti]"Python 3.9 and newer.
from rt_utils import RTStructBuilder
rtstruct = RTStructBuilder.create_new(dicom_series_path="./series")
# Your segmentation, as a boolean array shaped (Rows, Columns, Slices)
rtstruct.add_roi(mask=MASK_FROM_ML_MODEL, name="GTV", color=[255, 0, 255])
rtstruct.save("new-rt-struct")An ROI written from a liver segmentation model, as viewed in Slicer. (The underlying patient data has been hidden.)
from rt_utils import RTStructBuilder
rtstruct = RTStructBuilder.create_from(
dicom_series_path="./series",
rt_struct_path="./series/rt-struct.dcm",
)
print(rtstruct.get_roi_names())
mask = rtstruct.get_roi_mask_by_name("GTV") # (Rows, Columns, Slices), boolA mask is indexed (Rows, Columns, Slices):
| axis | index | runs |
|---|---|---|
| 0 | row | down a column |
| 1 | column | along a row |
| 2 | slice | through the series in ascending slice position |
The first two axes are ordered exactly as pydicom reads pixel_array, so a mask produced by segmenting the series already has the right layout. A mask from elsewhere, a NIfTI volume in particular, carries its own axis order and direction and may need rearranging first: see Mask layout, and use nifti_to_mask rather than guessing a transpose.
- Convert a NIfTI segmentation onto the series grid, reading both affines, with
label=to select one structure from a multi-label file - Write contours from coordinates in patient or pixel space, rather than from a mask
- Work entirely in memory, passing pydicom datasets in and getting a
BytesIOout - Manage ROIs: colours, names,
delete_roi, andRTROIInterpretedTypeso a planning system can tell a target from an organ at risk - Smooth masks before contouring, with a hook for your own implementation
- Merge two structure sets drawn on the same series
- Read a series held in a single file: a reconstructed SPECT, or an Enhanced CT, MR or PET
rt-utils.readthedocs.io: quickstart, guides, concepts and the full API reference.
Worth reading before your first add_roi: Mask layout. If something already looks wrong, start at Troubleshooting.
A comparison between RT-Utils and other conversion techniques is in this repository.
The real-world example from the JOSS paper was generated with RTUTILS_read_world_example_for_PET.ipynb.
Contributions are welcome. Please review our CONTRIBUTING guidelines, and see the changelog for what has changed recently.
If you are incorporating RT-Utils into your projects, kindly include the following citation:
Shrestha, A., A. Watkins, F. Yousefirizi, A. Rahmim, C. Uribe, "RT-utils: A minimal python library for RT-struct manipulation." Journal of Open Source Software, Vol. 10 (No. 107), pp. 7361, (2025). doi:10.21105/joss.07361.
Read the full paper: RT-utils: A Minimal Python Library for RT-struct Manipulation.

