Save overlapping segments in binary labelmaps - Nrrd

Hello everyone.
What a great software Slicer is and what a great community it has. I have been using Slicer for a while for visualization and registration. But currently, I am performing a radiomic study where I believe the most efficient strategy would be to convert the structures in the RT_STRUCT file to an NRRD (I have multiple patients and would like to perform the features extraction from all at once). In radiotherapy, some structures overlap, such as the CTV and PTV. Converting the visible segments to a binary label map saves only the intersection (as expected since it is binary). Is it possible to save the structures of the RT_STRUCT file to a single NRRD volume with overlaps? The idea is to open it with SimpleITK and select the desired label.
Thank you

Segmentation (with or without overlapping segments) is always saved into a single nrrd file. When segments overlap then Slicer automatically adds a 4th dimension (“layer”) to the volume. See more information here.

You can conveniently get segments from such 4D nrrd files conveniently with the slicerio Python package. The package can look up label value and layer index for a specific segment and get the segment as a numpy array.

1 Like

Dear @lassoan,
Thank you for your quick reply. I did follow your recommendation in a previous post, https://discourse.slicer.org/t/how-can-i-convert-an-rtstruct-to-an-nrrd/539/2, and I was able to save a 4D volume. The problem is we can’t reference the CT volume, and the number of images doesn’t match the number of masks, a necessary condition for PyRadiomics. Exporting to a file in the segmentations module does not save overlapping segments but provides the ability to reference a volume. I will try the slicerio Python package as you recommended since it seems very promising.

If your segmentation’s geometry does not match the CT geometry then you can click the “Specify geometry” button in Segment Editor and choose the CT as source geometry.

1 Like

@lassoan Thank you so much for your support. It did the trick. And the slicerio package is a very useful utility. Highly recommended it.

1 Like

For future reference and for someone with the same issue, to align the geometries of both volumes (CT + label map), we need to do the following:

image_vol = sitk.ReadImage(ct_nrrd_file_path)
origin = image_vol.GetOrigin()
spacing = image_vol.GetSpacing()
direction = image_vol.GetDirection()
segmentation_info = slicerio.read_segmentation_info(label_nrrd_path)
voxels, header = nrrd.read(label_path)
extracted_voxels, extracted_header = slicerio.extract_segments(voxels, header, segmentation_info, [(‘label_name’, label_value)])
label_map = sitk.GetImageFromArray(np.swapaxes(extracted_voxels.astype(np.uint16),0,2))
label_map.SetOrigin((origin[0], origin[1], origin[2]))
label_map.SetSpacing((spacing[0], spacing[1], spacing[2]))
label_map.SetDirection(direction)