How to convert multi region Segmentation.seg.nrrd into a set of binary label images?

Hello, friends.

I would like to ask your help for converting a 2D segmentation.seg.nrrd into a collection of 2D binary label images (one image per segmentation region).

I need to perform that by command line in python SimpleITK, or by creating a CLI modulo in C++ ITK.

There goes attached a test case.

Tanks,
Leo.

Segmentation.seg.nrrd (46.6 KB)

It looks like for the larger structures you can separate the color channels and then use bitwise operators to select each structure.

For instance, to get the green horizontal rectangle at the top, you can use the VectorIndexSelectionCast function to select the green channel, and then do a bitwise AND with 5. So it’d look something like this:

green_channel = sitk.VectorIndexSelectionCast(input_image, 1)
structure = green_channel & 5
1 Like

This is Slicer’s segmentation format. Why not use Slicer to export appropriate segment as a label map? You will have a nice UI with colors and segment names too.

1 Like

Yes. You’re right. My point is: I will have to do that same procedure over 300 images or more :expressionless:.

Besides, Slicer needs me to check and uncheck the structures I want in the label. Those structures overlap each other. It would make the process even more time-consuming.

I better find a practical way to get all the regions in a single label image per region.

All right. I did it. However, I am struggling with some issues;

I ) The overlapping structures do not show up. If they did, though, they would appear on top of some smaller structures. If you open the segmentation in 3D Slicer, you’ll get the point.

II ) I am getting always the same image, no matter what & operation I perform, and by the way, I am getting many structures in the final label, rather than just the Green one.

Take a look:
LoadedSegmentation

Many Thanks Again.

If the segmentations are consistently encoded the same way, @dchen’s suggestion should work. Otherwise some script inside Slicer should be your best bet.

1 Like

Yeah, you’re going to have to figure out the way Slicer is encoding the segmentations, which I’m not familiar with.

Like Dzenan says, you’re probably best off writing a script in Slicer. Then you should be able write another script that automatically runs the segmentation script on each of your 300 images. That way you can avoid any user interaction with Slicer.

1 Like

I see.
Thanks, @dzenanz and @dchen.
I am not familiar with writing 3D Slicer Scripts.

Any guideline anywhere?

Thanks again.

Start from some examples, and use interactive Python editor to discover methods of objects of interest.

You can use the slicerio Python package to conveniently access metadata and segments in a .seg.nrrd file in Python (in any Python environment, not just in 3D Slicer’s).

Currently, voxel arrays are provided as plain numpy arrays. If you work with SimpleITK images then you’ll need a few Python commands to create a SimpleITK image from the numpy array and the the nrrd’s space origin and space directions attributes. If you have those commands then it would be great if you could share it and then I would add an option for direct reading of segmentations into SimpleITK image.

If anything is unclear about how to use slicerio or need help with interpreting a .seg.nrrd file then let me know.

2 Likes

Thank you @Andras Lasso … I’ll give it a try on all those suggestions you gave me, and I’ll keep you guys posted here as dive into that.

Thanks again.

1 Like

It all work well.
Perfect lib for my needs. Just grateful for your help, @lassoan and the others.

Take a look on the extracted segment overlaid over the image:download

Minor plot tuning is need concerning the aspect ratio, but it is just perfect.

Thanks again.

2 Likes