16-bit segmentations appear gray after save + reload

Hi!
I’m having an issue with using 16-bit label images in 3D slicer (saved as nrrd). I make these files externally with python or ImageJ then load them into 3D slicer - this works fine, and I can edit with the Segment Editor with no problems. When I save the file, close it and re-open though, all my segments appear gray, rather than their usual random colour maps. Also, this often results in 3D slicer not closing properly - with ‘3D Slicer 5.2.2’ remaining in the task manager in windows, and not releasing the RAM space.

Any ideas on how I can fix this? Thanks so much!

When you edit these with the Segment Editor, how are you saving them? If you are saving them as _seg.nrrd, you shouldn’t have this problem, as Slicer will load them as segmentations.

What is the master volume you are using with the Segment Editor. Can you provide a screenshot of your scene?

Yes - I am saving the segmenation as .nrrd. They still open as segmentations, but all the colours reset to gray. The other volume in the scene is a 16-bit X-ray image. Unfortunately I can’t share these images, but I had the same result some small example 16-bit data.

Here, I load a small 16-bit X-ray image (one of the samples that come with ImageJ) as a volume, and a 16-bit label image as a segmentation. The label image is all zeros, with some small example blobs of the max value (65535). This gives the first image - displaying in green with one segment. Saving and re-loading, gives the second image where the same segments are present, but now all gray.


A nrrd file can be interpreted many ways. You may want to use the values as grayscale values, or interpret the daata set as a transform (values specifiying displacements), as a segmentation (each discrete value referring to a segment), etc. If you use the default .seg.nrrd file extension then that gives a hint to Slicer to interpret the file as a segmentation. If you use a generic file extension (e.g., just .nrrd) then you need to give the hint in the “Add data” window by choosing “Segmentation” in the “Description” column (by default the file is interpreted as a grayscale image).

Note that ImageJ is a 2D image oriented software. It can handle stacks of 2D images, but that is very different from interpreting the data as one 3D volume. This not just limits what you can do with the images but it may also cause data corruption - as we can see on your screenshots, the z spacing is incorrect, because ImageJ did not store the distance between the frames of the image stack (but other corruptions are possible, too, for example the image maybe left-right flipped).

Thanks for the info @lassoan ! I tried using the .seg.nrrd extension, but it still resets to gray when I save and then open the scene .mrml file later. It’s still recognising it as a segmentation, and appearing properly in the segment editor, but the colour of all segments is set to a uniform gray. This doesn’t seem to happen with equivalent 8-bit data. It’s not a big issue though - for now I can just manually re-import the data and the segment colouring is fixed.

As for ImageJ, I only meant this as a quick example image, so I didn’t set the z spacing. (On my actual data this is set correctly!)

Is there a reason why you are using 16bit data for labelmaps/segmentations? 8 bit would accommodate 255 classes and is sufficient for most usage.

Slicer adjusts the scalar type automatically based on what values are in the segmentation, so I’m not sure how these 16-bit segmentations are created. @K-Meech can you clarify?

Please also share an example segmentation file (that you created of some publicly available data set) by uploading to somewhere (onedrive, dropbox, google drive, etc.) and posting the link here.

@muratmaga - I’m using 16-bit, as I have > 255 classes. It’s an instance segmentation, and there are a lot of instances!
@lassoan - I made these segmentations outside 3D slicer via some python scripts, so the segmentations are written via numpy + scikit-image. I’ll generate an example file next week when I’m back from vacation.

Sorry for the delay in getting back to you @lassoan - I’ve now made some example data which you should be able to download from dropbox: Dropbox - slicer-example - Simplify your life

In this folder there is a small example x-ray stack (as nrrd), and a rough 16-bit segmentation with 2 segments (as nrrd). In 3D slicer, I load the stack as a volume, and the segmentation as a segmentation. The 2 segments display correctly in different colours. I then save the scene .mrml file and close. Double clicking the scene file to re-open, shows the correct image and segments - but the colour of all segments is now reset to gray.

Let me know if you need any more info - thanks!


Thanks for the excellent instructions for reproducing this. The problem is that the segmentation file reader in Slicer generates segment colors automatically when the file is read from a non-segmentation NRRD file. However, it does not mark the segmentation node as modified, therefore by default saving the segmentation node is not offered by default when you save the scene.

All you need to do to fix this is to check the checkbox next to the segmentation node in the “Save data” window. An even better solution is to write the segment names, colors, and terminology in your segmentation file header.

I’ll fix the segmentation reader to automatically check this checkbox when colors are generated (and not stored in the segmentation file).

1 Like

Thanks @lassoan ! All working now.