# Marching cubes element size

Hi,

I’m using scikit-image’s “marching_cubes_lewiner” to find the vertices and faces for producing a trianlge/tetrahedral meshes from a stack of binary images.

`verts, faces, normals, values = marching_cubes_lewiner(stack, step_size=19)`

I was wondering if anyone knew of a method/package to simplify the output of the marching cubes algorithm so that I can have coarser meshes. I’ve of course tried adjusting the “step_size” parameter and 19 is the highest I can go before the algorithm breaks and I’m left with 3 small random cubes.

Hi @steersteer, you could apply a Gaussian filter to the 3d volume in order to smooth the surface, this will result in meshes with less vertices I think (please note that this method can change the topology of your surface, though, whereas changing the `step_size` parameter ensures to keep the same topology). What is your goal here: do you want to reduce the number of vertices of the mesh for an easier visualization, or is it something else?

1 Like

It’s for finite element analysis ultimately. Im creating tetrahedral meshes and I’m able to change the elements sizes etc for the internal elements with a python interface to Tetgen. I just haven’t found a way to affect the surface past setting the step size to 19 (seems to be the maximum possible for this model).
I’d like to have a mesh with homogeneous elements which I know I could probably do by reducing the internal elements to “match” the surface but this would be a far too fine mesh, plus it would be good to be able to adjust the element size for different samples

If I search for “mesh simplification Python”, I see a few interesting-looking packages, including https://github.com/sp4cerat/Fast-Quadric-Mesh-Simplification, https://github.com/HusseinBakri/3DMeshBulkSimplification, and https://github.com/PyMesh/PyMesh, (first two are MIT-licensed, last one I can’t find a license for) as well as this SO question: https://stackoverflow.com/questions/38197212/mesh-decimation-in-python. Do any of these solve your problem, @steersteer?

Ultimately, I would love to have one or more of these algorithms in scikit-image. @emmanuelle should we create an issue for this?

@jni I agree it would be a useful addition. In the past I’ve also used the VTK filters for this through Mayavi, in VTK the technique is called Mesh decimation. Below is an example using Mayavi

``````from mayavi import mlab
import numpy as np
x, y, z = np.ogrid[0:1:100j, 0:1:100j, 0:1:100j]
sphere = (x - 0.5)**2 + (y - 0.5)**2 + (z - 0.5)**2 < 0.4**2
sphere = sphere.astype(np.uint8) # mayavi cannot process bools
fig = mlab.figure()
contour = mlab.pipeline.contour(mlab.pipeline.scalar_field(sphere))
filtered_contour = mlab.pipeline.decimate_pro(contour)
# Parameter of decimate filter, value between 0 (no decimation) and 1
filtered_contour.filter.target_reduction = 0.9
display_surface = mlab.pipeline.surface(filtered_contour)
# get geometrical data from VTK, it's a bit of a pain to find how to extract the
# different kinds of information
data = filtered_contour.get_output_dataset()
points = data.points.to_array()
``````

You can also tune other parameters of the decimate filter through Mayavi’s GUI (click on the Mayavi icon as in the screenshot below). Note that mastering mayavi is a bit involved, but the advantage is that you get most of VTK’s algorithms in Python.

1 Like
1 Like

Thanks for your suggestions, I’ll give them a try. Unfortunately I know PyMesh is the perfect solution to all of my problems but it doesn’t seem to work with windows unless using Docker which I’d rather not do as I’d like an “all in one” solution with my python code (plus I don’t really know how to use docker).

Thanks, I’ve tried using VTK’s decimate and decimate_pro through PyVista’s interface but it results in very random triangles which is not really what I’m looking for because I’d like to have homogenous elements.

I’d like to have similar results to Meshlab’s “clustering decimation”

I’m having good results running the meshlab server from my code using https://github.com/3DLIRIOUS/MeshLabXML

As skimage is wriote in Cython, another choice is CGAL, a very powerful geometry lib.

Unfortunately, we cannot depend on CGAL, as it is licensed with GPL. Additionally, we don’t have any C++ dependencies as it would complicate our build chain too much. Ease of building from source is a key constraint for us.

1 Like