Nifti segmentation to dicom-seg in a batch with python

Hi,

I use 3D slicer for a while, but am rather new with the python usage in slicer.
I have to convert a lot of nifti files to dicom-seg format, which is a cumbersome task, so I wanted to do that in a batch as the nifti files with every CT dicom are named the same.

I tried first to do it with one scan and used the following code:
‘’’
import slicer
from DICOMLib import DICOMUtils
import DICOMScalarVolumePlugin
import DICOMSegmentationPlugin

segmentationNode = r’path_to_segmentation_in_nifti’
dicomDataDir = r’path_to_CT_in_dicom’
outputFolder = r’path_to_output_folder’

loadedNodeIDs = # this list will contain the list of all loaded node IDs
with DICOMUtils.TemporaryDICOMDatabase() as db:
DICOMUtils.importDicom(dicomDataDir, db)
patientUIDs = db.patients()
for patientUID in patientUIDs:
loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

Associate segmentation node with a reference volume node

shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
referenceVolumeShItem = shNode.GetItemByDataNode(loadedNodeIDs)
studyShItem = shNode.GetItemParent(referenceVolumeShItem)
segmentationShItem = shNode.GetItemByDataNode(segmentationNode)
shNode.SetItemParent(segmentationShItem, studyShItem)

Export to DICOM

exporter = DICOMSegmentationPlugin.DICOMSegmentationPluginClass()
exportables = exporter.examineForExport(segmentationShItem)
for exp in exportables:
exp.directory = outputFolder
exporter.export(exportables)
‘’’

But I get an error when trying to read in the referenceVolume:
Traceback (most recent call last):
File “”, line 1, in
TypeError: GetItemByDataNode argument 1: method requires a VTK object

I’m not sure how to get this NodeIDs into a VTK object. Could anyone help me give the golden tip.

Thanks!

I think the issue is likely that loadedNodeIDs is a list of ID strings of image volume nodes, whereas GetItemByDataNode() expects its input to be a node, rather than a node ID. If you change the erroring line to

# Get the image node by its ID string
referenceVolumeNode = slicer.mrmlScene.GetNodeByID(loadedNodeIDs[0])
# Get the subject hierarchy item id corresponding to the 
referenceVolumeShItem = shNode.GetItemByDataNode(referenceVolumeNode)

Perfect! That really helped.
I now updated the code to:

import slicer
from DICOMLib import DICOMUtils
import DICOMScalarVolumePlugin
import DICOMSegmentationPlugin

niftiPath = r’path_to_segmentation_in_nifti’
dicomDataDir = r’path_to_CT_in_dicom’
outputFolder = r’path_to_output_folder’

#Load the NIfTI segmentation
segmentationNode = slicer.util.loadSegmentation(niftiPath)
segmentationNode.SetAttribute(‘DICOM.SeriesDescription’, ‘Segmentation Series Description’) # Customize the description
segmentationNode.SetAttribute(‘DICOM.Modality’, ‘SEG’)

loadedNodeIDs = # this list will contain the list of all loaded node IDs
db = slicer.dicomDatabase
DICOMUtils.importDicom(dicomDataDir, db)
patientUIDs = db.patients()
for patientUID in patientUIDs:
loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

#Associate segmentation node with a reference volume node
shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
referenceVolumeNode = slicer.mrmlScene.GetNodeByID(loadedNodeIDs[0])
referenceVolumeShItem = shNode.GetItemByDataNode(referenceVolumeNode)

#Set the reference volume for the segmentation
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(referenceVolumeNode)
studyShItem = shNode.GetItemParent(referenceVolumeShItem)
segmentationShItem = shNode.GetItemByDataNode(segmentationNode)
shNode.SetItemParent(segmentationShItem, studyShItem)

#Export to DICOM
exporter = DICOMSegmentationPlugin.DICOMSegmentationPluginClass()
exportables = exporter.examineForExport(segmentationShItem)
for exp in exportables:
exp.directory = outputFolder

exporter.export(exportables)

However, the export does not seem to work properly yet. The modality is not set to SEG, and if I want to open it in a different software program, it is not recognized as a segmentation.

What I do manually is, create a new subject with a new study, link the CT and the segmentations. Then, in segment editor I change the source geometry of the segmentations to the CT, and then export the segmentations with the DICOMSegmentationPlugin.

I cannot really find a way to do that with Python. Does someone have a suggestion?