# Trying to re-create model smoothing in python code.

**URL:** <https://discourse.slicer.org/t/trying-to-re-create-model-smoothing-in-python-code/30102>\
**Category:** Support\
**Tags:** segmentation, python, 3d-model\
**Created:** [June 18, 2023, 12:35pm UTC](https://discourse.slicer.org/t/trying-to-re-create-model-smoothing-in-python-code/30102 "2023-06-18T12:35:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Mcarthur](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adam_mcarthur/32/66454_2.png) [@Adam\_Mcarthur](https://discourse.slicer.org/u/Adam_Mcarthur)\
**Post date:** [June 18, 2023, 12:35pm UTC](https://discourse.slicer.org/t/trying-to-re-create-model-smoothing-in-python-code/30102/1 "2023-06-18T12:35:55Z")

</div>

Hey folks, right now I am converting .nii files into .ply files, and then trying to smooth them. This doesn’t seem to work.

Essentially, I want to do the same smoothing 3D Slicer does on segmentation when you turn them into a 3D Model. How would I re-create this in python code? Do I need to do a different conversion?

Even if I could get some information on what 3D Slicer is doing behind the scenes here that would be great.

What I have: [https://drive.google.com/uc?id=1cm0FsXB5ffoLB01HduRyDXK8UB7vkkFX](https://drive.google.com/uc?id=1cm0FsXB5ffoLB01HduRyDXK8UB7vkkFX)

What I want: [https://drive.google.com/uc?id=1F7e5A8L3UK4GOB8NERdwShcow-ghOVkz](https://drive.google.com/uc?id=1F7e5A8L3UK4GOB8NERdwShcow-ghOVkz)  
(Same thing but smoothed: ignore colours)

Thanks in advance :))

---

<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 18, 2023, 12:37pm UTC](https://discourse.slicer.org/t/trying-to-re-create-model-smoothing-in-python-code/30102/2 "2023-06-18T12:37:58Z")

</div>

The [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#segmentations) shows you examples of how to load an image file as segmentation and export smoothed closed surface representation to model node or files.

---

<div class="post-metadata">

**Author:** ![Adam\_Mcarthur](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adam_mcarthur/32/66454_2.png) [@Adam\_Mcarthur](https://discourse.slicer.org/u/Adam_Mcarthur)\
**Post date:** [June 19, 2023, 9:06pm UTC](https://discourse.slicer.org/t/trying-to-re-create-model-smoothing-in-python-code/30102/3 "2023-06-19T21:06:24Z")

</div>

Thanks, but I don’t think this is entirely what I want to do - I more want to replicate 3D Slicers behaviour using native python models.

I have tried things like this with no success. Any suggestions on what the actual step I am missing is?

```py
.....
import nibabel as nib
import numpy as np
from skimage import measure
from plyfile import PlyData, PlyElement

from scipy.ndimage import gaussian_filter

nii_file = "./test/test.nii"
model_file = "./test/final.ply"

nifti_file = nib.load(nii_file)
np_array = nifti_file.get_fdata() # type: ignore [attr-defined]

# Smoothing
smoothed = gaussian_filter(np_array, sigma=1)

# Apply zoom to increase the resolution of the 3D data
verts, faces, _, values = measure.marching_cubes(np_array, 0)
...

```

---

<div class="post-metadata">

**Author:** ![Eva91](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/eva91/32/79594_2.png) [@Eva91](https://discourse.slicer.org/u/Eva91)\
**Post date:** [February 28, 2025, 3:03pm UTC](https://discourse.slicer.org/t/trying-to-re-create-model-smoothing-in-python-code/30102/4 "2025-02-28T15:03:27Z")

</div>

I am actually in need of the same thing. Have you come up with a good solution? In that case, would you mind sharing ?
