# How to get segmentations as numpy arrays with same dimensions?

**URL:** <https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [June 23, 2020, 6:57am UTC](https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202 "2020-06-23T06:57:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SummerZ2020](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/summerz2020/32/7137_2.png) [@SummerZ2020](https://discourse.slicer.org/u/SummerZ2020)\
**Post date:** [June 23, 2020, 6:57am UTC](https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202/1 "2020-06-23T06:57:11Z")

</div>

hello, lassoan. I encountered the same problem when save the segmentation result to file. I right clicked the segmentation in the “Subject hierarchy”, and then choose “export visible segments to binary labelmap”, and got the labelmap which name is “Segmentation-bone-label”. And then I got the segmentation mask array using python. But the size of each image in segmentation mask is only 224_332, and the original image in volume is 512_512. I got the segmentation mask which size is same as original image before. And I donot know why this time the size is only bounding box. Here is the python code I used:

> > > bone=array(“Segmentation-bone-label”)  
> > > bone.shape  
> > > (114, 224, 322)  
> > > volume = array(“5 ARTERIAL PHASE”)  
> > > volume.shape  
> > > (114, 512, 512)

Looking forward to your reply, thank you so much.

---

<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:** [June 23, 2020, 9:40pm UTC](https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202/2 "2020-06-23T21:40:51Z")

</div>

The simplest is to use the latest Slicer version, which does not crop the segmentation to the minimum necessary size. If you already created your segmentation in Slicer-4.10 then you need to specify a Reference volume as described above.

---

<div class="post-metadata">

**Author:** ![SummerZ2020](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/summerz2020/32/7137_2.png) [@SummerZ2020](https://discourse.slicer.org/u/SummerZ2020)\
**Post date:** [June 24, 2020, 1:36am UTC](https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202/4 "2020-06-24T01:36:24Z")

</div>

The version is 4.11(2020-6-15). And where can i find the advanced export section in 4.11 to add reference volume. In the screenshot below, you can see the segmentation size is not 512\*512, and the Slicer version is 4.11.

 ![export](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/9/89cead611f9eb57d62200b2ac267bca632f49de5.png)

---

<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:** [June 24, 2020, 3:35pm UTC](https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202/5 "2020-06-24T15:35:16Z")

</div>

How did you create `Segmentation_1-bone-label` and `Segmentation_2-bone-label`?  
If they were created from two different segmentations (of two different volumes), could you check the dimensions of these volumes (in Volumes module, Volume Information section)?

---

<div class="post-metadata">

**Author:** ![JanWitowski](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/janwitowski/32/4512_2.png) [@JanWitowski](https://discourse.slicer.org/u/JanWitowski)\
**Post date:** [June 25, 2020, 6:40am UTC](https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202/6 "2020-06-25T06:40:59Z")

</div>

In addition to @lassoan replies, you can change the segmentation geometry to match volume geometry with `SetReferenceImageGeometryParameterFromVolumeNode()`. This allowed me in the past to batch process mismatched examples, e.g.

```
loadedVolumeNode = slicer.util.loadVolume(ct_path)
loadedSegmentationNode = slicer.util.loadSegmentation(seg_path)
loadedSegmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(loadedVolumeNode)
slicer.util.saveNode(loadedSegmentationNode, seg_path)
slicer.mrmlScene.RemoveNode(loadedVolumeNode)
slicer.mrmlScene.RemoveNode(loadedSegmentationNode)
```

---

<div class="post-metadata">

**Author:** ![SummerZ2020](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/summerz2020/32/7137_2.png) [@SummerZ2020](https://discourse.slicer.org/u/SummerZ2020)\
**Post date:** [June 25, 2020, 2:09pm UTC](https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202/7 "2020-06-25T14:09:17Z")

</div>

yes, they are from differnent volumes.Here is how can I create the labelmap in the scrrenshot 1-2. and the third screenshot showes the volume dimension. And I tried to get the dimension of the labelmap, the result is shown below.

> > > bone = array(“Segmentation\_5-bone-label”)  
> > > bone.shape  
> > > (118, 276, 382)  
> > > volume = array(“4 Tx Abdomen 5mm”)  
> > > volume.shape  
> > > (118, 512, 512)

 ![binary](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/e/ce7a4b281f5fa80a0b0e9dd2e856dadd5546f16b.jpeg)

---

<div class="post-metadata">

**Author:** ![SummerZ2020](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/summerz2020/32/7137_2.png) [@SummerZ2020](https://discourse.slicer.org/u/SummerZ2020)\
**Post date:** [June 25, 2020, 2:37pm UTC](https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202/8 "2020-06-25T14:37:11Z")

</div>

I found if I save the segmentation to nrrd file first, and then load it and volume again. and then the labelmap created from the segmentation is bounding box size. If I save the segmentation directly after segmentation, the labelmap size is same as the volume(512\*512).

---

<div class="post-metadata">

**Author:** ![SummerZ2020](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/summerz2020/32/7137_2.png) [@SummerZ2020](https://discourse.slicer.org/u/SummerZ2020)\
**Post date:** [June 25, 2020, 2:57pm UTC](https://discourse.slicer.org/t/how-to-get-segmentations-as-numpy-arrays-with-same-dimensions/12202/9 "2020-06-25T14:57:14Z")

</div>

> [@JanWitowski](#):
>
> `loadedSegmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(loadedVolumeNode)`

Your solution works greatly. Thanks a lot. 👍  
BTW, do you know how to get the node name (like loadSegmentationNode and loadedVolumeNode in your code) if I load the volume and segmentation from GUI. thanks again.
