How to programmatically create the Master Volume for a segmentation node with multiple segments

Hi there,
I’m new to 3D Slicer and now I’m developing a Python scripting module.

Currently, I want to implement a procedure in python code of my module for calculating the volume value of the overlapping/Intersection part of two segments.
As shown in the figure below, at the beginning I have two segmentation nodes, I set their color to red and green respectively:

Figure1
Figure2

And according to this post:Calculating shared volume between two models, here are the steps I followed to get the Intersection of two segments:

  1. Go to the ‘Segmentation’ module and use ‘Export/Import Models and Labelmaps’ to export the two segments to the ‘Models’ module to create two corresponding models:
    Figure3
    Figure4

  2. Go to the ‘segmentation’ module and create a new segmentation node, I named it ‘NEW Segmentation’, and import both of models get from the previous step into this new segmentation node using the ‘Export/Import Models and Labelmaps’:
    Figure5

  3. Go to the ‘Segmentation Editor’ module, switch the Segmentation node to ‘NEW Segmentation’, and now I notice that all effects are greyed out:
    Figure6

According to the example given by @mikebind in this post: How to programmatically use logical operator add function, I found that in order to use the logical operator effect, which in my case is the ‘INTERSECT’ opreation, a master volume must be selected first, although I know I can create this Master Volume by clicking on the small “Specify geometry button” and selecting the ‘NEW Segmentation’ in the drop down box(the lowest option), but how to create the Master Volume for the ‘NEW Segmentation’ programmatically using python?

Thank you very much in advance!

seg = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")

adds an empty segmentation node to your scene.

Hello Rudolf,
Thank you for your reply!

I’ve tried the code you provided. However, If I’m correct, this is actually the same as clicking the ‘Create new segmentation’ in the ‘Segmentations’ module, and this is how the ‘NEW segmentation’ I mentioned above with two segments was created:

  1. Go to the ‘segmentations’ module and create a new segmentation node, I named it ‘ NEW Segmentation ’, and import both of models get from the previous step into this new segmentation node using the ‘Export/Import Models and Labelmaps’

So, how to create the Master Volume for the ‘NEW Segmentation’ programmatically using python? Because after I create this Master Volume for the ‘NEW Segmentation’, I can execute code similar to the following to use logical operator effects and the ‘INTERSECT’ operation:

segmentationNode = getNode(‘NEW Segmentation’)
masterVolume = segmentationNode.GetNodeReference(slicer.vtkMRMLSegmentationNode().GetReferenceImageGeometryReferenceRole())
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentEditorNode”)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolume)
tgt = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(‘RED’)
src = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(‘GREEN’)
segmentEditorNode.SetSelectedSegmentID(tgt)
segmentEditorWidget.setActiveEffectByName(“Logical operators”)
effect = segmentEditorWidget.activeEffect()
effect.setParameter(“Operation”,“INTERSECT”)
effect.setParameter(“ModifierSegmentID”,src)
effect.self().onApply()

You can either get the volume node from an MRML node like

volumeNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLScalarVolumeNode")

or create a new one like

volumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode")

Check out the script repository for more examples.

2 Likes

Thank you so much, it’s working now :+1:

1 Like

Thank you so much, it’s working now!