Foreground Masking via command line

Hi all,
I would need an automatic way of running Foreground Masking via command line.

As far as I understood it is not a cli module, so I need to load the image and run the mask generation via a python script.
However, I didn’t find a way for accessing its logic.

Any suggestion?
Thanks a lot.

Paolo

Hi Paolo -

Can you elaborate on what you mean by Foreground Masking ?

Best,
Steve

Hi Steve,
I mean this module:
https://www.slicer.org/wiki/Documentation/4.10/Modules/ForegroundMasking

Thanks,
Paolo

Hmm, I don’t recall that one, perhaps we deprecated it. But it looks like you can do the same thing with the segment editor pretty easily.

1 Like

Thanks @pieper, I got it !

If volume geometries are matching then you can also do masking (or combine volumes with various functions) using standard numpy array operations. See this example in the script repository.

Hi @lassoan
thanks for answering.
I was interested in generating the mask, so I think I’ll give a try to something different to foreground masking.

Paolo

Otsu (and about 6-8 other) automatic threshold computation methods are available in Threshold effect and morphological opening is available in Smoothing effect in Segment Editor. You can use these effects from Python as shown in examples in the script repository.

1 Like

Hello,

I had to process thousands of CTs recently, using batch processing method with python script.

This is the part of the script and I hope this might helpful :

roi = slicer.modules.brainsroiauto #Foreground Masking cli module
seg1 = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
params = {}
params['inputVolume'] = volume      
params['outputROIMaskVolume'] = seg1
params['outputClippedVolumeROI'] = 'None'
params['otsuPercentileThreshold'] = 0.01
params['ROIAutoDilateSize'] = 0
params['outputVolumePixelType'] = 'short'

slicer.cli.runSync(roi, None, params)

labelmapVolumeNode1 = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')
slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(seg1, labelmapVolumeNode1, volume)
slicer.util.saveNode(labelmapVolumeNode1, resultpath + 'ROI/' + file)

slicer.mrmlScene.Clear()