Convert .seg.nrrd to nrrd labelmap

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

Thanks a lot.

Good news: segmentation is already a labelmap. You can use any nrrd file reader. There is an example in the script repository 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.

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

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

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

1 Like

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

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

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.

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

image

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

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)])
2 Likes

Hello @lassoan,

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