I am currently writting my own loadable module and I would need to segment a volume (for example by thresholding it) and display the segmentation in color over the segmented volume, as it is done in the “Segmentations” module (“Segment Editor”, the threshold effect).
To do so, I read the code of this loadable module (mostly qSlicerSegmentEditorAbstractEffect.cxx and SegmentEditorThresholdEffect.py) and I managed to extract some of the things I would need but I’m missing things and I’m a bit lost in the two files mentionned.
Thanks for any answer that could help me understand how to do this.
Probably the simplest is to create a segment editor widget, set its inputs (scene, input volume, segmentation), activate an effect, set parameters, and apply the effect.
Here is an example in Python (the same works in C++ with some trivial changes in syntax):
# Create segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
# To show segment editor widget (useful for debugging): segmentEditorWidget.show()
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
# Run segmentation
segmentEditorWidget.setActiveEffectByName("Grow from seeds")
effect = segmentEditorWidget.activeEffect()
# You can change parameters by calling: effect.setParameter("MyParameterName", someValue)
# Most effect don't have onPreview, you can just call onApply
# Clean up and show results
# Clean up
# Make segmentation results nicely visible in 3D
segmentationDisplayNode = segmentationNode.GetDisplayNode()
I guess it is possible to clean, by removing the initialization of the segment editor node (and just creating a segmentation node). However, in my case I inizialize the segment editor, and, then, I use the segmentation node create from the segmentation editor. This because I want that the segments that I create by thresholding the volumes will appear in the Segmentation editor module immediatly, so the user has already everything settled for modifying the selection done by thresholding.
I would need to do my own segmentation after (and not use an already existing one like thresholding or growing from seeds in slicer) so I guess with your solution Andras it might not be possible ? or I would need to create my own effect in order to activate it ?
I believe I won’t have this issue with your solution Davide, so I’ll try to work from the code you gave me and come back to you if necessary.
Davide, with your solution, would it be possible to display two segments of the same volume with two different colors ? I think that’s what you did with your SegmentTwo and SegmentThree both done on volumeTwo but if I follow your example only the first segment is displayed.
You can set the color of any segments independently. You can also have as many segments in the segmentation as you want and show them as the same time. If you need overlapping segments, set masking option Overwrite other segments to None.
I’ve already tried adding this line at the end:
SegmentationDisplayNode->SetSegmentOverrideColor(SegmentTwoID, 0.7, 0.5, 0.5);
And I get only the first segment displayed with the new color (0.7,0.5,0.5)
And even without trying to modify the color of any segment, I only get the first segment displayed
Add your segment to the scene and check if all segments shows up in the Segmentations module. If you still have problems then save the segmentation node to file and share it with us so that we can have a look (upload to Dropbox or OneDrive and attach the link).
Hello.Recently, I have been writing modules in C + +, and I have also studied a lot about the call of segmenteditor. I want to know how to apply effects in C + +, such as threshold apply.
Best wishes to you.Thank you in advance.