# Convert .seg.nrrd to nrrd labelmap

**URL:** <https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838>\
**Category:** Support\
**Tags:** segmentation, python\
**Created:** [May 28, 2021, 12:34pm UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838 "2021-05-28T12:34:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![siyuan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/siyuan/32/11091_2.png) [@siyuan](https://discourse.slicer.org/u/siyuan)\
**Post date:** [May 28, 2021, 12:34pm UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/1 "2021-05-28T12:34:25Z")

</div>

Hi, I was wondering how I could convert .seg.nrrd to a nrrd labelmap using pure python.  
Here is an attempt from my friend. It works but it may throw a broadcasting error sometime.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/3/636cb1ee4a9ec55c0ce4d748bd0c4cb46754804e.png)

Thanks a lot.

---

<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:** [May 28, 2021, 9:37pm UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/2 "2021-05-28T21:37:08Z")

</div>

Good news: segmentation is already a labelmap. You can use any nrrd file reader. There is an [example in the script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-information-from-segmentation-nrrd-file-header) of how to extract information about the segments from the custom fields in the header. This script is usable in any Python environment, not just in Slicer’s.

I’m in the process of creating a standalone Python package (that can be pip installed into any Python environment) for convenient reading/writing of Slicer-specific file formats (segmentations, markups, etc.) from Python, but it will probably take a week or so to become available.

---

<div class="post-metadata">

**Author:** ![siyuan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/siyuan/32/11091_2.png) [@siyuan](https://discourse.slicer.org/u/siyuan)\
**Post date:** [May 29, 2021, 4:02am UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/3 "2021-05-29T04:02:57Z")

</div>

Thank you for reply, and this will help me a lot. 😃 😃

---

<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:** [May 29, 2021, 4:48am UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/4 "2021-05-29T04:48:29Z")

</div>

I created a first version of the `slicerio` package. It can read and write .seg.nrrd files:

> **[slicerio](https://pypi.org/project/slicerio/)**
>
> Utilities for 3D Slicer

Please try it and let me know if you find it useful or if you need more features.

---

<div class="post-metadata">

**Author:** ![siyuan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/siyuan/32/11091_2.png) [@siyuan](https://discourse.slicer.org/u/siyuan)\
**Post date:** [August 1, 2021, 5:11pm UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/5 "2021-08-01T17:11:06Z")

</div>

For those who still use old-version Slicer:  
Python package pynrrd can read the header of a .seg.nrrd and you will find some ‘extend’ keywords. These keywords define the local-region of label and what u need to do is to map it back to reference size.  
For exmaple, in the following picture: the Segmentation\_ReferenceImageExtentOffset 344 257 12 means your segmentation start from 344 in x, 257 in y, 12 in z and then based on the size of segmentation array, you can easily map it to reference volume size. Code will be uploaded soon.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/a/eab8a1f2da6631b9236daabf34ba9d09dcbf3324.png)

---

<div class="post-metadata">

**Author:** ![fghazouani](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@fghazouani](https://discourse.slicer.org/u/fghazouani)\
**Post date:** [December 14, 2021, 3:55pm UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/6 "2021-12-14T15:55:37Z")

</div>

Hello @lassoan  
I manage to use the slicerio package to read a .seg.nrrd file and to get the number of segments.  
My question know how I can display each segment (label) from the extracted segment information.

Thank you

---

<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:** [December 14, 2021, 5:32pm UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/7 "2021-12-14T17:32:18Z")

</div>

It depends on what software would you like to use for image display. For very primitive visualizations you can use matplotlib (you can then just use `slicerio.extract_segments` for each segment), but for proper evaluation of 3D segmentations, multiple segments, overlay on the input image, in 2D and 3D, all in correct physical space, etc. you would probably much better off using Slicer. You can use Slicer as a Jupyter notebook kernel if you prefer using Jupyter notebooks instead of the Slicer GUI.

---

<div class="post-metadata">

**Author:** ![fghazouani](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@fghazouani](https://discourse.slicer.org/u/fghazouani)\
**Post date:** [December 15, 2021, 8:21am UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/8 "2021-12-15T08:21:18Z")

</div>

Thank you @lassoan for your answer.  
I know that Slicer is the best tool to display segments and overlay on the input image, what I need is to do the same thing, i.e., get segment by segment and not all overlayed on the image.  
I have succeed by using SimpleITK to overlay segments with the input image, but now i want to use `slicerio.extract_segments` to extract each segment separately like in the figure below.  
I know that `slicerio.extract_segments` provide a voxel arrays, (for example with shape [512, 512, 170]), but how I can use `slicerio.extract_segments` for each segment from this voxel arrays.

 ![segments](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/6/96fdfac16956b63ebbbefd9de175a7383cbc4c00.png)

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/1/21f05b67268d3575dbf863ad79bd4fe52c3fc4fa.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:** [December 16, 2021, 6:00am UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/9 "2021-12-16T06:00:28Z")

</div>

You can get voxels of a single segment into `segment_voxel` numpy array like this:

```python
input_filename = "path/to/Segmentation.seg.nrrd"
segment_name = "ribs"
label_value = 1

import slicerio
segmentation_info = slicerio.read_segmentation_info(input_filename)
voxels, header = nrrd.read(input_filename)
segment_voxels, segment_header = slicerio.extract_segments(voxels, header, segmentation_info, [(segment_name, label_value)])

```

---

<div class="post-metadata">

**Author:** ![fghazouani](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@fghazouani](https://discourse.slicer.org/u/fghazouani)\
**Post date:** [December 16, 2021, 3:42pm UTC](https://discourse.slicer.org/t/convert-seg-nrrd-to-nrrd-labelmap/17838/10 "2021-12-16T15:42:50Z")

</div>

Hello @lassoan,

Thank you very much for your help.  
It work very well now!
