# Loading image and corresponding mask on Slicer from command line

**URL:** <https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930>\
**Category:** Support\
**Created:** [January 23, 2020, 9:58pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930 "2020-01-23T21:58:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbillah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tbillah/32/4139_2.png) [@tbillah](https://discourse.slicer.org/u/tbillah)\
**Post date:** [January 23, 2020, 9:58pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/1 "2020-01-23T21:58:07Z")

</div>

Hi there,

Is there a command like:

> Slicer T1.nii.gz LabelMap=Mask.nii.gz

using which I can load an image and its mask (as LabelMap) directly from the command line?

Basically, I want to replicate the following GUI loading from the command line:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/f/2f4c3006533c53c64d85aa904c53cf0825defd7a.png)

Note that:

> Slicer T1.nii.gz Mask.nii.gz

will load two images as two foreground volumes which don’t serve the purpose I am interested in.

---

<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:** [January 24, 2020, 1:13am UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/2 "2020-01-24T01:13:25Z")

</div>

Deafult loader is used when you simply pass the file name.

You can use .label.nii.gz extension instead of just .nii.gz as a hint to get your volume loaded as labelmap (or .seg.nrrd to make it load as a segmentation).

If renaming the file is impossible then you can force reading as volume+labelmap by calling `Slicer.exe --python-code "slicer.util.loadVolume('/tmp/series/T1.nii.gz'); slicer.util.loadLabelVolume('/tmp/series/Mask.nii.gz')"` or as volume+segmentation by by calling `Slicer.exe --python-code "slicer.util.loadVolume('/tmp/series/T1.nii.gz'); seg=slicer.util.loadSegmentation('/tmp/series/Mask.nii.gz'); seg.CreateClosedSurfaceRepresentation()"`.

Segmentation has many advantages over simple labelmaps, for example, it is rendered in slice views a bit nicer (it shows a stronger outline and more transparent filling) it can be easily displayed in 3D view, you can sort&filter segments, associate with standard terminologies, etc.

---

<div class="post-metadata">

**Author:** ![tbillah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tbillah/32/4139_2.png) [@tbillah](https://discourse.slicer.org/u/tbillah)\
**Post date:** [January 24, 2020, 2:25pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/3 "2020-01-24T14:25:23Z")

</div>

@lassoan, the following does not load as you expected:

> Slicer T1.nii.gz Mask.label.nii.gz

Rather, it still loads them as two separate foreground volumes.

FYI, my Slicer is:  
`Slicer-4.10.2-linux-amd64/Slicer`

Are we missing something here?

---

<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:** [January 24, 2020, 2:48pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/4 "2020-01-24T14:48:49Z")

</div>

That’s right, `Slicer something.seg.nrrd` is loaded properly as segmentation but `something.label.nrrd` is still loaded as scalar volume (if you drag-and-drop something.label.nrrd then by default it is loaded as labelmap, but not when passing as command-line argument).

I’ll add .label.nii.gz and .label.nrrd as recognized composite file extensions to Slicer Preview Release, but until then you can use `--python-code` argument to force loading any volume as labelmap (regardless of filename extension).

---

<div class="post-metadata">

**Author:** ![tbillah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tbillah/32/4139_2.png) [@tbillah](https://discourse.slicer.org/u/tbillah)\
**Post date:** [January 24, 2020, 3:03pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/5 "2020-01-24T15:03:53Z")

</div>

Thanks. I confirm that the following works:

```auto
Slicer --python-code "slicer.util.loadVolume('T1.nii.gz');slicer.util.loadLabelVolume('Mask.nii.gz');
slicer.util.getNode('vtkMRMLSliceNodeRed').SetUseLabelOutline(1);
slicer.util.getNode('vtkMRMLSliceNodeYellow').SetUseLabelOutline(1);
slicer.util.getNode('vtkMRMLSliceNodeGreen').SetUseLabelOutline(1)"

```

By the way, can you give any shortcut for the last three lines? As you know, they produce the following view:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/3/a3d9f7bdd0075dd091492b293b5cada30ef82a66.jpeg)

---

<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:** [January 24, 2020, 3:16pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/6 "2020-01-24T15:16:07Z")

</div>

You can show a volume in slice views by calling [`slicer.util.setSliceViewerLayers`](https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/util.py#L322-L332).

---

<div class="post-metadata">

**Author:** ![tbillah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tbillah/32/4139_2.png) [@tbillah](https://discourse.slicer.org/u/tbillah)\
**Post date:** [January 24, 2020, 3:22pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/7 "2020-01-24T15:22:34Z")

</div>

With this option, there is no control for “toggle between showing label map volume with regions filled or outlined.”

---

<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:** [January 24, 2020, 3:31pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/8 "2020-01-24T15:31:29Z")

</div>

You can change labelmap display node properties to set region outline/filling, but I would recommend to load labelmap volumes as segmentation instead (due to the advantages described above).

---

<div class="post-metadata">

**Author:** ![tbillah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tbillah/32/4139_2.png) [@tbillah](https://discourse.slicer.org/u/tbillah)\
**Post date:** [January 24, 2020, 4:11pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/9 "2020-01-24T16:11:51Z")

</div>

> You can change labelmap display node properties to set region outline/filling

I am sure this is a good idea. But can you be more specific about how to do that?

---

<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:** [January 24, 2020, 4:43pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/10 "2020-01-24T16:43:56Z")

</div>

Sorry, outline/filling is not defined in labelmap display node but in [vtkMRMLSliceNode](http://apidocs.slicer.org/master/classvtkMRMLSliceNode.html#a86599c00bb51107b577133825ce67a6f).

---

<div class="post-metadata">

**Author:** ![tbillah](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tbillah/32/4139_2.png) [@tbillah](https://discourse.slicer.org/u/tbillah)\
**Post date:** [January 24, 2020, 4:50pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/11 "2020-01-24T16:50:23Z")

</div>

Yup, coming back to what I noted in first place. Use three lines like this:

```auto
slicer.util.getNode('vtkMRMLSliceNodeRed').SetUseLabelOutline(1);

```

So, no shortcut for setting the property for all views all together.

---

<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:** [January 24, 2020, 5:34pm UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/12 "2020-01-24T17:34:12Z")

</div>

It would be easy to add outline/fill as an option to setSliceViewerLayers (pull requests are welcome), which would simplify the startup script. You can also add utility function like `slicer.show('somevolume.nrrd','somelabel.nrrd')` to your `.slicerrc.py` file or in an extension.

However, labelmap volumes are becoming more and more used just as legacy infrastructure, so I would recommend to load them as segmentation (`slicer.util.loadSegmentation('somelabel.nrrd')`).

---

<div class="post-metadata">

**Author:** ![oguzcanbekar](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/oguzcanbekar/32/12174_2.png) [@oguzcanbekar](https://discourse.slicer.org/u/oguzcanbekar)\
**Post date:** [January 10, 2023, 7:34am UTC](https://discourse.slicer.org/t/loading-image-and-corresponding-mask-on-slicer-from-command-line/9930/13 "2023-01-10T07:34:40Z")

</div>

Hi @lassoan I really appreciate for your effort!

Related to this command I would like to ask something…

As you mention I am able to run my image and corresponding a segmentation file from terminal but I would like to ask How can I run same image with 2 diiferent corresponding segmentation files? Is it possible?

Thank you very much again.!
