Apply a linear transformation to a segmentation

Hi, I have a question. I would like to apply a linear transformation to a segmentation via command line and always save it as a segmentation, so I don’t want to refer it to any volume. it’s possible?
Thank you very much.
Michela

You can apply a transform to the segmentation node using SetAndObserveTransformNodeID(nodeID) followed by HardenTransform().

Note that linear transforms are applied without making any changes to the voxel array, just by simply updating the physical<->voxel coordinate system mapping.

If you want to make changes to the voxel array then you do need to specify a reference geometry. For a complete example of doing data augmentation, including random warping transformation, look at this example in the script repository.

1 Like

Hi @lassoan, I have a related question about this. I am currently running into an issue with my second moment of area module (as well as the segment cross-sectional area module). I want to apply a linear transformation to a segmentation node in case the initial volume was CT scanned at an oblique angle. This way, any slice geometries are calculated along an anatomically relevant axis. Although, when I input a transformed segmentation node (hardened or not hardened, doesn’t matter), it still calculates CSA along the original oblique axes. However, when I input a reference volume node then it works and CSA is calculated with the transformation in effect.

Why is that? Is there any way to make this work without a volume node and/or is re-slicing my only option? The reason I am trying to do this is because I’m interested in importing models into Slicer from elsewhere, converting them to segmentation nodes, and calculating slice geometries. These models may come at an oblique angle and without a reference volume.

Any help would be appreciated. Thanks!

Resampling of a volume is a lossy operation, therefore it is only performed if it is absolutely necessary. Since a linear transform can be applied to a volume by simply changing the origin, spacing, and axis directions of the voxel grid, there is no need to change the voxel array.

Of course, in your case, the goal is resampling, so you need to use one of the resampling modules (or Crop volume module).

You need to generate the reference volume yourself or use “Resample scalar/vector/DWI volume” module, in which you can specify the output geometry (origin, spacing, axis directions, and extents). Alternatively, you can use Crop volume module - in that case you need to create a ROI node and specify spacing. Use the new Markups ROI node, as you can specify axis directions directly there, and not just via a transform node (and the old Annotation ROI will go away at some point anyway).