点云—>Mesh模型
- 1.介绍
- 1.1 背景
- 1.2 效果示意
- 2 算法实现
- 2.1 依赖库
- 2.2 实验数据
- 2.3 代码实现
- 2.4 实验效果
- 3.总结
1.介绍
1.1 背景
(1)本文主要内容是将三维点云(离散的三维点)进行表面重建生成Mesh网格,之前有篇关于开源软件-Cars-Mesh使用,它是对开源软件-Cars使用生成的点云进行处理得到Mesh网格结构,由于使用cars-mesh需要的配置文件较多,深入其内部涉及到点云mesh构建部分,得出如下结论:
cars-mesh主要有三种mesh构建方法:
- 泊松表面重建(poisson_reconstruction)
- Delaunay 三角剖分(delaunay_2d_reconstruction)
- ball_pivoting_reconstruction
此外还有移动立方体(Marching Cubes Algorithm)、贪婪投影三角化(Greedy Triangulation)等方法。
(2)由于基于卫星影像生成的建筑物点云往往只有建筑物屋顶点云,建筑物立面几乎没有点云,因此充分考虑这种特点,选取了Delaunay三角剖分的方法进行重建,能够保持建筑物立面垂直以及屋顶有棱有角。
1.2 效果示意
如下效果是在meshlab中呈现的:
2 算法实现
2.1 依赖库
本算法依赖三维点云处理库open3d以及在二维上进行三角剖分的Delaunay实现函数,这里在scipy和matplotlib均有实现,本文选择了scipy中的。
2.2 实验数据
vertices.ply,其只包含点的xyz信息,点云对应的颜色无。实验数据见资源绑定,包含原始点云和mesh构建后的数据,效果在cloudcompare中按照高程渲染效果如下:
2.3 代码实现
import open3d as o3d
import numpy as np
from scipy.spatial import Delaunay
import matplotlib.tri as mtri
class Mesh:
def __init__(self, vertices, triangles, vertex_colors=None):
self.vertices = vertices
self.triangles = triangles
self.vertex_colors = vertex_colors
def delaunay_2d_reconstruction(pcd_file: str, method: str = "scipy") -> Mesh:
"""
2.5D Delaunay triangulation: Delaunay triangulation on the planimetric
points and add afterwards the z coordinates.
Parameters
----------
pcd_file: str
Path to the PLY file containing point cloud data.
method: str, default='scipy'
Method to use for Delaunay 2.5D triangulation. Available methods are
'scipy' and 'matplotlib'.
Returns
-------
mesh: Mesh
Mesh object containing vertices, triangles, and vertex colors.
"""
# Load point cloud from PLY file
pcd = o3d.io.read_point_cloud(pcd_file)
# Get points, colors, and z coordinates from point cloud
points = np.asarray(pcd.points)[:, :2] # Project points to XY plane
colors = np.asarray(pcd.colors)
# Perform 2D Delaunay triangulation
if method == "scipy":
mesh_data = Delaunay(points)
elif method == "matplotlib":
mesh_data = mtri.Triangulation(points[:, 0], points[:, 1])
# Construct mesh
mesh_vertices = np.hstack([points, np.zeros((len(points), 1))])
mesh_triangles = mesh_data.simplices
# Set z coordinates based on the original point cloud
z_coordinates = np.asarray(pcd.points)[:, 2]
mesh_vertices[:, 2] = z_coordinates
# Create Mesh object with vertex colors
mesh = Mesh(mesh_vertices, mesh_triangles, vertex_colors=colors)
return mesh
def save_mesh_as_ply(mesh: Mesh, filename: str):
"""
Save mesh as a PLY file.
Parameters
----------
mesh: Mesh
Mesh object containing vertices, triangles, and vertex colors.
filename: str
Path to save the PLY file.
"""
# Create Open3D TriangleMesh object
mesh_o3d = o3d.geometry.TriangleMesh()
mesh_o3d.vertices = o3d.utility.Vector3dVector(mesh.vertices)
mesh_o3d.triangles = o3d.utility.Vector3iVector(mesh.triangles)
# Set vertex colors
if mesh.vertex_colors is not None:
mesh_o3d.vertex_colors = o3d.utility.Vector3dVector(mesh.vertex_colors)
# Save TriangleMesh object to PLY file
o3d.io.write_triangle_mesh(filename, mesh_o3d)
# Example usage:
pcd_file = "vertices.ply"
method = "scipy" # or "matplotlib"
mesh = delaunay_2d_reconstruction(pcd_file, method)
save_mesh_as_ply(mesh, "vertices_result_mesh.ply")
2.4 实验效果
整体效果在前面已经有呈现了,以下呈现几栋比较高的建筑效果:首先是mesh网格结构:
3.总结
整体而言,针对基于卫星影像生成的点云,Delaunay 三角剖分mesh构建效果良好:
(1)mesh重构本质上还是依赖于点云生成效果好坏,Delaunay 三角剖分在高建筑效果比较突出,但在低矮建筑效果差一些;
(2)在建筑物楼顶棱角细节层面以及与地面接触的部分有待进一步优化;
(3)TODO:尝试更多的mesh重构方法以及优化(2)