Loading in a segmentation generated outside slicer (on matlab)

Hey guys! I’m trying to load a scalar volume that was exported out of slicer, “WHS_SD_rat_atlas_v2_original”, back in as a segmentation. The whole reason I am doing this is to avoid manually defining the segments.

To do so, I pulled together some matlab scripts I found online to make the matlab script “Copy_of_read_Atlas2” which loads the scalar volume together with a legend in the form of a .txt file. It begins to generate the segmentation file with header information embedded in the same file, similar to a segmentation that I exported out of slicer “1015Segmentation.seg”. There’s a loop in there that I have printing the information for each segment (line 169), but I have it set to 1 right now because this is a high-RAM demand script. I think it’s good to just get one segmentation going for now. Then there’s the loop that reassigns the scalar volume onto 4-D array for the segmentation (line 208, also set to 1). I think all that stuff works well.

I think the issue might have to do with datatypes? I don’t have a compsci background but I’m suspicious of all the "U"s that can be read (in matlab, line 25) off the bad generated segmentation. The generation of the data starts on line 225 in “Copy_of_read_Atlas2”. Could someone point it out?

Trying to load the bad segmentation into slicer crashes it. It won’t load on old versions of slicer (I’m using 4.8.1).

Here are all the files:


If you use a recent nightly build of Slicer then you can follow these steps:

  • save segmentation from MATLAB as nrrd file using nrrdwrite.m (or return it from a MATLAB function using MatlabBridge)
  • load the nrrd file into Slicer as labelmap volume (using slicer.util.loadLabelVolume method; if you use GUI to select and load the volume, in “Add data” dialog, click “Show options” and select labelmap option)
  • import the labelmap into a segmentation node as shown in this example

Thanks Andras! Those python interactor steps worked smoothly using the Nightly 4.9.0. Cheers.