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:
And according to this post:Calculating shared volume between two models, here are the steps I followed to get the Intersection of two segments:
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:
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’:
Go to the ‘Segmentation Editor’ module, switch the Segmentation node to ‘NEW Segmentation’, and now I notice that all effects are greyed out:
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.
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:
- 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()
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentEditorNode”)
tgt = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(‘RED’)
src = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(‘GREEN’)
effect = segmentEditorWidget.activeEffect()
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.
Thank you so much, it’s working now
Thank you so much, it’s working now!