create segmentation in segmen editor using the MRHead from sample data.
export segmentation to labelmap
save MRHead, Segmentation, and the labelmap (as Segmentation-label.nrrd) to file.
load MRHead again
load labelmap file Segmentation-label.nrrd, and select Descriptions as Segmentation.
modify the labelmap in segment editor
save the result again as Segmentation-label-new.nrrd without export segmentation to labelmap, and make sure crop to minimum extent is not checked.
now read MRHead, Segmentation-label.nrrd, and Segmentation-label-1.nrrd, and convert to numpy array with simpleitk, check their shape.
The shape is expected to be the same, but it’s not.
In current Slicer versions, segmentation is not cropped to the minimum extent anymore by default. You can enable this option (to reduce memory usage) or disable it (to make it easier to use the segmentation in software that operates in voxel space instead of physical space) in the Save data dialog.
Have a look at the files - MRHead, Segmentation, and the labelmap all have the same extent. They are all correct.
The segmentation that is imported from a labelmap is not associated with any reference volume, so there is no reference geometry to stick to when the segmentation is saved. We need to consider other use cases, too, but probably we can change the behavior so that reference geometry will be set when importing a plain nrrd or nifti file. I’ve added a ticket to track this request:
I would recommend to use .seg.nrrd file format for storing segmentations. You can use it the same way in SimpleITK, but it stores extra information, such as segment names, colors, etc. You can access this metadata from Python, without Slicer as shown here.
If you store segmentation in .seg.nrrd files (with these custom metadata fields) then the reference geometry will be preserved.
Reference image geometry is stored in Segmentation_ConversionParameters field:
Segmentation_ConversionParameters:=…sampling is calculated.&Reference image geometry|0;0;1.2999954223632812;-86.64489746093749;-1;0;0;133.92860412597656;0;-1;0;116.78569793701172;0;0;0;1;0;255;0;255;0;129;|Image geometry description string determining the geometry of the labelmap that is created in course of conversion. Can be copied from a volume, using the button.&Smoothing factor|0.5|Smoo…