A minimal Python library for RT Struct manipulation
RT-Utils is motivated to allow physicians and other users to view the results of segmentation performed on a series of DICOM images. RT-Utils allows you to create or load RT Structs, extract 3d masks from RT Struct ROIs, easily add one or more regions of interest, and save the resulting RT Struct in just a few lines! You can also use the RT-Utils for merging two existing RT Structs to one file.
RT-Utils provides a builder class to faciliate the creation and loading of an RT Struct. From there, you can add ROIs through binary masks and optionally input the colour of the region along with the region name.
The format for the ROI mask is an nd numpy array of type bool. It is an array of 2d binary masks, one plane for each slice location within the DICOM series. The slices should be sorted in ascending order within the mask. Through these masks, we extract the contours of the regions of interest and place them within the RT Struct file. Note that there is currently only support for the use of one frame of reference UID and structered set ROI sequence. Also note that holes within the ROI may be handled poorly.
pip install rt_utils
git clone https://github.com/qurit/rt-utils.git
cd rt-utils
pip install -e .
from rt_utils import RTStructBuilder
# Create new RT Struct. Requires the DICOM series path for the RT Struct.
rtstruct = RTStructBuilder.create_new(dicom_series_path="./testlocation")
# ...
# Create mask through means such as ML
# ...
# Add the 3D mask as an ROI.
# The colour, description, and name will be auto generated
rtstruct.add_roi(mask=MASK_FROM_ML_MODEL)
# Add another ROI, this time setting the color, description, and name
rtstruct.add_roi(
mask=MASK_FROM_ML_MODEL,
color=[255, 0, 255],
name="RT-Utils ROI!"
)
rtstruct.save('new-rt-struct')
from rt_utils import RTStructBuilder
import matplotlib.pyplot as plt
# Load existing RT Struct. Requires the series path and existing RT Struct path
rtstruct = RTStructBuilder.create_from(
dicom_series_path="./testlocation",
rt_struct_path="./testlocation/rt-struct.dcm"
)
# Add ROI. This is the same as the above example.
rtstruct.add_roi(
mask=MASK_FROM_ML_MODEL,
color=[255, 0, 255],
name="RT-Utils ROI!"
)
rtstruct.save('new-rt-struct')
The results of a generated ROI with a dummy mask, as viewed in Slicer.
The results of a generated ROI with a liver segmentation model, as viewed in Slicer. (Note the underlying patient data has been hidden)
from rt_utils import RTStructBuilder
import matplotlib.pyplot as plt
# Load existing RT Struct. Requires the series path and existing RT Struct path
rtstruct = RTStructBuilder.create_from(
dicom_series_path="./testlocation",
rt_struct_path="./testlocation/rt-struct.dcm"
)
# View all of the ROI names from within the image
print(rtstruct.get_roi_names())
# Loading the 3D Mask from within the RT Struct
mask_3d = rtstruct.get_roi_mask_by_name("ROI NAME")
# Display one slice of the region
first_mask_slice = mask_3d[:, :, 0]
plt.imshow(first_mask_slice)
plt.show()
The results of a loading an exisiting ROI as a mask, as viewed in Python.
To be able to merge two RT Structs it is important that both RT Structs have to belong to the same image series, e.g. if there is one set for the organs at risk and one set for the target volume(s).
from rt_utils import RTStructMerger
# Load existing RT Structs and corresponding image series and merge them into one RTStruct
merged_rt_struct = RTStructMerger.merge_rtstructs(
dicom_series_path="./testlocation",
rt_struct_path1="./testlocation/rt-struct1.dcm",
rt_struct_path2="./testlocation/rt-struct2.dcm"
)
merged_rt_struct.save('merged-rt-struct')
The add_roi method of our RTStruct class has a multitude of optional parameters available. Below is a comprehensive list of all these parameters and what they do.
nifti to rtstruct
If you are incorporating RT-Utils into your projects, kindly include the following citation:
Shrestha A, Watkins A, Yousefirizi F, Rahmim A, Uribe CF (2024). RT-utils: A Minimal Python Library for RT-struct Manipulation. arXiv preprint arXiv:2405.06184.