# Apply Laplacian smoothing using Python script

**URL:** <https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [March 27, 2018, 2:17pm UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438 "2018-03-27T14:17:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![anandmulay3](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anandmulay3](https://discourse.slicer.org/u/anandmulay3)\
**Post date:** [March 27, 2018, 2:17pm UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/1 "2018-03-27T14:17:31Z")

</div>

how can i set smoothing effect to laplacian filter type in scriptable module script ??

---

<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:** [March 27, 2018, 2:28pm UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/2 "2018-03-27T14:28:01Z")

</div>

Segment editor smoothing effect uses [vtkWindowedSincPolyDataFilter](https://www.vtk.org/doc/nightly/html/classvtkWindowedSincPolyDataFilter.html), which works better than [Laplacian filter](https://www.vtk.org/doc/nightly/html/classvtkSmoothPolyDataFilter.html). The main problem with Laplacian filter is that it reduces segment volume.

See this example how to apply Smoothing effect from Python script:

> <https://gist.github.com/lassoan/1673b25d8e7913cbc245b4f09ed853f9>

Change `SmoothingMethod` from `MEDIAN` to `JOINT_TAUBIN` to use vtkWindowedSincPolyDataFilter instead of median filter.

---

<div class="post-metadata">

**Author:** ![anandmulay3](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anandmulay3](https://discourse.slicer.org/u/anandmulay3)\
**Post date:** [March 28, 2018, 6:48am UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/3 "2018-03-28T06:48:48Z")

</div>

Ahh… I’m already using this one…  
Alright so this is the best smoothing technique …  
Thanks 🙂

Can i repeat same code of segmentation to add multiple segment in same model .

---

<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:** [March 28, 2018, 10:55am UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/4 "2018-03-28T10:55:38Z")

</div>

> [@anandmulay3](#):
>
> Can i repeat same code of segmentation to add multiple segment in same model

To fuse segments, you can use Logical operators effect.

You can merge segments (boundaries between them is kept) if you use the direct file export tool that has been added recently to the nightly build - see [Save segmentation directly to STL or OBJ files](https://discourse.slicer.org/t/save-segmentation-directly-to-stl-or-obj-files/2428)

You can merge models one by one using Merge Models module.

---

<div class="post-metadata">

**Author:** ![anandmulay3](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anandmulay3](https://discourse.slicer.org/u/anandmulay3)\
**Post date:** [March 29, 2018, 1:38pm UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/5 "2018-03-29T13:38:43Z")

</div>

thanks it works and gives nice output , but how can i do this by adding code into this script ?? [https://gist.github.com/lassoan/1673b25d8e7913cbc245b4f09ed853f9](https://gist.github.com/lassoan/1673b25d8e7913cbc245b4f09ed853f9)

and thanks for the support 🙂

---

<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:** [March 29, 2018, 3:40pm UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/6 "2018-03-29T15:40:44Z")

</div>

Could you describe what you would like to do? Develop a new interactive Slicer module that does this? Or apply this processing in a batch mode on a large number of images?

You may find this tutorial useful: [https://www.slicer.org/wiki/Documentation/Nightly/Training#Slicer4\_Programming\_Tutorial](https://www.slicer.org/wiki/Documentation/Nightly/Training#Slicer4_Programming_Tutorial)

---

<div class="post-metadata">

**Author:** ![anandmulay3](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anandmulay3](https://discourse.slicer.org/u/anandmulay3)\
**Post date:** [April 2, 2018, 6:12am UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/7 "2018-04-02T06:12:13Z")

</div>

I’m trying to make a batch file which will process the fbx file with different segmentation part in a single file.  
i’m already using your script to make segmentation and save output in fbx using fbx exporter, but now im using latest nightly build so i want use the export segmentation feature using script.

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:** [April 4, 2018, 5:54am UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/8 "2018-04-04T05:54:50Z")

</div>

You can use [slicer.modules.segmentations.logic().ExportSegmentsClosedSurfaceRepresentationToFiles(…)](http://apidocs.slicer.org/master/classvtkSlicerSegmentationsModuleLogic.html#a9578121589a3b5e20a05aa8dce33c642) method.

---

<div class="post-metadata">

**Author:** ![anandmulay3](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anandmulay3](https://discourse.slicer.org/u/anandmulay3)\
**Post date:** [April 4, 2018, 7:50am UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/9 "2018-04-04T07:50:23Z")

</div>

Thanks Its working perfectly… 🙂

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [August 23, 2021, 5:57am UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/10 "2021-08-23T05:57:52Z")

</div>

Hi Andras,  
Is the sinc filter in model maker the same as the segment editor?

Regards,  
Saima Safdar

---

<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:** [August 23, 2021, 12:04pm UTC](https://discourse.slicer.org/t/apply-laplacian-smoothing-using-python-script/2438/11 "2021-08-23T12:04:15Z")

</div>

There are a few differences.

Segment Editor performs the same joint smoothing algorithm as Model maker, but then converts back the result into binary labelmap representation, so that it can be appropriately visualized and further edited. In contrast, Model maker creates a model that is not editable anymore.

Segment Editor performs a slight additional anti-aliasing filtering during generation of the closed surface representation.

Segment Editor can handle overlapping segments, while the Model maker cannot
