You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python基于存储灰度值的3D array构建3D立方体模型?

3D灰度体素数组转可渲染3D模型的Python实现方案

你的输入为索引对应体素空间坐标、值为灰度值的3D数组,可根据使用场景选择以下3种落地实现,从快速预览到生产级导出全覆盖:

方案1:Matplotlib+NumPy快速交互式预览

该方案无需安装重型3D依赖,适合快速验证数组内容、查看体素大致分布。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 替换为你自己的3D灰度数组,形状为(深度, 高度, 宽度),dtype建议为uint8
# gray_3d_arr = np.random.randint(0, 256, size=(32, 32, 32), dtype=np.uint8)

# 自定义显示阈值:灰度值低于阈值的体素视为透明空区
threshold = 50
voxel_mask = gray_3d_arr > threshold

# 将灰度值映射为RGBA颜色,保留原始灰度明暗
colors = np.zeros(voxel_mask.shape + (4,), dtype=np.float32)
gray_norm = gray_3d_arr / 255.0
colors[..., 0] = gray_norm  # R通道
colors[..., 1] = gray_norm  # G通道
colors[..., 2] = gray_norm  # B通道
colors[..., 3] = voxel_mask.astype(np.float32)  # A通道

# 启动渲染窗口
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
ax.voxels(voxel_mask, facecolors=colors, edgecolor=None)
ax.set_xlabel('X轴')
ax.set_ylabel('Y轴')
ax.set_zlabel('Z轴')
plt.show()
  • 优点:代码量极小,依赖为数据分析环境标配,支持鼠标拖拽旋转、缩放查看
  • 缺点:渲染性能差,数组尺寸超过646464时卡顿明显,无法导出通用3D模型文件,仅适合临时预览

方案2:PyVista生成高质量渲染+导出通用3D模型

该方案基于VTK封装,是科学可视化场景的主流选择,兼顾性能、效果和导出能力,适合生产环境使用。
安装命令:pip install pyvista

import numpy as np
import pyvista as pv

# 替换为你自己的3D灰度数组
# gray_3d_arr = np.random.randint(0, 256, size=(128, 128, 128), dtype=np.uint8)

# 将numpy数组包装为均匀体素网格
grid = pv.UniformGrid()
grid.dimensions = np.array(gray_3d_arr.shape) + 1  # 网格顶点数比体素数多1
grid.cell_data["gray_value"] = gray_3d_arr.flatten(order="F")  # 绑定灰度值到每个体素单元

# 阈值筛选实体部分,提取表面网格剔除空区,大幅降低渲染负载
surface_mesh = grid.threshold(value=(30, 255), scalars="gray_value").extract_geometry()

# 启动交互式渲染窗口
plotter = pv.Plotter()
plotter.add_mesh(surface_mesh, cmap="gray", show_scalar_bar=False)
plotter.show()

# 按需导出通用3D模型文件
# surface_mesh.save("gray_voxel_model.stl")  # STL格式,无颜色信息,适合3D打印
# surface_mesh.save("gray_voxel_model.glb")  # GLB格式,保留灰度颜色,可直接在Web端/引擎中加载
  • 优点:性能极强,可流畅渲染百万级体素数据,支持光照调整、切面查看、体绘制等高级功能,可导出全品类通用3D格式
  • 缺点:依赖VTK底层库,安装包体积约百兆级别

方案3:Open3D实现真实感渲染

该方案基于物理渲染管线,出图质感更贴近真实效果,适合制作展示类可视化结果。
安装命令:pip install open3d

import numpy as np
import open3d as o3d

# 替换为你自己的3D灰度数组
# gray_3d_arr = np.random.randint(0, 256, size=(48, 48, 48), dtype=np.uint8)

threshold = 60
# 初始化体素网格
voxel_grid = o3d.geometry.VoxelGrid.create_dense(
    origin=[0, 0, 0],
    voxel_size=1.0,
    width=gray_3d_arr.shape[0],
    height=gray_3d_arr.shape[1],
    depth=gray_3d_arr.shape[2],
    color=[1.0, 1.0, 1.0]
)
# 遍历为每个有效体素设置对应灰度颜色
for x in range(gray_3d_arr.shape[0]):
    for y in range(gray_3d_arr.shape[1]):
        for z in range(gray_3d_arr.shape[2]):
            val = gray_3d_arr[x, y, z]
            if val > threshold:
                voxel = o3d.geometry.Voxel()
                voxel.grid_index = [x, y, z]
                gray_val = val / 255.0
                voxel.color = [gray_val, gray_val, gray_val]
                voxel_grid.add_voxel(voxel)

# 启动渲染窗口
o3d.visualization.draw_geometries([voxel_grid])
  • 优点:渲染质感优秀,自带视角控制、后处理特效,支持点云、网格等多类3D数据处理
  • 缺点:原生体素接口遍历大数组效率偏低,更适合128128128尺寸以内的数组使用

实用提示:如果你的数组尺寸超过256256256,不建议直接渲染全量体素,可以先做降采样,或者调用Marching Cubes算法提取灰度等值面生成三角网格,渲染性能可提升1-2个数量级。

内容的提问来源于stack exchange,提问作者walter Wang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 16:06:24