Loading image and corresponding mask on Slicer from command line

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

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.

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.

1 Like

@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?

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

Thanks. I confirm that the following works:

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:

You can show a volume in slice views by calling slicer.util.setSliceViewerLayers.

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

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

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?

Sorry, outline/filling is not defined in labelmap display node but in vtkMRMLSliceNode.

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

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

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

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

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