I have a segmentation node (vtkMRMLSegmentationNode) that contains several segments (in this case, they represent organs) and I would like to apply a different linear transform (computed locally to the same reference fixed volume) to each of these segments and then combine the transformed segments in a new segmentation node.
So to achieve this, I export each single segment into a segmentation node (with GetSegment and AddSegment functions) that I transform with SetAndObserveTransformNodeID, and then export the transformed segment and add it to a final transformed segmentation node (with all the other transformed segments).
I don’t know if it is the right way to do this or if there is any easier solution, but it seems that the segments are not transformed, as if the transformation was not harden. Also, the segmentation nodes created do not seem accessible in the Slicer views, the eye icon is shaded.
You can only apply transformation to an entire segmentation, so moving out each each segment to a separate segmentation node is the right way.
If you apply a linear transform to a segmentation then hardening a transform does not need resampling. Hardening can be fully represented in change of segmentation origin, spacing, and axis directions.
If you want all your transformed segments in the same grid you can move back all the segments into the same segmentation node and then export to merged labelmap.
Thanks for your answer!
The problem is that the segments are not transformed when I use SetAndObserveTransformNodeID
#create the output/merged transformed segmentation node that will contain all transformed segments
outputTransformedSegmentationNode = slicer.vtkMRMLSegmentationNode()
outputTransformedSegmentationNode.SetName('TransformedSegmentationMask')
slicer.mrmlScene.AddNode(outputTransformedSegmentationNode)
then for each given segment :
#Get organ segment from its Id
organSegment = segmentationNode.GetSegmentation().GetSegment(segmentId)
#Export segment to segmentation node
organSegmentationNode = slicer.vtkMRMLSegmentationNode()
organSegmentationNode.SetName('OrganSegmentationTransformed' + segmentId)
organSegmentationNode.GetSegmentation().AddSegment(organSegment)
slicer.mrmlScene.AddNode(organSegmentationNode)
#apply local transform
organSegmentationNode.SetAndObserveTransformNodeID(localTransformLabelmapNode.GetID())
#Extract transformed segment
transformedSegment = organSegmentationNode.GetSegmentation().GetSegment(segmentId)
#And add the transformedSegment to the outputTransformedSegmentationNode
outputTransformedSegmentationNode.GetSegmentation().AddSegment(transformedSegment)
Thanks, works great!
One last thing : I iterate through the segments of the input segmentation node and apply the strategy described above, and both the input segmentation node and the output segmentation node (both containing all segments) are transformed at last. how can I keep the input segmentation node the same (and prevent the transformation of the segments it contains) ?
I made a deep copy of the segments before applying the transformation to their segmentation nodes, in order to put them back untransformed in the original merged segmentation node and it works.
Thanks a lot for your answers.
I found this topic very helpful. I just have one question regarding segment selector widget of my python module. The code line to add the segment to the new segmentation node (organSegmentationNode.GetSegmentation().AddSegment(organSegment)) makes the selector widget to return to the first segment of the segmentation. Thus, I need to click twice in the desired segment and unnecessary data is added to the mrml scene.
I don’t know if I’m doing something wrong so I would appreciate someone’s help.
I noticed that the bug was fixed, but I am having exactly the same problem. I tried to used the setCurrentNodeID and setCurrentSegmentID of the SegmentSelectorWidget in order to maintain the segmentID after add the segment to the new segmentationNode but without success.
Any idea why this continues to happen? Thank you very much!
You need to use the latest Slicer Preview Release. I’ve tested it and it works well for me, selection in the widget is preserved when new segment is added.