Convert binary mask from MATLAB to segmentation in 3DSlicer

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.

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.

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.

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).

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

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

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.

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 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) .

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?

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.

1 Like

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.

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

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

1 Like