备注
前往结尾 下载完整示例代码。或者通过 Binder 在浏览器中运行此示例。
Marching Cubes#
Marching cubes 是一种从三维体积中提取二维表面网格的算法。这可以被概念化为地形图或气象图上等高线的三维泛化。它通过遍历体积,寻找跨越感兴趣水平的区域来工作。如果找到这样的区域,则生成三角剖分并添加到输出网格中。最终结果是一组顶点和一组三角形面。
该算法需要一个数据量和一个等值面值。例如,在CT成像中,Hounsfield单位从+700到+3000代表骨头。因此,一个可能的输入将是一组重建的CT数据和值+700,以提取骨头或类似骨头密度的区域的网格。
此实现同样适用于各向异性数据集,其中体素间距在每个空间维度上不相等,通过使用 spacing 关键字参数来实现。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from skimage import measure
from skimage.draw import ellipsoid
# Generate a level set about zero of two identical ellipsoids in 3D
ellip_base = ellipsoid(6, 10, 16, levelset=True)
ellip_double = np.concatenate((ellip_base[:-1, ...], ellip_base[2:, ...]), axis=0)
# Use marching cubes to obtain the surface mesh of these ellipsoids
verts, faces, normals, values = measure.marching_cubes(ellip_double, 0)
# Display resulting triangular mesh using Matplotlib. This can also be done
# with mayavi (see skimage.measure.marching_cubes docstring).
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
# Fancy indexing: `verts[faces]` to generate a collection of triangles
mesh = Poly3DCollection(verts[faces])
mesh.set_edgecolor('k')
ax.add_collection3d(mesh)
ax.set_xlabel("x-axis: a = 6 per ellipsoid")
ax.set_ylabel("y-axis: b = 10")
ax.set_zlabel("z-axis: c = 16")
ax.set_xlim(0, 24) # a = 6 (times two for 2nd ellipsoid)
ax.set_ylim(0, 20) # b = 10
ax.set_zlim(0, 32) # c = 16
plt.tight_layout()
plt.show()
脚本总运行时间: (0 分钟 0.335 秒)