# Interpreting segmentation nrrd files

**URL:** <https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641>\
**Category:** Support\
**Tags:** segmentation, nrrd\
**Created:** [December 12, 2017, 12:28am UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641 "2017-12-12T00:28:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcantor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dcantor/32/782_2.png) [@dcantor](https://discourse.slicer.org/u/dcantor)\
**Post date:** [December 12, 2017, 12:28am UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/1 "2017-12-12T00:28:37Z")

</div>

Hello,

I am segmenting prostates with Slicer and now I want to post-process my segmentations. So I read the nrrd segmentation files with my python code. I have a case where I have one file with one segmentation and two segments corresponding to two lesions. I notice that:

1. in the nrrd file the **data** is 4-d array where the first dimension seems to correspond to the segment in a multi-segment segmentation. (is that the case?)

2. The header **dimension** header is now 4 (instead of 3) I am assuming that is because each segment/lesion is stored in its own array.

3. The **spaces direction** header is an array with 4 elements now, whereas the first element of the array is ‘none’ and the other three elements are 3-d vectors. I assume this is to match the fact that data is now a 4-d matrix (is that the case?)

4. The **sizes** header is also 4-d and it seems that the first coordinate corresponds to the number of segments and the other three are the minimum common cube that contain the segments.

Are my assumptions correct?

Thanks,

D

---

<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 12, 2017, 1:04am UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/2 "2017-12-12T01:04:27Z")

</div>

Yes, all the above are correct. Segmentation is stored in a 4D array (segment index and 3 spatial dimensions) because segments may overlap.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [December 12, 2017, 1:50am UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/3 "2017-12-12T01:50:53Z")

</div>

In case you haven’t seen it: [pynrrd](https://github.com/mhe/pynrrd).

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [December 13, 2017, 5:18pm UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/4 "2017-12-13T17:18:50Z")

</div>

If you know the segments don’t overlap you could export to a labelmap (to store as a 3D nrrd or nifti which is sometimes more compatible with other software).

---

<div class="post-metadata">

**Author:** ![tbillah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tbillah/32/4139_2.png) [@tbillah](https://discourse.slicer.org/u/tbillah)\
**Post date:** [June 14, 2019, 7:59pm UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/5 "2019-06-14T19:59:29Z")

</div>

Let’s look at the following key value pair for a segment NRRD header:

Segment0\_Extent:=7 110 376 473 383 572

What does the above key value pair mean? 7 should be the 7th volume, but what do the following numbers mean?

Again, what does the following imply?

Segmentation\_ReferenceImageExtentOffset:=114 108 88

Is the Offset in IJK or world coordinates?

Reference to any TUTORIAL would be helpful.

---

<div class="post-metadata">

**Author:** ![tbillah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tbillah/32/4139_2.png) [@tbillah](https://discourse.slicer.org/u/tbillah)\
**Post date:** [June 14, 2019, 8:13pm UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/6 "2019-06-14T20:13:02Z")

</div>

Thanks for @fedorov. I guess part of the answer is [here](https://vtk.org/doc/nightly/html/classvtkImageData.html#a9cfc94cd8d5c96656222b1ec0f3595c5) .

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [June 14, 2019, 8:18pm UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/7 "2019-06-14T20:18:41Z")

</div>

The segmentation 4D nrrd file is considered Slicer-specific, to enable single-file export/import of whole labelmap based segmentations. So there are no tutorials, because we don’t intend this to be used directly, but through the Slicer storage infrastructure.

The extent does not start at 0 because segments do not store the whole reference image geometry, only the non-empty part of the segment labelmap (“effective extent”).  
(The vtkImageData::Extent variable that you linked just now explains what an extent is, but it does not explain the effective extent used for segments)

I suggest that instead of trying to read this 4D nrrd, you export the segmentation as a labelmap within Slicer and save that in a usual 3D nrrd. Of course any overlap information will be lost (if you’re concerned about this you can export the segments in individual labelmaps too with some python scripting).  
There are many topics describing how to do this programmatically, such as [this](https://discourse.slicer.org/t/export-segmentation-to-labelmap-using-python/6801) or [this](https://discourse.slicer.org/t/get-label-map-node-from-segmentation-node/1137). If you want to do this from the UI then here’s the easiest way  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/3/b310ff8dc8ffe9c4a910511c42c4e70e99cf2fdc.png)  
but you can find more options in the Segmentations module

---

<div class="post-metadata">

**Author:** ![tbillah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tbillah/32/4139_2.png) [@tbillah](https://discourse.slicer.org/u/tbillah)\
**Post date:** [June 14, 2019, 8:24pm UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/8 "2019-06-14T20:24:43Z")

</div>

> [@cpinter](#):
>
> I suggest that instead of trying to read this 4D nrrd

Thanks. I am loading the segmentation file (i.e. header) in Python. I need to apply transform (warp/affine) on your segmentation file which is why understanding each key-value pair is important to me.

In case missed, what does the following mean? Is it in IJK?

> Segmentation\_ReferenceImageExtentOffset:=114 108 88

---

<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 14, 2019, 8:29pm UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/9 "2019-06-14T20:29:45Z")

</div>

Most private metadata fields in the 4D .seg.nrrd file is for performance improvement only (so that we don’t need to recompute effective extents of frames), so you can safely ignore them. But probably we should document them somewhere anyway. I’ll specify those fields and add a link here.

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [June 14, 2019, 8:31pm UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/10 "2019-06-14T20:31:36Z")

</div>

> [@lassoan](#):
>
> But probably we should document them somewhere anyway.

I suggest it makes sense to do it here:

[https://www.slicer.org/wiki/Documentation/4.10/SlicerApplication/SupportedDataFormat](https://www.slicer.org/wiki/Documentation/4.10/SlicerApplication/SupportedDataFormat)

---

<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 17, 2019, 5:57pm UTC](https://discourse.slicer.org/t/interpreting-segmentation-nrrd-files/1641/11 "2019-06-17T17:57:31Z")

</div>

I’ve added description here: [https://github.com/Slicer/Slicer/blob/e7d91177d4873dfde90e47df336be8487bff6c5d/Libs/MRML/Core/vtkMRMLSegmentationStorageNode.h#L56-L122](https://github.com/Slicer/Slicer/blob/e7d91177d4873dfde90e47df336be8487bff6c5d/Libs/MRML/Core/vtkMRMLSegmentationStorageNode.h#L56-L122)

I’ve added links from the [https://www.slicer.org/wiki/Documentation/Nightly/SlicerApplication/SupportedDataFormat](https://www.slicer.org/wiki/Documentation/Nightly/SlicerApplication/SupportedDataFormat) pages to the specification of the custom fields.
