简体   繁体   English

如何将 3D 点云 (.ply) 转换为网格(具有面和顶点)?

[英]How do I convert a 3D point cloud (.ply) into a mesh (with faces and vertices)?

I have a 3-D point cloud file with 1 million points that I need to convert into a mesh file in trimesh.我有一个包含 100 万个点的 3-D 点云文件,我需要将其转换为 trimesh 中的网格文件。 The ultimate goal here is to take a point cloud and determine if that point cloud is convex or concave (trimesh allows me to do that once i convert the cloud into a mesh).这里的最终目标是获取点云并确定该点云是凸的还是凹的(一旦我将点云转换为网格,trimesh 允许我这样做)。 I'm open to other libraries to solve this.我对其他图书馆开放来解决这个问题。

I've tried Delaunay triangulation using scipy, I just can't seem to convert my pointcloud into the right format so that it can be read by trimesh.我已经尝试使用 scipy 进行 Delaunay 三角剖分,我似乎无法将我的点云转换为正确的格式,以便它可以被 trimesh 读取。

import open3d as o3d
import numpy as np
import trimesh
from scipy.spatial import Delaunay


pointcloud = o3d.io.read_triangle_mesh("pointcloud.ply")
points = np.array(pointcloud.points)
triangle_mesh = Delaunay(points)
#  How do i include triangle_mesh from Delaunay triangulation into processing the mesh file?
mesh = trimesh.load("pointcloud.ply")
print(trimesh.convex.is_convex(mesh))

Error错误

geometry::TriangleMesh appears to be a geometry::PointCloud (only contains vertices, but no triangles).
geometry::TriangleMesh with 1390073 points and 0 triangles.
expected = (faces.shape[0], faces.shape[1] * 2)
AttributeError: 'NoneType' object has no attribute 'shape'

Open3d 0.8.0.0 has now implemented the rolling ball pivoting algorithm to reconstruct a mesh from a point cloud. Open3d 0.8.0.0 现在已经实现了滚球枢轴算法以从点云重建网格。

I solved the problem of generating a trimesh from a point cloud using the following:我使用以下方法解决了从点云生成网格的问题:

import open3d as o3d
import trimesh
import numpy as np

pcd = o3d.io.read_point_cloud("pointcloud.ply")
pcd.estimate_normals()

# estimate radius for rolling ball
distances = pcd.compute_nearest_neighbor_distance()
avg_dist = np.mean(distances)
radius = 1.5 * avg_dist   

mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(
           pcd,
           o3d.utility.DoubleVector([radius, radius * 2]))

# create the triangular mesh with the vertices and faces from open3d
tri_mesh = trimesh.Trimesh(np.asarray(mesh.vertices), np.asarray(mesh.triangles),
                          vertex_normals=np.asarray(mesh.vertex_normals))

trimesh.convex.is_convex(tri_mesh)

Just inquiring what this line of code actually does?只是询问这行代码实际上做了什么? It calls the function with point cloud, as pcd , but what does the double vector section mean?它调用带有点云的函数,如pcd ,但双向量部分是什么意思? - o3d.utility.DoubleVector([radius, radius * 2])) - o3d.utility.DoubleVector([radius, radius * 2]))

mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(
           pcd, o3d.utility.DoubleVector([radius, radius * 2]))

To save the mesh after following answer of https://stackoverflow.com/a/57971745/6394769 use trimesh export options:要在回答https://stackoverflow.com/a/57971745/6394769后保存网格,请使用 trimesh 导出选项:

trimesh.exchange.export.export_mesh(tri_mesh, 'output file path.ply .obj etc..')

https://trimsh.org/trimesh.exchange.ply.html https://trimsh.org/trimesh.exchange.ply.html

If you want to keep colours in trimesh, make this change:如果您想在 trimesh 中保留颜色,请进行以下更改:

tri_mesh = trimesh.Trimesh(....., vertex_colors = np.asarray(mesh.vertex_colors))

https://trimsh.org/trimesh.base.html https://trimsh.org/trimesh.base.html

Alternatively you can directly save mesh using open3d itself:或者,您可以使用 open3d 本身直接保存网格:

o3d.io.write_triangle_mesh('file path.ply or.obj etc..', mesh, write_ascii=True (if needed as text and not binary file), compressed=False, print_progress=False)

Mesh colour also saved here if present inside mesh variable.如果存在于网格变量中,网格颜色也保存在这里。

http://www.open3d.org/docs/release/python_api/open3d.io.write_point_cloud.html#open3d.io.write_point_cloud http://www.open3d.org/docs/release/python_api/open3d.io.write_point_cloud.html#open3d.io.write_point_cloud

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM