Segmentation mask

Hi Julien,

Hope you’re fine.
I have a question: If I want to give the data to sct and receive the segmentation mask as the output file (I expect something like a 3d output that is masked around the area of the spinal cord area). Is this possible?
As far as I know, to view the masked region, we should open the original file and add the segmentation mask produced after analysis to it, for observing the output segmentation on the image.
In summary, I want to know, is it possible to give the original image and receive the annotated image (masked image) as the output?

Thanks in advance, and regards.

Hi Maryam,

Thank you very much for your question. There are a number of SCT tools available to help, depending on which spinal cord region you would like to be masked:

  • sct_deepseg_sc: This can produce a precise segmentation for the spinal cord. An example is shown in red below:


  • sct_create_mask: This can produce a mask for the region surrounding the spinal cord. It also provides options that allow you to adjust the diameter of the mask. An example mask is shown in yellow below:

  • sct_crop_image: This last tool allows you to use a mask to crop the original file so that only the data inside the masked region is preserved.

Are any of these options close to what you are looking for? :slight_smile:

Kind regards,

Thanks so much Joshua,

The first one is actually what I mean by mask. But my concern is having this masked image as a 3D output, which possibly includes the segmentation mask over all slices/levels of the spinal cord; I need that masked output image (we can call it also annotations). So in the end, I can have an image and a masked version of that image (two NIfTI outputs possibly).

Best regards,

Thank you for the clarification!

I’m having some trouble understanding what you mean by “3D output”, as sct_deepseg_sc does produce a 3D mask:

(This image shows two example images: an output t2_seg.nii.gz image (in red), overlaid on top of the corresponding input anatomical image, t2.nii.gz)

(This image shows t2_seg.nii.gz by itself.)

If instead you are looking to use this mask to extract the spinal cord region from the original image, you can use sct_maths to multiply the two images together. For example:

sct_maths -i t2.nii.gz -mul t2_seg.nii.gz

That will give you the following image:

If I’ve misunderstood what you are looking for, please let me know. :slightly_smiling_face:

Kind regards,

Thanks, Joshua for your fast response.

Yes, the overlaid red mask on top of the original image, is what I mean. However, is saving them as a whole masked image (mask overlaid on the original image) possible? In other words, can I save these two (mask overlaid on top of the input) as one output image, that could be seen afterward in my computer without the need to add the mask to the corresponding input image every time?

Thanks very much :slight_smile:

Hi @Maryam,

I think is not possible to have the overlaided mask in red above the T2(or the structural image of your preference) in greyscale. Since they would be a single NIFTI image (per request) I believe they should follow the same colour scale. You should be able to produce manually the T2 image with the overlaided mask in white (or have them both within the same colour palette). It might be difficult to understand or identify then.

Another option, which needs knowledge of the viewer you use, would be to write down two lines of code that, when executed, load 2 different images, one in greyscale and another in red. I do not know if this option meets your requirements although.



1 Like

To expand on what @Aran has said, the red colour in the screenshot is not an inherent property of the NIfTI image, but is instead added by the image viewer (in this case, FSLeyes).

When I made my screenshot, I gave each image its own colour palette separately:

Screenshot from 2021-09-08 10-23-02

t2_seg.nii.gz is a binary mask, with values ranging from [0, 1], and was assigned a red colormap in the viewer.

Screenshot from 2021-09-08 10-23-24

t2.nii.gz instead has values ranging from [0, 4135.95], and was assigned a grayscale colormap in the viewer.

If these two images were to be combined into one image, it wouldn’t be possible to have different colormaps for each image.

Based on what you’ve said here, I want to double-check: Is the main problem that it is too tedious to visualize the segmentation (because you have to manually add the mask each time)?

If so, there are a few alternative ways to address this problem:

  1. Like @Aran has said, many viewers offer a way to open up images using a command. That can save some time so that you don’t have to manually open the mask each time. For example, for FSLeyes, you can run a command like this:

    fsleyes t2.nii.gz -cm greyscale t2_seg.nii.gz -cm red &

    This will automatically open both images together, with the red colormap applied to the segmentation mask.

    Then, you can also combine it with your sct_deepseg_sc command using &&:

    sct_deepseg_sc -i t2.nii.gz -c t2 && fsleyes t2.nii.gz -cm greyscale t2_seg.nii.gz -cm red &

    That way, the viewer will automatically open up after the sct_deepseg_sc command finishes.

  2. You may also want to consider using SCT’s “Quality Control (QC)” feature, by adding the -qc flag to your command like this:

    sct_deepseg_sc -i t2.nii.gz -c t2 -qc <output_folder_name_for_qc>

    The output QC report provides a more detailed view to let you inspect the segmentation slice-by-slice. And, it provides a table, so that you can switch between segmentations easily. (So, there is no need to manually open multiple images and multiple masks – it will all be on the same page.)

    Here is an example of what the QC interface looks like for cord segmentations:

I hope this is able to make things a little easier for you. Please let me know if there is anything else I can do to help. :slightly_smiling_face:

1 Like

Thanks, Aran and Joshua,

So, it does not sound possible, the white mask I think does not work well and the second option too, since I want to save the input and overlaid mask as one in the end.

P.S. the reason is not being tedious; I need to have both “masked and input image files” for my project.

Thanks to both of you.


Hi Maryam, I think it would help us if you would tell us why you need the masked and input image as a single NIfTI file. I really cannot think of a good reason to do that.

Hi Julien,

It’s for preparing the training dataset; I want to create a dataset including the masks and original images; I have previously tried some annotation softwares like ITK, but they were not accurate enough, and in some cases, it seems impossible to annotate the spinal cord for some MR modalities manually (specially wm and gm regions – however in T2s case, it’s a bit better since visually they have more visible borders to annotate).

Thanks very much and regards,

If you want to train a deep learning model, then you should not input in your training framework a single file with both the image content and the mask, but instead a pair of 3D images (the image and its associated ground truth mask). I strongly recommend you look at our other project ivadomed, which is a user-friendly framework to train deep learning models on MRI data. It includes tutorials, which will help you understand the format/type of required inputs.

Okay, thanks Julien, I will check it.
So, for making the masks/ground truth data, what software do you use? Because annotating of DWI, MT and T1 seem difficult to me, the only one that I found kind of visually easier to annatate, was T2s.

Any type of software will do (FSLeyes, ITKsnap, MITKWorkBench, 3D Slicer, etc.). If you have difficulties in visualizing the structure to make the manual segmentation, the problem is not the software but the image. One way to do it is to register your contrasts to the T2s image and then use the T2s-based segmentation as a ground truth for the other contrasts.

1 Like

Thank you very much. So for registration, I can first use SCT, and then annotate the registered image, right?


1 Like

Hi Julien,

By following the data preparation instruction, I face the below error after running the ‘sct_run_batch’ command.
My installed SCT version is 5.3.0 and the latest ivadomed.

sct_run_batch: error: unrecognized arguments:

Can you please guide me on what can be the reason? I followed the instruction here.

Thanks and regards,

Hi @Maryam

The syntax you used is obsolete. Please always refer to the latest SCT’s official documentation.

Thank you for reporting this issue (which will be fixed soon-- follow ticket here).

1 Like