# Foreground Masking via command line

**URL:** <https://discourse.slicer.org/t/foreground-masking-via-command-line/17059>\
**Category:** Support\
**Created:** [April 13, 2021, 11:11am UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059 "2021-04-13T11:11:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaoloZaffino](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/paolozaffino/32/81052_2.png) [@PaoloZaffino](https://discourse.slicer.org/u/PaoloZaffino)\
**Post date:** [April 13, 2021, 11:11am UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059/1 "2021-04-13T11:11:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [April 13, 2021, 5:33pm UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059/2 "2021-04-13T17:33:48Z")

</div>

Hi Paolo -

Can you elaborate on what you mean by Foreground Masking ?

Best,  
Steve

---

<div class="post-metadata">

**Author:** ![PaoloZaffino](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/paolozaffino/32/81052_2.png) [@PaoloZaffino](https://discourse.slicer.org/u/PaoloZaffino)\
**Post date:** [April 13, 2021, 8:19pm UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059/3 "2021-04-13T20:19:18Z")

</div>

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

Thanks,  
Paolo

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [April 13, 2021, 10:07pm UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059/4 "2021-04-13T22:07:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PaoloZaffino](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/paolozaffino/32/81052_2.png) [@PaoloZaffino](https://discourse.slicer.org/u/PaoloZaffino)\
**Post date:** [April 16, 2021, 7:16pm UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059/5 "2021-04-16T19:16:49Z")

</div>

Thanks @pieper, I got it !

---

<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:** [April 18, 2021, 1:10pm UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059/6 "2021-04-18T13:10:36Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Mask_volume_using_segmentation) in the script repository.

---

<div class="post-metadata">

**Author:** ![PaoloZaffino](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/paolozaffino/32/81052_2.png) [@PaoloZaffino](https://discourse.slicer.org/u/PaoloZaffino)\
**Post date:** [April 20, 2021, 12:00pm UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059/7 "2021-04-20T12:00:30Z")

</div>

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

---

<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:** [April 20, 2021, 12:47pm UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059/8 "2021-04-20T12:47:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![du5t](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/du5t/32/68489_2.png) [@du5t](https://discourse.slicer.org/u/du5t)\
**Post date:** [November 28, 2023, 5:42am UTC](https://discourse.slicer.org/t/foreground-masking-via-command-line/17059/9 "2023-11-28T05:42:22Z")

</div>

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 :

```python
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()

```
