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)说明
体素下采样使用常规体素网格从输入点云创建统一的下采样点云。它经常被用作许多点云处理任务的预处理步骤。该算法分为两个步骤:
1.点被装入体素中。
2.每个体素通过计算体素内部所有点的平均来生成一个点。
(3)测试代码
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])
(4)测试结果
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)测试结果
3.6点云距离测量
(1)函数
compute_point_cloud_distance
(2)说明
Open3D提供了compute_point_cloud_distance方法来计算从源点云到目标点云的距离。也就是说,它为源点云中的每个点计算到目标点云中最近点的距离。
(3)测试代码,(以下代码,找到距离大于椅子设定值的物体,如0.01或者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)
# 把和椅子的距离大于0.01的找出来
dists = pcd.compute_point_cloud_distance(chair)
dists = np.asarray(dists)
ind = np.where(dists > 0.01)[0]
# ind = np.where(dists > 0.2)[0]
pcd_without_chair = pcd.select_by_index(ind)
o3d.visualization.draw_geometries([pcd_without_chair],
zoom=0.3412,
front=[0.4257, -0.2125, -0.8795],
lookat=[2.6172, 2.0475, 1.532],
up=[-0.0694, -0.9768, 0.2024])
(4)测试结果
1)距离大于0.01(ind = np.where(dists > 0.01)[0])
2)距离大于0.2(ind = np.where(dists > 0.2)[0])
3.7 包围盒(Bounding volumes)
(1)函数
- get_axis_aligned_bounding_box -- 根据坐标获取物体的包围盒(x,y,z坐标方向)
- get_oriented_bounding_box -- 根据物体的角度获取物体的包围盒(可围着物体旋转)
PS:感觉有点像图像检测中的外接矩形(x,y坐标),最小外接矩形(围着物体可旋转),待验证。
(2)说明
点云的几何类型和Open3D中的所有其他几何类型一样具有边界卷。目前,Open3D实现了axisaligned dboundingbox和OrientedBoundingBox,它们也可用于裁剪几何图形。
(3)测试代码
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)
# 把和椅子的距离大于0.01的找出来
aabb = chair.get_axis_aligned_bounding_box()
aabb.color = (1, 0, 0)
obb = chair.get_oriented_bounding_box()
obb.color = (0, 1, 0)
o3d.visualization.draw_geometries([chair, aabb, obb],
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)测试结果