bundles / skimage 0.26.1rc0.dev0+git20260530.b607368ff / skimage / measure / _marching_cubes_lewiner / marching_cubes
function
skimage.measure._marching_cubes_lewiner:marching_cubes
source: /dev/scikit-image/src/skimage/measure/_marching_cubes_lewiner.py :9
Signature
def marching_cubes ( volume , level = None , * , spacing = (1.0, 1.0, 1.0) , gradient_direction = descent , step_size = 1 , allow_degenerate = True , method = lewiner , mask = None ) Summary
Marching cubes algorithm to find surfaces in 3d volumetric data.
Extended Summary
In contrast with Lorensen et al. approach [2], Lewiner et al. algorithm is faster, resolves ambiguities, and guarantees topologically correct results. Therefore, this algorithm is generally a better choice.
Parameters
volume: (M, N, P) ndarrayInput data volume to find isosurfaces. Will internally be converted to float32 if necessary.
level: float, optionalContour value to search for isosurfaces in
volume. If not given or None, the average of the min and max of vol is used.spacing: length-3 tuple of floats, optionalVoxel spacing in spatial dimensions corresponding to numpy array indexing dimensions (M, N, P) as in
volume.gradient_direction: {'descent', 'ascent'}, optionalControls if the mesh was generated from an isosurface with gradient descent toward objects of interest (the default), or the opposite, considering the left-hand rule. The two options are: * descent : Object was greater than exterior * ascent : Exterior was greater than object
step_size: int, optionalStep size in voxels. Default 1. Larger steps yield faster but coarser results. The result will always be topologically correct though.
allow_degenerate: bool, optionalWhether to allow degenerate (i.e. zero-area) triangles in the end-result. Default True. If False, degenerate triangles are removed, at the cost of making the algorithm slower.
method: {'lewiner', 'lorensen'}, optionalWhether the method of Lewiner et al. or Lorensen et al. will be used.
mask: (M, N, P) array, optionalBoolean array. The marching cube algorithm will be computed only on True elements. This will save computational time when interfaces are located within certain region of the volume M, N, P-e.g. the top half of the cube-and also allow to compute finite surfaces-i.e. open surfaces that do not end at the border of the cube.
Returns
verts: (V, 3) arraySpatial coordinates for V unique mesh vertices. Coordinate order matches input
volume(M, N, P). Ifallow_degenerateis set to True, then the presence of degenerate triangles in the mesh can make this array have duplicate vertices.faces: (F, 3) arrayDefine triangular faces via referencing vertex indices from
verts. This algorithm specifically outputs triangles, so each face has exactly three indices.normals: (V, 3) arrayThe normal direction at each vertex, as calculated from the data.
values: (V,) arrayGives a measure for the maximum value of the data in the local region near each vertex. This can be used by visualization tools to apply a colormap to the mesh.
Notes
The algorithm [1] is an improved version of Chernyaev's Marching Cubes 33 algorithm. It is an efficient algorithm that relies on heavy use of lookup tables to handle the many different cases, keeping the algorithm relatively easy. This implementation is written in Cython, ported from Lewiner's C++ implementation.
To quantify the area of an isosurface generated by this algorithm, pass verts and faces to skimage.measure.mesh_surface_area.
Regarding visualization of algorithm output, to contour a volume named myvolume about the level 0.0, using the mayavi package
>>> >> from mayavi import mlab >> verts, faces, _, _ = marching_cubes(myvolume, 0.0) >> mlab.triangular_mesh([vert[0] for vert in verts], [vert[1] for vert in verts], [vert[2] for vert in verts], faces) >> mlab.show()
Similarly using the visvis package
>>> >> import visvis as vv >> verts, faces, normals, values = marching_cubes(myvolume, 0.0) >> vv.mesh(np.fliplr(verts), faces, normals, values) >> vv.use().Run()
To reduce the number of triangles in the mesh for better performance, see this example using the mayavi package.
See also
Aliases
-
skimage.measure.marching_cubes