# Resample segments in segmentation using Python scripting

**URL:** https://discourse.slicer.org/t/resample-segments-in-segmentation-using-python-scripting/26411
**Category:** Development
**Tags:** segmentation
**Created:** [November 18, 2022, 11:57pm UTC](https://discourse.slicer.org/t/resample-segments-in-segmentation-using-python-scripting/26411 "2022-11-18T23:57:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![YousifAlKhoury](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yousifalkhoury/32/17402_2.png) [@YousifAlKhoury](https://discourse.slicer.org/u/YousifAlKhoury)
#### Post date: [November 18, 2022, 11:57pm UTC](https://discourse.slicer.org/t/resample-segments-in-segmentation-using-python-scripting/26411/1 "2022-11-18T23:57:46Z")

</div>

Is there a way to force the segments to the common image geometry through python code?

I am importing segmentations from a volumelabelmap and I can’t run ‘fill between slices’ on the imported segments.

---

<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: [November 20, 2022, 9:38pm UTC](https://discourse.slicer.org/t/resample-segments-in-segmentation-using-python-scripting/26411/2 "2022-11-20T21:38:51Z")

</div>

Fill between slices should resample all segments into the same geometry. How do you know that it does not?

You can force resampling by applying any operation on it (e.g., paint and erase a point) or saving and loading.

---

<div class="post-metadata">

### Author: ![YousifAlKhoury](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yousifalkhoury/32/17402_2.png) [@YousifAlKhoury](https://discourse.slicer.org/u/YousifAlKhoury)
#### Post date: [November 21, 2022, 7:02pm UTC](https://discourse.slicer.org/t/resample-segments-in-segmentation-using-python-scripting/26411/3 "2022-11-21T19:02:31Z")

</div>

Hi Andras,

I am trying to change the current segmentation node used by the segment editor programmatically. We have a drop-down menu to specify the rough mask (segmentation node) in our module.

We want to select another segmentation node and apply edits on that. However, let’s say for example my rough mask has a segment on slice 1 and 10. When I change to that rough mask and try to fill between slices, nothing happens. But if I apply any paint, even the tiniest bit, on those segments and run fill between slices, it works. So, I am guessing that’s what you meant by any edit forces resampling. However, this is quite time consuming if I have many slices to edit. Therefore, I was asking if this resampling can be done programmatically through python rather than manually applying an edit.

Here is the code I use to import a rough mask into the segment editor, we initially have the rough mask as a label map that we convert to a segmentation node:

```auto
def changeRoughMask(self, roughMaskNode, masterVolumeNode, segmentEditor):
    segmentNode = slicer.mrmlScene.GetNodeByID(self._segmentNodeId)
    slicer.mrmlScene.RemoveNode(segmentNode)

    if (roughMaskNode and masterVolumeNode):
      segmentNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLSegmentationNode')
      segmentNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
      self._segmentNodeId = segmentNode.GetID()
      self.labelmapToSegmentationNode(roughMaskNode, segmentNode)
      # set segmentation node and master volume node in segmentation editor
      segmentEditor.setSegmentationNode(segmentNode)
      segmentEditor.setMasterVolumeNode(masterVolumeNode)
      # update viewer windows
      slicer.util.setSliceViewerLayers(background=masterVolumeNode,
                                      label= segmentNode,
                                      labelOpacity=0.5)
      slicer.util.resetSliceViews()

      return True
    return False

```

Thanks.

---

<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: [November 23, 2022, 10:09pm UTC](https://discourse.slicer.org/t/resample-segments-in-segmentation-using-python-scripting/26411/4 "2022-11-23T22:09:22Z")

</div>

This should resample all the segments in the segmentation using the current geometry:

```python
segmentationNode = getNode('Segmentation')
segGeomLogic = slicer.vtkSlicerSegmentationGeometryLogic()
segGeomLogic.SetInputSegmentationNode(segmentationNode)
segGeomLogic.SetSourceGeometryNode(segmentationNode)
segGeomLogic.CalculateOutputGeometry()
segGeomLogic.ResampleLabelmapsInSegmentationNode()

```
