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.
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:
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.
Yes. You’re right. My point is: I will have to do that same procedure over 300 images or more .
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.
If the segmentations are consistently encoded the same way, @dchen’s suggestion should work. Otherwise some script inside Slicer should be your best bet.
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.
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.
Hello Leo,
I have to do the same as you did with SimpleITK and slicerio packages to overlay image and segments. Unfortunately, I am new to both of these libraries, so I will be very grateful if you print that commands allowing to achieve the same result.