# Save segmentation with orig values as nii.gz when classes missing

**URL:** <https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504>\
**Category:** Support\
**Created:** [April 9, 2025, 3:29pm UTC](https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504 "2025-04-09T15:29:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deep\_Learning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/deep_learning/32/15345_2.png) [@Deep\_Learning](https://discourse.slicer.org/u/Deep_Learning)\
**Post date:** [April 9, 2025, 3:29pm UTC](https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504/1 "2025-04-09T15:29:54Z")

</div>

I have several binary masks saved as nii.gz. Let’s say that I have 3 classes 1,2 and 3. But 2 is missing. I want to load nii.gz into slicer as a segmentation along with the volume. And manually paint the second class. Then save as nii.gz. The new class should have value 2 and the other values should be unchanged.

When I load the file, the segmentation loads as Segment\_1 and Segment\_3. I add a segment. Slicer names it Segment\_2.  
All good up to this point…  
Now when I export to binary label mask and save as Newfile.nii.gz, I believe that my classes are mixed up. I load Newfile.nii.gz as a volume and mouse over. Now instead of 1,2,3 (or 1,3,4) I have 1,3,2. Class 3 has a value =2 and Class 2 has a value=3.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [April 9, 2025, 3:32pm UTC](https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504/2 "2025-04-09T15:32:21Z")

</div>

Slicer flattens the segmentations into labelmap in the order your segments are. So if your segment order is like:

Segment\_1  
Segment\_3  
Segment\_2

Segment\_2 will get index of 3.

If you want to get it index of 2, move one segment up before you export. Or better yet, use a color table that defines the indices, that way order doesn’t matter.

---

<div class="post-metadata">

**Author:** ![Deep\_Learning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/deep_learning/32/15345_2.png) [@Deep\_Learning](https://discourse.slicer.org/u/Deep_Learning)\
**Post date:** [April 9, 2025, 3:57pm UTC](https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504/3 "2025-04-09T15:57:55Z")

</div>

I tried moving one up. I do not think that it changed anything. The problem is that I have a bunch of classes and more than one are missing (sometimes).

Is it possible to load a segmentation and then save it with the same values? Assuming some are missing?

My work around right now is to save only the new class. Then I can add that class into the orig file.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [April 9, 2025, 4:39pm UTC](https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504/4 "2025-04-09T16:39:46Z")

</div>

> [@Deep\_Learning](#):
>
> I do not think that it changed anything. T

How do you know? It does changes things.

If you want Slicer to export your labelmap with specific numbers, you should really use a color table. That gives you full control over what the indices will be.

> **[Colors — 3D Slicer documentation](https://slicer.readthedocs.io/en/latest/developer_guide/modules/colors.html#color-table-csv-file-format-csv)**

---

<div class="post-metadata">

**Author:** ![Deep\_Learning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/deep_learning/32/15345_2.png) [@Deep\_Learning](https://discourse.slicer.org/u/Deep_Learning)\
**Post date:** [April 9, 2025, 7:59pm UTC](https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504/5 "2025-04-09T19:59:38Z")

</div>

“move one segment up before you export.” This refers to the order in subject heirarchy? Or is it only the move up/down in the Segmentation Module. Want to check before I try again. I just dragged it up in the subject heirarchy. May not be the same thing… That was the first thing that I tried… That’s why I wrote "I do not think that it changed anything. "

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [April 9, 2025, 8:31pm UTC](https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504/6 "2025-04-09T20:31:43Z")

</div>

> [@Deep\_Learning](#):
>
> “move one segment up before you export.” This refers to the order in subject heirarchy? Or is it only the move up/down in the Segmentation Module.

In the `Segmentations` or `Segment editor` module. Right click on the segment you want to move up, and choose “Move Segment One Up” (or something like that) from the context menu. But SH should follow that.

---

<div class="post-metadata">

**Author:** ![Deep\_Learning](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/deep_learning/32/15345_2.png) [@Deep\_Learning](https://discourse.slicer.org/u/Deep_Learning)\
**Post date:** [April 10, 2025, 1:37pm UTC](https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504/7 "2025-04-10T13:37:07Z")

</div>

That works. But I would need also need to add empty segments.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [April 10, 2025, 2:50pm UTC](https://discourse.slicer.org/t/save-segmentation-with-orig-values-as-nii-gz-when-classes-missing/42504/8 "2025-04-10T14:50:17Z")

</div>

> [@Deep\_Learning](#):
>
> But I would need also need to add empty segments.

That’s because you want to skip indices for labels that are not there. As I said, if you want to have full control, use a color table.

It can go something like  
1 Segment\_1  
2 Segment\_2  
3 Segment\_3

And it doesn’t matter if you have if you didn’t have Segment\_1 and Segment\_2. Segment\_3 will get index number of 3.
