# Loading in a segmentation generated outside slicer (on matlab)

**URL:** <https://discourse.slicer.org/t/loading-in-a-segmentation-generated-outside-slicer-on-matlab/3556>\
**Category:** Support\
**Tags:** segmentation, matlab, nrrd, file-import\
**Created:** [July 22, 2018, 10:50am UTC](https://discourse.slicer.org/t/loading-in-a-segmentation-generated-outside-slicer-on-matlab/3556 "2018-07-22T10:50:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nassir](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@Nassir](https://discourse.slicer.org/u/Nassir)\
**Post date:** [July 22, 2018, 10:50am UTC](https://discourse.slicer.org/t/loading-in-a-segmentation-generated-outside-slicer-on-matlab/3556/1 "2018-07-22T10:50:09Z")

</div>

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:

[https://drive.google.com/open?id=1vP9KQiKK9NHCrKB3G03fXsAV3JAclOdT](https://drive.google.com/open?id=1vP9KQiKK9NHCrKB3G03fXsAV3JAclOdT)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 22, 2018, 11:57am UTC](https://discourse.slicer.org/t/loading-in-a-segmentation-generated-outside-slicer-on-matlab/3556/2 "2018-07-22T11:57:13Z")

</div>

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](https://github.com/PerkLab/SlicerMatlabBridge/tree/master/MatlabCommander/commandserver) (or return it from a MATLAB function using [MatlabBridge](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Create_a_segmentation_from_a_labelmap_volume_and_display_in_3D)

---

<div class="post-metadata">

**Author:** ![Nassir](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@Nassir](https://discourse.slicer.org/u/Nassir)\
**Post date:** [July 22, 2018, 9:23pm UTC](https://discourse.slicer.org/t/loading-in-a-segmentation-generated-outside-slicer-on-matlab/3556/3 "2018-07-22T21:23:17Z")

</div>

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