Segmentation to binary label map through python console

Hello,

I have a lot of RTstructs that I need to convert to binary label maps. I tried using the scripts for exporting to label map in the repository here (Documentation/4.8/ScriptRepository - Slicer Wiki)

here is the code:

seg = getNode(‘Segmentation’)
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLLabelMapVolumeNode’)
slicer.modules.segmentations.logic().ExportAllSegmentsToLabelmapNode(seg, labelmapVolumeNode)

That worked great except for the fact that the label map node is only the size of the label (if the image is like 512x512x100, the label map ended up being 7x7x3). I assume this is because the label map volume that is created isn’t created with the same image dimensions as the original image. I can do this manually using the segmentations module using export but I would rather do it programmatically. I tried following this example: Slicer/Utilities/Templates/Modules/ScriptedSegmentEditorEffect/SegmentEditorTemplateKeyLib/SegmentEditorEffect.py at main · Slicer/Slicer · GitHub

for creating a label map but I couldn’t get it to work.

Any help that can be provided would be great thanks

There is a tool just doing that: SlicerRT/BatchProcessing/_readme.txt at master · SlicerRt/SlicerRT · GitHub

If you insist on using your own code you can call ExportVisibleSegmentsToLabelmapNode or a few others that have an argument called referenceVolumeNode where you can specify the reference volume.

Oh great, thanks. I think I figured out how to do what I was thinking but I can use this.

I just tried to use it in my command line (windows 10), it doesn’t seem to do anything. How are the files supposed to be? Currently they are all dicoms in their own folders (each RTstruct is in its own folder, same with the reference CT). I tried moving them all to the same folder and it didn’t work Here is how I called it:

“C:\Program Files\Slicer 4.8.0\Slicer.exe” --no-main-window --python-script “F:\c study\Python\BatchStructureSetConversion.py” --input-folder “F:/c study/R1/Ims” --output-folder “F:/contouring study/R1/testBatch”

where the input folder Ims has all the files.

EDIT: also it seems that running this script completely removed everything from my dicom directory?

Hi, have you tried using the following code to convert segmentation map to a labelmap using your reference volume to determine the dimensions of the labelmap?:

seg = getNode(‘Segmentation’)
reference = getNode (‘InputVolume’) # this will be the volume the segmentation was drawn on
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLLabelMapVolumeNode’)

ids = vtk.vtkStringArray()
seg.GetDisplayNode().GetVisibleSegmentIDs(ids)
slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(seg, ids, labelmapVolumeNode, reference)

1 Like