# Convert binary mask from MATLAB to segmentation in 3DSlicer

**URL:** <https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [May 18, 2020, 9:54pm UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607 "2020-05-18T21:54:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![go-lauren](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/go-lauren/32/6890_2.png) [@go-lauren](https://discourse.slicer.org/u/go-lauren)\
**Post date:** [May 18, 2020, 9:54pm UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/1 "2020-05-18T21:54:04Z")

</div>

I have created binary masks for hrd/img scans in MATLAB, but I want to export them as segmentations in 3DSlicer.

I have tried saving the masks as a .nii file and adding data as labelmap, but nothing shows up.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [May 18, 2020, 11:23pm UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/2 "2020-05-18T23:23:22Z")

</div>

Are you sure you successfuly export the masks from Matlab? Did you try to open them with some other software (ITK-SNap, fiji)?

Looking at the error log (Ctrl+0) will probably while loading has failed.

---

<div class="post-metadata">

**Author:** ![go-lauren](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/go-lauren/32/6890_2.png) [@go-lauren](https://discourse.slicer.org/u/go-lauren)\
**Post date:** [May 19, 2020, 12:08am UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/3 "2020-05-19T00:08:32Z")

</div>

So I can load them as a volume (not labelmaps) into slicer and they will appear, but I want it to be a segmentation for the mri image.

It successfully loads as far as I know, but nothing appears. Currently, if I try importing as a segmentation, nothing shows up.

---

<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:** [May 19, 2020, 3:53am UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/4 "2020-05-19T03:53:31Z")

</div>

You can directly load nrrd and nifti files as segmentations in recent Slicer Preview Releases. In the “Add data” dialog choose “Segmentation” as description.

In older Slicer versions, you had to do this in two steps: load volume as labelmap volumes, then convert to segmentation (by right-clicking on it in Data module).

---

<div class="post-metadata">

**Author:** ![go-lauren](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/go-lauren/32/6890_2.png) [@go-lauren](https://discourse.slicer.org/u/go-lauren)\
**Post date:** [May 20, 2020, 6:23pm UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/5 "2020-05-20T18:23:22Z")

</div>

I don’t really see Segmentation as an option in the Add data dialogue. I’m using 3D Slicer 4.10.2

---

<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:** [May 20, 2020, 6:36pm UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/6 "2020-05-20T18:36:20Z")

</div>

As I wrote above, you need to use a recent Slicer Preview Release.

---

<div class="post-metadata">

**Author:** ![go-lauren](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/go-lauren/32/6890_2.png) [@go-lauren](https://discourse.slicer.org/u/go-lauren)\
**Post date:** [May 20, 2020, 10:06pm UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/7 "2020-05-20T22:06:12Z")

</div>

So actually I think the problem was that the scale of the binary mask and the scale of the image doesn’t match, but I’m not sure why because I just made sure they correspond pixel to pixel.

---

<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:** [May 20, 2020, 10:55pm UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/8 "2020-05-20T22:55:43Z")

</div>

Is the origin, spacing, and axis directions are the same in the image and labelmap? Nifti header is not human-readable (it is one of the issues with this format), but you can use for example [nibabel](https://dartbrains.org/features/notebooks/3_Introduction_to_NeuroimagingData_in_Python.html) to display file header content so that you can inspect it. If you don’t work with brain images then the best is to use a generic file format, such as nrrd (you can find nrrdread.m and nrrdwrite.m Matlab functions for reading/writing nrrd files [here](https://github.com/PerkLab/SlicerMatlabBridge/tree/master/MatlabCommander/commandserver)) .

---

<div class="post-metadata">

**Author:** ![go-lauren](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/go-lauren/32/6890_2.png) [@go-lauren](https://discourse.slicer.org/u/go-lauren)\
**Post date:** [May 20, 2020, 11:09pm UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/9 "2020-05-20T23:09:07Z")

</div>

I’m not sure what those would be, because I simply loaded the hdr/img into MATLAB and then created a binary mask based on every slice of the 3 dim. image it produced. Is there a way to convert hdr/img to nrrd?

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [May 20, 2020, 11:29pm UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/10 "2020-05-20T23:29:00Z")

</div>

Are you trying to load both the binary mask you created and the original volume into the Slicer? If so, you can try to copy and paste the image spacing from the original AnalyzeImage volume to your binary mask using the `Volumes`. It would help if you can provide screenshots.

Otherwise all we can say something seems to be lost along your matlab conversion.

---

<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:** [May 21, 2020, 12:05am UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/11 "2020-05-21T00:05:29Z")

</div>

> [@muratmaga](#):
>
> Otherwise all we can say something seems to be lost along your matlab conversion.

I agree that this is the most likely the root cause of the issue.

@go-lauren: When the image is read into Matlab, you need to store the image origin, position, and axis directions, too. It may be stored as a single 4x4 matrix or a 3x3 matrix and a vector (and in nift it might be also stored as a quaternion and a vector). When you write the processed image to file, you need to save this information to the image file. You can find how it is done for nrrd in usage examples in nrrdread.m and nrrdwrite.m.

---

<div class="post-metadata">

**Author:** ![go-lauren](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/go-lauren/32/6890_2.png) [@go-lauren](https://discourse.slicer.org/u/go-lauren)\
**Post date:** [May 21, 2020, 1:07am UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/12 "2020-05-21T01:07:50Z")

</div>

Yes, pasting the spacing worked! What can I do to make sure that the mask and the image have the same spacing?

---

<div class="post-metadata">

**Author:** ![go-lauren](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/go-lauren/32/6890_2.png) [@go-lauren](https://discourse.slicer.org/u/go-lauren)\
**Post date:** [May 21, 2020, 1:19am UTC](https://discourse.slicer.org/t/convert-binary-mask-from-matlab-to-segmentation-in-3dslicer/11607/13 "2020-05-21T01:19:12Z")

</div>

Never mind, I got it!! Thank you so much!
