一、open3d的安装与介绍
1.安装open3d
pip install open3d 或 pip3 install open3d
查看安装包:pip list
2.open3d简介?
二.案例:斯坦福兔子
1.生成点云
①不同角度扫描到的兔子的点云
以bun000为例,?可以看出生成的是一片,不是整个兔子
?
import open3d as o3d
import numpy as np
import os
print(os.getcwd())
print("Open3D read Point Cloud")
pcd=o3d.io.read_point_cloud(r"D:\python\Ads2021\bunny\data\bun000.ply")
print(pcd)
o3d.visualization.draw_geometries([pcd],width=800,height=600)
?②多角度点云拼在一起生成整个点云(有大量重复)
import open3d as o3d
import numpy as np
import os
print(os.getcwd())
print("Open3D read Point Cloud")
pcd=o3d.io.read_point_cloud(r"D:\python\Ads2021\bunny\bunny10k.ply")
print(pcd)
o3d.visualization.draw_geometries([pcd],width=800,height=600)
按ctrl+-号可以减少点云的体素尺寸,ctrl+放大,ctrl-缩小
?看看open3d提供了哪些功能
?2.近邻搜索
文档:http://www.open3d.org/docs/release/
①周围n个点
import open3d as o3d
import numpy as np
print("Open3D read Point Cloud")
pcd = o3d.io.read_point_cloud(r"D:\python\Ads2021\bunny\bunny10k.ply")
pcd.paint_uniform_color([0.5, 0.5, 0.5])#这里的rgb取值为0-1,[0.5,0.5,0.5]意味着整个兔子的点云都以灰色呈现
pcd_tree = o3d.geometry.KDTreeFlann(pcd)#KD树贴着物体表面去找近邻的点
pcd.colors[100] = [1, 0, 0]#兔子点云的第100个点以红色呈现(一共6K+个点,不能超过这个范围)
[k, idx, _] = pcd_tree.search_knn_vector_3d(pcd.points[100],100)#对树做近邻搜索,以第100个点为中心,找附近的100个点
np.asarray(pcd.colors)[idx[1:], :] = [0, 1, 0]#将返回找到的点的坐标放到一个数组里,色彩设置为绿色
o3d.visualization.draw_geometries([pcd],width=1200,height=1000)#绘制pcd
?②索引半径
[k,idx,_] = pcd_tree.search_radius_vector_3d(pcd.points[3000],0.1) #索引半径小于0.02
np.asarray(pcd.colors)[idx[1:], :] = [0, 0, 1]
?③混合搜索
pcd.colors[2000]=[1, 0, 0]
[k2, idx2, _]=pcd_tree.search_hybrid_vector_3d(pcd.points[2000],0.05,200)
np.asarray(pcd.colors)[idx2[1:], :] = [0, 1, 0.8]
o3d.visualization.draw_geometries([pcd],width=1200,height=1000)
?3.法向量估计
令点与其他临近点生成平面,根据距平面最近距离判断法向量
import open3d as o3d
import numpy as np
print("Open3D read Point Cloud")
pcd = o3d.io.read_point_cloud(r"D:\python\Ads2021\bunny\bunny10k.ply")
print(pcd)
dumppcd = pcd.voxel_down_sample(voxel_size=0.01) #下采样(降采样)
dumppcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01,max_nn=30))
print(dumppcd.normals[0])
print(np.asarray(dumppcd.normals)[:10,:])
o3d.visualization.draw_geometries([dumppcd],point_show_normal=True,
window_name="法线估计", width=1200,height=1000, mesh_show_back_face=False)
?4.用无结构的点云数据生成结构化数据Mesh
参考:https://blog.csdn.net/io569417668/article/details/110855147
?
?5.用三角片面生成结构化数据Mesh
生成三角片面先做法向量估计,数据量更小,判断哪些点应该相连生成平面
存了面的信息,面都是三角形的
①mesh模型
创建球体,生成点云信息,点数越少越不圆
?REF算法:http://www.open3d.org/docs/0.10.0/tutorial/reference.html#Bernardini1999
mesh = o3d.geometry.TriangleMesh.create_sphere()
mesh.compute_vertex_normals()
o3d.visualization.draw_geometries([mesh])
pcd = mesh.sample_points_uniformly(number_of_points=500)
o3d.visualization.draw_geometries([pcd])
?②兔子案例
import open3d as o3d
# import open3d_tutorial as o3dtut
import numpy as np
print("Open3D read Point Cloud")
pcd = o3d.io.read_triangle_mesh(r"D:\python\Ads2021\bunny\bunny10k.ply") #newrabbit.pcd")
print(pcd)
pcd.compute_vertex_normals()
pcdmesh = pcd.sample_points_poisson_disk(3000)
o3d.visualization.draw_geometries([pcdmesh],point_show_normal=True)
radii=[0.005, 0.01, 0.02, 0.04]
ballmesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(pcdmesh,o3d.utility.DoubleVector(radii))
print(ballmesh)
o3d.visualization.draw_geometries([ballmesh])
o3d.visualization.draw_geometries([pcd, ballmesh])
?
|