Trying to re-create model smoothing in python code.

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:

What I want:
(Same thing but smoothed: ignore colours)

Thanks in advance :))

The script repository shows you examples of how to load an image file as segmentation and export smoothed closed surface representation to model node or files.

1 Like

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?

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)