如何从含verts与faces的3D Mesh提取骨架?求Python实现参考
在Python中提取3D网格骨架线的可行方案
直接调用CGAL的Python绑定
CGAL的Surface_mesh_skeletonization模块有官方Python绑定,可直接实现你需要的功能:
- 安装依赖:
pip install cgal(部分平台需提前配置C++编译环境) - 示例代码:
from CGAL.CGAL_Surface_mesh_skeletonization import extract_outer_skeleton from CGAL.CGAL_IO import read_polygon_mesh # 读取OBJ格式的3D网格 mesh = read_polygon_mesh("your_mesh.obj") # 提取外骨架 skeleton = extract_outer_skeleton(mesh) # 遍历骨架节点与边 for v in skeleton.vertices(): pt = skeleton.point(v) print(f"Skeleton vertex: ({pt.x()}, {pt.y()}, {pt.z()})") for e in skeleton.edges(): print(f"Skeleton edge: {e.source()} -> {e.target()}")
替代方案:Open3D库
Open3D提供基于中轴变换的骨架提取,虽实现细节与CGAL不同,但能覆盖多数场景:
- 安装:
pip install open3d - 示例代码:
import open3d as o3d # 加载网格并计算法向量 mesh = o3d.io.read_triangle_mesh("your_mesh.obj") mesh.compute_vertex_normals() # 生成骨架 skeleton = mesh.create_mesh_skeleton() # 输出骨架信息 print(f"骨架节点数: {len(skeleton.vertices)}") print(f"骨架边数: {len(skeleton.edges)}") # 可视化网格与骨架 o3d.visualization.draw_geometries([mesh, skeleton])
替代方案:PyVista库
PyVista封装了VTK的骨架提取逻辑,操作更简洁:
- 安装:
pip install pyvista - 示例代码:
import pyvista as pv # 读取网格 mesh = pv.read("your_mesh.obj") # 提取骨架 skeleton = mesh.extract_skeleton() # 查看骨架数据 print(skeleton) # 可视化 plotter = pv.Plotter() plotter.add_mesh(mesh, opacity=0.3) plotter.add_mesh(skeleton, color="red", line_width=4) plotter.show()
内容的提问来源于stack exchange,提问作者seeker
相关产品推荐
相关产品推荐

