# Segmentation is cropped to minimum extent

**URL:** <https://discourse.slicer.org/t/segmentation-is-cropped-to-minimum-extent/14788>\
**Category:** Support\
**Created:** [November 27, 2020, 2:25am UTC](https://discourse.slicer.org/t/segmentation-is-cropped-to-minimum-extent/14788 "2020-11-27T02:25:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ButuiHu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/butuihu/32/2386_2.png) [@ButuiHu](https://discourse.slicer.org/u/ButuiHu)\
**Post date:** [November 27, 2020, 2:25am UTC](https://discourse.slicer.org/t/segmentation-is-cropped-to-minimum-extent/14788/1 "2020-11-27T02:25:43Z")

</div>

step to reproduce:

1. create segmentation in segmen editor using the MRHead from sample data.
2. export segmentation to labelmap
3. save MRHead, Segmentation, and the labelmap (as `Segmentation-label.nrrd`) to file.
4. load MRHead again
5. load labelmap file `Segmentation-label.nrrd`, and select Descriptions as Segmentation.
6. modify the labelmap in segment editor
7. save the result again as `Segmentation-label-new.nrrd` without export segmentation to labelmap, and make sure crop to minimum extent is not checked.
8. 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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 27, 2020, 2:57am UTC](https://discourse.slicer.org/t/segmentation-is-cropped-to-minimum-extent/14788/2 "2020-11-27T02:57:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ButuiHu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/butuihu/32/2386_2.png) [@ButuiHu](https://discourse.slicer.org/u/ButuiHu)\
**Post date:** [November 27, 2020, 3:23am UTC](https://discourse.slicer.org/t/segmentation-is-cropped-to-minimum-extent/14788/3 "2020-11-27T03:23:56Z")

</div>

Yes, I’m using the nightly build (20201124). I do not enable the option, but `Segmentation-label-1.nrrd` is cropped to the minimum extent.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 27, 2020, 3:54am UTC](https://discourse.slicer.org/t/segmentation-is-cropped-to-minimum-extent/14788/4 "2020-11-27T03:54:21Z")

</div>

> [@ButuiHu](#):
>
> 1. create segmentation in segmen editor using the MRHead from sample data.
> 2. export segmentation to labelmap
> 3. save MRHead, Segmentation, and the labelmap (as `Segmentation-label.nrrd` ) to file.

Have a look at the files - MRHead, Segmentation, and the labelmap all have the same extent. They are all correct.

> [@ButuiHu](#):
>
> 1. load labelmap file `Segmentation-label.nrrd` , and select Descriptions as Segmentation.

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:

> <https://github.com/Slicer/Slicer/issues/5323>
>
> When a plain nrrd file is loaded as segmentation then the reference geometry is …not set and so when the segmentation is saved, the extent is reduced to the minimum necessary. We should preserve the original extent.
> 
> See details here:
> https://discourse.slicer.org/t/segmentation-is-cropped-to-minimum-extent/14788

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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_information_from_segmentation_nrrd_file_header).

---

<div class="post-metadata">

**Author:** ![ButuiHu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/butuihu/32/2386_2.png) [@ButuiHu](https://discourse.slicer.org/u/ButuiHu)\
**Post date:** [November 27, 2020, 4:46am UTC](https://discourse.slicer.org/t/segmentation-is-cropped-to-minimum-extent/14788/5 "2020-11-27T04:46:39Z")

</div>

thanks, if I would like to pad the segmentation to the reference geometry, what meta data should I consider in the `.seg.nrrd` file?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 27, 2020, 4:54am UTC](https://discourse.slicer.org/t/segmentation-is-cropped-to-minimum-extent/14788/6 "2020-11-27T04:54:27Z")

</div>

If you store segmentation in .seg.nrrd files (with [these custom metadata fields](http://apidocs.slicer.org/master/classvtkMRMLSegmentationStorageNode.html#details)) 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…
