一、下载第三方库Open3D
1.在cmd里输入pip?install open3d
2.如下图,下载3D Object相关的文件后续可以引用
The Stanford 3D Scanning Repository
?
注:下载时间有一点久,但是等候一下就行
二、经典案例——斯坦福兔子
1.试用open 3d,默认生成彩色的三维模型
import open3d as o3d
import numpy as np
import os
os.chdir("C:/Users/读书人mn/Desktop/123123/bunny")
print("Open3D read Point Cloud")
pcd=o3d.io.read_point_cloud(r"data/thu_statue.ply")
print(pcd)
o3d.visualization.draw_geometries([pcd],width=800,height=600)
结果如下:(该结果不是用斯坦兔子生成的结果,但是,知识调用的文件不同,结果都是一样的)
?2.不同方法进行搜索
(1)近邻搜索
import open3d as o3d
import numpy as np
#近邻搜索
print("Open3D read Point Cloud")
pcd = o3d.io.read_point_cloud(r"data/bunny10k.ply")#.io读数据 r 生数据
pcd.paint_uniform_color([0.5, 0.5, 0.5])
pcd_tree = o3d.geometry.KDTreeFlann(pcd)
pcd.colors[1000] = [1, 0, 0]#依次为红、绿、蓝 100指的是近邻搜索的点的个数
[k, idx, _] = pcd_tree.search_knn_vector_3d(pcd.points[1000],1000)#knn是点
np.asarray(pcd.colors)[idx[1:], :] = [0, 1, 0]#数组 依次为红、绿、蓝
o3d.visualization.draw_geometries([pcd],width=1200,height=1000)#绘制图形
(2)半径搜索
该搜索将半径定于小于0.02
import open3d as o3d
import numpy as np
#半径搜索
print("Open3D read Point Cloud")
pcd = o3d.io.read_point_cloud(r"data/bunny10k.ply")
pcd.paint_uniform_color([0.5, 0.5, 0.5])
pcd_tree = o3d.geometry.KDTreeFlann(pcd)
pcd.colors[100] = [1, 0, 0]
[k,idx,_] = pcd_tree.search_radius_vector_3d(pcd.points[3000],0.1) #索引半径小于0.02
np.asarray(pcd.colors)[idx[1:], :] = [0, 0, 1]
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"data/bunny10k.ply")
pcd.paint_uniform_color([0.5, 0.5, 0.5])
pcd_tree = o3d.geometry.KDTreeFlann(pcd)
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"data/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
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"data/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])
备注:1.三角片面生成,必须先做法向量的估计
2.在生成模型的时候,内部会存一些点的信息,但是肯定也会有面的信息
结果如下:
?
|