Transform single segment in python module

Hi all,

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.

Thanks for your help,


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()

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)

#apply local transform

#Extract transformed segment 
transformedSegment = organSegmentationNode.GetSegmentation().GetSegment(segmentId)
#And add the transformedSegment to the outputTransformedSegmentationNode

At last :

mergedTransformedLabelmapNode = slicer.vtkMRMLLabelMapVolumeNode()
slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode(outputTransformedSegmentationNode, visibleSegmentIds, mergedTransformedLabelmapNode, inputM1LocalVolume)

But the segments in the output label map are not transformed. Is there anything I am missing or not doing correctly?

After your set the parent transform you also need to harden it.

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) ?

Keep the original segmentation node as is (untransformed), and move back all transformed segments back to this segmentation node.

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.

1 Like