# Updating Closed Surface Representation

**URL:** <https://discourse.slicer.org/t/updating-closed-surface-representation/11983>\
**Category:** Support\
**Created:** [June 10, 2020, 6:43pm UTC](https://discourse.slicer.org/t/updating-closed-surface-representation/11983 "2020-06-10T18:43:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [June 10, 2020, 6:43pm UTC](https://discourse.slicer.org/t/updating-closed-surface-representation/11983/1 "2020-06-10T18:43:43Z")

</div>

Heya! I am trying to work with the closed surface representation. Manually I can get the results I want. With code it seems as if some of the steps I want it to perform are lost or never occur.

**Steps**  
1. Create a segmentation, set name/color  
2. Using the SegmentationEditor set threshold min/max  
3. Closed Surface Representation (1.5x Oversampling, 0 joint smoothing, 0 smoothing factor, 0 decimation)  
4. Using the SegmentationEditor set median smoothing 2mm to this segmentation  
5. Update the Closed Surface Representation (1.5x Oversampling, 1 joint smoothing, 1 smoothing factor, 0.9 decimation)

Essentially, I want to smooth the representation that isn’t decimated. After smoothing, decimate it. I imagine there must be a way to update the surface representation, but I can’t seem to find the necessary snippet to do this. I would appreciate any and all direction on the approach

```
# Access segmentation module
slicer.util.selectModule('Segment Editor')
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(seriesVolumeNode)

# Create spine segment
segmentTypeID = "Spine"
newSegment = slicer.vtkSegment()
newSegment.SetName(segmentTypeID)
newSegment.SetColor([0.89, 0.85, 0.78])
segmentationNode.GetSegmentation().AddSegment(newSegment,segmentTypeID)

# Create segment editor widget to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)

# Access segment editor node
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
slicer.mrmlScene.AddNode(segmentEditorNode)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(seriesVolumeNode)

# Segment Editor Effect: Thresholding
segmentEditorWidget.setActiveEffectByName("Threshold")
thresholdEffect = segmentEditorWidget.activeEffect()
thresholdEffect.setParameter("MinimumThreshold","90")
thresholdEffect.self().onApply()
thresholdEffect.setParameter("MaximumThreshold","1600")
thresholdEffect.self().onApply()

# Create Closed Surface Representation
segmentationNode.GetSegmentation().SetConversionParameter("Oversampling factor", "1.5")
segmentationNode.GetSegmentation().SetConversionParameter("Joint smoothing", "0.00")
segmentationNode.GetSegmentation().SetConversionParameter("Smoothing factor", "0.00")
segmentationNode.GetSegmentation().SetConversionParameter("Decimation factor", "0.00")
segmentationNode.CreateClosedSurfaceRepresentation()

# Segment Editor Effect: Smoothing
segmentEditorWidget.setActiveEffectByName("Smoothing")
smoothingEffect = segmentEditorWidget.activeEffect()
# 2mm MEDIAN Smoothing
smoothingEffect.setParameter("SmoothingMethod", "MEDIAN")
smoothingEffect.self().onApply
smoothingEffect.setParameter("KernelSizeMm", 2)
smoothingEffect.self().onApply

# Update Closed Surface Representation
segmentationNode.GetSegmentation().SetConversionParameter("Oversampling factor", "1.5")
segmentationNode.GetSegmentation().SetConversionParameter("Joint smoothing", "1.00")
segmentationNode.GetSegmentation().SetConversionParameter("Smoothing factor", "1.00")
segmentationNode.GetSegmentation().SetConversionParameter("Decimation factor", "0.90")
# Update the smoothed closed surface representation some how
```

---

<div class="post-metadata">

**Author:** ![Queen\_Rei](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/queen_rei/32/6348_2.png) [@Queen\_Rei](https://discourse.slicer.org/u/Queen_Rei)\
**Post date:** [June 11, 2020, 3:49pm UTC](https://discourse.slicer.org/t/updating-closed-surface-representation/11983/2 "2020-06-11T15:49:58Z")

</div>

Any advice helps \<3 Even a pointer to where to find out what functions could update the surface representation would come a long way!

---

<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:** [June 11, 2020, 4:18pm UTC](https://discourse.slicer.org/t/updating-closed-surface-representation/11983/3 "2020-06-11T16:18:17Z")

</div>

> [@Queen\_Rei](#):
>
> I want to smooth the representation that isn’t decimated. After smoothing, decimate it.

It looks like your code already does what you need. Whenever you change the binary labelmap representation (smoothing, etc.), the binary labelmap representation is re-converted to closed surface representation (including smoothing and decimation).

For final export, you can do additional combination of smoothing and decimation using VTK filters (in just a few lines of code, using vtkQuadricDecimation, vtkDecimatePro, vtkWindowedSincPolyDataFilter etc.). Decimate CLI module has an additional FastQuadric method that you may try, too.
