1、主要参考
(1)视频,大佬讲的就是好啊
【Open3D】三维点云python教程_哔哩哔哩_bilibili
(2)官方的github地址
GitHub - isl-org/Open3D: Open3D: A Modern Library for 3D Data Processing
(3)作者
@article{Zhou2018, author = {Qian-Yi Zhou and Jaesik Park and Vladlen Koltun}, title = {{Open3D}: {A} Modern Library for {3D} Data Processing}, journal = {arXiv:1801.09847}, year = {2018}, }
(4)尤其注意,文档地址
Open3D: A Modern Library for 3D Data Processing — Open3D 0.16.0 documentation
2、相关模块
(1)Open3D-ML,一个机器学习的包
3、各类官方例子
3.1打开椅子fragment.ply并显示
注意,运行下面程序后会自动下载相应的fragment.ply文件
(1)代码
import open3d as o3d
import numpy as np
print("Load a ply point cloud, print it, and render it")
ply_point_cloud = o3d.data.PLYPointCloud()
pcd = o3d.io.read_point_cloud(ply_point_cloud.path)
# 或者你有文件了
# path = "D:/RGBD_CAMERA/python_3d_process/fragment.ply"
# pcd = o3d.io.read_point_cloud(path) # path为文件路径
print(pcd)
print(np.asarray(pcd.points))
o3d.visualization.draw_geometries([pcd],
zoom=0.3412,
front=[0.4257, -0.2125, -0.8795],
lookat=[2.6172, 2.0475, 1.532],
up=[-0.0694, -0.9768, 0.2024])
(2)显示结果
注意:按键盘+或者-可以修改点云大小,鼠标可以转动角度
3.2下采样downsampling
3.2.1包围盒下采样
(1)函数,参数应该就包围盒的大小(体素)
voxel_down_sample(voxel_size=0.05)
(2)测试代码
import open3d as o3d
import numpy as np
# print("Load a ply point cloud, print it, and render it")
# ply_point_cloud = o3d.data.PLYPointCloud()
# pcd = o3d.io.read_point_cloud(ply_point_cloud.path)
# 或者你有文件了
path = "D:/RGBD_CAMERA/python_3d_process/fragment.ply"
pcd = o3d.io.read_point_cloud(path) # path为文件路径
print(pcd)
#--------------------------------------------------------
#(例子一)显示
#---------------------------------------------------------
# print(pcd)
# print(np.asarray(pcd.points))
# o3d.visualization.draw_geometries([pcd],
# zoom=0.3412,
# front=[0.4257, -0.2125, -0.8795],
# lookat=[2.6172, 2.0475, 1.532],
# up=[-0.0694, -0.9768, 0.2024])
#--------------------------------------------------------
#(例子二)下采样
#---------------------------------------------------------
downpcd = pcd.voxel_down_sample(voxel_size=0.05)
# downpcd = pcd.voxel_down_sample(voxel_size=0.5)
print(downpcd)
o3d.visualization.draw_geometries([downpcd],
zoom=0.3412,
front=[0.4257, -0.2125, -0.8795],
lookat=[2.6172, 2.0475, 1.532],
up=[-0.0694, -0.9768, 0.2024])
(3)测试结果
1)0.05
采样前后的数据
PointCloud with 196133 points.
PointCloud with 4718 points.
2)0.5
采样前后的数据
PointCloud with 196133 points.
PointCloud with 58 points.
3.3定点法向量估计
(1)测试代码
import open3d as o3d
import numpy as np
# print("Load a ply point cloud, print it, and render it")
# ply_point_cloud = o3d.data.PLYPointCloud()
# pcd = o3d.io.read_point_cloud(ply_point_cloud.path)
# 或者你有文件了
path = "D:/RGBD_CAMERA/python_3d_process/fragment.ply"
pcd = o3d.io.read_point_cloud(path) # path为文件路径
print(pcd)
#--------------------------------------------------------
#(例子一)显示
#---------------------------------------------------------
# print(pcd)
# print(np.asarray(pcd.points))
# o3d.visualization.draw_geometries([pcd],
# zoom=0.3412,
# front=[0.4257, -0.2125, -0.8795],
# lookat=[2.6172, 2.0475, 1.532],
# up=[-0.0694, -0.9768, 0.2024])
#--------------------------------------------------------
#(例子二)下采样
#---------------------------------------------------------
downpcd = pcd.voxel_down_sample(voxel_size=0.05)
# # downpcd = pcd.voxel_down_sample(voxel_size=0.5)
# print(downpcd)
# o3d.visualization.draw_geometries([downpcd],
# zoom=0.3412,
# front=[0.4257, -0.2125, -0.8795],
# lookat=[2.6172, 2.0475, 1.532],
# up=[-0.0694, -0.9768, 0.2024])
#--------------------------------------------------------
#(例子三)定点法向量估计
#---------------------------------------------------------
print("Recompute the normal of the downsampled point cloud")
downpcd.estimate_normals(
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30))
o3d.visualization.draw_geometries([downpcd],
zoom=0.3412,
front=[0.4257, -0.2125, -0.8795],
lookat=[2.6172, 2.0475, 1.532],
up=[-0.0694, -0.9768, 0.2024],
point_show_normal=True)
print("Print a normal vector of the 0th point")
print(downpcd.normals[0])
print("Print the normal vectors of the first 10 points")
print(np.asarray(downpcd.normals)[:10, :])
(2)测试结果
注意:可以通过键盘上的按键N来回切换查看向量
3.4剪切点云数据
(1)两个主要函数
- read_selection_polygon_volume读取指定多边形选择区域的json文件。
- vol.crop_point_cloud (pcd)过滤掉点。只剩下椅子了。
(2)使用以下代码后会自动下载并解压2个文件
(3)测试代码如下
import open3d as o3d
import numpy as np
#--------------------------------------------------------
#(例子四)剪切点云
#---------------------------------------------------------
demo_crop_data = o3d.data.DemoCropPointCloud()
pcd = o3d.io.read_point_cloud(demo_crop_data.point_cloud_path)
vol = o3d.visualization.read_selection_polygon_volume(demo_crop_data.cropped_json_path)
chair = vol.crop_point_cloud(pcd)
o3d.visualization.draw_geometries([chair],
zoom=0.7,
front=[0.5439, -0.2333, -0.8060],
lookat=[2.4615, 2.1331, 1.338],
up=[-0.1781, -0.9708, 0.1608])
(4)测试结果如图
(5)使用本地文件的方法如下:
import open3d as o3d
import numpy as np
#--------------------------------------------------------
#(例子四)剪切点云
#---------------------------------------------------------
# demo_crop_data = o3d.data.DemoCropPointCloud()
# pcd = o3d.io.read_point_cloud(demo_crop_data.point_cloud_path)
# vol = o3d.visualization.read_selection_polygon_volume(demo_crop_data.cropped_json_path)
# chair = vol.crop_point_cloud(pcd)
# o3d.visualization.draw_geometries([chair],
# zoom=0.7,
# front=[0.5439, -0.2333, -0.8060],
# lookat=[2.4615, 2.1331, 1.338],
# up=[-0.1781, -0.9708, 0.1608])
#--------------------------------------------------------
#(例子四)剪切点云--使用本地
#---------------------------------------------------------
plypath = "D:/RGBD_CAMERA/python_3d_process/DemoCropPointCloud/fragment.ply"
pcd = o3d.io.read_point_cloud(plypath) # path为文件路径
jsonpath = "D:/RGBD_CAMERA/python_3d_process/DemoCropPointCloud/cropped.json"
vol = o3d.visualization.read_selection_polygon_volume(jsonpath)
chair = vol.crop_point_cloud(pcd)
o3d.visualization.draw_geometries([chair],
zoom=0.7,
front=[0.5439, -0.2333, -0.8060],
lookat=[2.4615, 2.1331, 1.338],
up=[-0.1781, -0.9708, 0.1608])
(6)其中cropped.json的内容如下:
{
"axis_max" : 4.022921085357666,
"axis_min" : -0.76341366767883301,
"bounding_polygon" :
[
[ 2.6509309513852526, 0.0, 1.6834473132326844 ],
[ 2.5786428246917148, 0.0, 1.6892074266735244 ],
[ 2.4625790337552154, 0.0, 1.6665777078297999 ],
[ 2.2228544982251655, 0.0, 1.6168160446813649 ],
[ 2.166993206001413, 0.0, 1.6115495157201662 ],
[ 2.1167895865303286, 0.0, 1.6257706054969348 ],
[ 2.0634657721747383, 0.0, 1.623021658624539 ],
[ 2.0568612343437236, 0.0, 1.5853892911207643 ],
[ 2.1605399001237027, 0.0, 0.96228993255083017 ],
[ 2.1956669387205228, 0.0, 0.95572746049785073 ],
[ 2.2191318790575583, 0.0, 0.88734449982108754 ],
[ 2.2484881847925919, 0.0, 0.87042807267013633 ],
[ 2.6891234157295827, 0.0, 0.94140677988967603 ],
[ 2.7328692490470647, 0.0, 0.98775740674840251 ],
[ 2.7129337547575547, 0.0, 1.0398850034649203 ],
[ 2.7592174072415405, 0.0, 1.0692940558509485 ],
[ 2.7689216419453428, 0.0, 1.0953914441371593 ],
[ 2.6851455625455669, 0.0, 1.6307334122162018 ],
[ 2.6714776099981239, 0.0, 1.675524657088997 ],
[ 2.6579576128816544, 0.0, 1.6819127849749496 ]
],
"class_name" : "SelectionPolygonVolume",
"orthogonal_axis" : "Y",
"version_major" : 1,
"version_minor" : 0
}
3.5 给点云上颜色
(1)参数,后面三个是RGB的颜色,取值都是0--1
paint_uniform_color([1, 0.706, 0])
(2)代码
import open3d as o3d
import numpy as np
#--------------------------------------------------------
#(例子四)剪切点云--使用本地
#---------------------------------------------------------
plypath = "D:/RGBD_CAMERA/python_3d_process/DemoCropPointCloud/fragment.ply"
pcd = o3d.io.read_point_cloud(plypath) # path为文件路径
jsonpath = "D:/RGBD_CAMERA/python_3d_process/DemoCropPointCloud/cropped.json"
vol = o3d.visualization.read_selection_polygon_volume(jsonpath)
chair = vol.crop_point_cloud(pcd)
# o3d.visualization.draw_geometries([chair],
# zoom=0.7,
# front=[0.5439, -0.2333, -0.8060],
# lookat=[2.4615, 2.1331, 1.338],
# up=[-0.1781, -0.9708, 0.1608])
print("Paint chair")
chair.paint_uniform_color([1, 0.706, 0])
o3d.visualization.draw_geometries([chair],
zoom=0.7,
front=[0.5439, -0.2333, -0.8060],
lookat=[2.4615, 2.1331, 1.338],
up=[-0.1781, -0.9708, 0.1608])
(3)测试结果