Open3d的中文资料相对不是很丰富,网上有些代码还有小bug。今天我这儿搬运一下,提供点云显示和交互式点选的代码。
以下代码采用open3d 0.12.0 应该可以直接运行。如果有bug欢迎提出。
1.点云显示
代码引自:https://zhuanlan.zhihu.com/p/57215172
import numpy as np
import open3d as o3d
points = np.random.rand(10000, 3)
point_cloud = o3d.geometry.PointCloud()
point_cloud.points = o3d.utility.Vector3dVector(points)
o3d.visualization.draw_geometries([point_cloud])
import numpy as np
import open3d as o3d
points = np.random.rand(10000, 3)
point_cloud = o3d.geometry.PointCloud()
point_cloud.points = o3d.utility.Vector3dVector(points)
point_cloud.paint_uniform_color([0, 1, 0])#给所有点云一个统一的颜色
o3d.visualization.draw_geometries([point_cloud])
3.给每个点云以不同的颜色
此处定义一个随机color矩阵,一个向量由一个 1*3的向量决定。此处可以使用RGB颜色,不过颜色的值都要归一化到0~1之间。例如上图[0, 1, 0]表示绿色,[1, 0, 0]则表示红色,[0, 0, 1]则表示蓝色。其他颜色可以依据RGB颜色值表归一后获得。代码如下:
import numpy as np
import open3d as o3d
points = np.random.rand(10000, 3)
point_cloud = o3d.geometry.PointCloud()
color = np.random.rand(10000, 3)
point_cloud.points = o3d.utility.Vector3dVector(points)
point_cloud.colors = o3d.utility.Vector3dVector(color)
o3d.visualization.draw_geometries([point_cloud])
4.一个坐标系加入两组点云
如下代码第一块点云为绿色,第二块为红色。
import numpy as np
import open3d as o3d
points = np.random.rand(10000, 3)
point_cloud = o3d.geometry.PointCloud()
point_cloud.points = o3d.utility.Vector3dVector(points)
point_cloud.paint_uniform_color([0, 1, 0])#给所有点云一个统一的颜色
points_2 = np.random.rand(100, 3)
point_cloud2 = o3d.geometry.PointCloud()
point_cloud2.points = o3d.utility.Vector3dVector(points_2)
point_cloud2.paint_uniform_color([1, 0, 0])#给所有点云一个统一的颜色
o3d.visualization.draw_geometries([point_cloud, point_cloud2])
采用o3d.visualization.draw绘制可以获得更加丰富的显示效果,可以旋转点的大小,设置光照等
5.同时绘制线和点云:
print("Let's draw a box using o3d.geometry.LineSet.")
points = [
[0, 0, 0],
[1, 0, 0],
[0, 1, 0],
[1, 1, 0],
[0, 0, 1],
[1, 0, 1],
[0, 1, 1],
[1, 1, 1],
]
lines = [#设置线条连接points中点的次序
[0, 1],
[0, 2],
[1, 3],
[2, 3],
[4, 5],
[4, 6],
[5, 7],
[6, 7],
[0, 4],
[1, 5],
[2, 6],
[3, 7],
]
colors = [[1, 0, 0] for i in range(len(lines))]
line_set = o3d.geometry.LineSet(
points=o3d.utility.Vector3dVector(points),
lines=o3d.utility.Vector2iVector(lines),
)
line_set.colors = o3d.utility.Vector3dVector(colors)
point_cloud2 = o3d.geometry.PointCloud()
point_cloud2.points = o3d.utility.Vector3dVector(points)
point_cloud2.paint_uniform_color([0, 1, 0])
o3d.visualization.draw_geometries([line_set, point_cloud2])
6.点云的坐标点选
点云坐标点选很简单,显示的函数稍微调整即可。以第二段代码为例进行修改,代码如下:
import numpy as np
import open3d as o3d
points = np.random.rand(10000, 3)
point_cloud = o3d.geometry.PointCloud()
color = np.random.rand(10000, 3)
point_cloud.points = o3d.utility.Vector3dVector(points)
point_cloud.paint_uniform_color([0, 1, 0])#给所有点云一个统一的颜色
o3d.visualization.draw_geometries_with_editing([point_cloud])
图中小黄球和小蓝球就是按住shifit+鼠标左键点选得到的点,坐标在控制台中显示。按住shift+鼠标右键可以实现取消选择。
使用下面这段代码也可以实现上述功能,不过交互功能可以更加丰富。如按住q键就可以关闭Open3d的窗口,代码如下:
import numpy as np
import open3d as o3d
points = np.random.rand(10000, 3)
point_cloud = o3d.geometry.PointCloud()
color = np.random.rand(10000, 3)
point_cloud.points = o3d.utility.Vector3dVector(points)
point_cloud.paint_uniform_color([0, 1, 0])#给所有点云一个统一的颜色
vis = o3d.visualization.VisualizerWithEditing()
vis.create_window()
vis.add_geometry(point_cloud)
vis.run() # user picks points
vis.destroy_window()
上述交互代码参考如下代码:(链接:http://www.open3d.org/docs/release/tutorial/visualization/interactive_visualization.html)
# examples/python/visualization/interactive_visualization.py
import numpy as np
import copy
import open3d as o3d
def demo_crop_geometry():
print("Demo for manual geometry cropping")
print(
"1) Press 'Y' twice to align geometry with negative direction of y-axis"
)
print("2) Press 'K' to lock screen and to switch to selection mode")
print("3) Drag for rectangle selection,")
print(" or use ctrl + left click for polygon selection")
print("4) Press 'C' to get a selected geometry and to save it")
print("5) Press 'F' to switch to freeview mode")
pcd = o3d.io.read_point_cloud("../../test_data/ICP/cloud_bin_0.pcd")
o3d.visualization.draw_geometries_with_editing([pcd])
def draw_registration_result(source, target, transformation):
source_temp = copy.deepcopy(source)
target_temp = copy.deepcopy(target)
source_temp.paint_uniform_color([1, 0.706, 0])
target_temp.paint_uniform_color([0, 0.651, 0.929])
source_temp.transform(transformation)
o3d.visualization.draw_geometries([source_temp, target_temp])
def pick_points(pcd):
print("")
print(
"1) Please pick at least three correspondences using [shift + left click]"
)
print(" Press [shift + right click] to undo point picking")
print("2) After picking points, press 'Q' to close the window")
vis = o3d.visualization.VisualizerWithEditing()
vis.create_window()
vis.add_geometry(pcd)
vis.run() # user picks points
vis.destroy_window()
print("")
return vis.get_picked_points()
def demo_manual_registration():
print("Demo for manual ICP")
source = o3d.io.read_point_cloud("../../test_data/ICP/cloud_bin_0.pcd")
target = o3d.io.read_point_cloud("../../test_data/ICP/cloud_bin_2.pcd")
print("Visualization of two point clouds before manual alignment")
draw_registration_result(source, target, np.identity(4))
# pick points from two point clouds and builds correspondences
picked_id_source = pick_points(source)
picked_id_target = pick_points(target)
assert (len(picked_id_source) >= 3 and len(picked_id_target) >= 3)
assert (len(picked_id_source) == len(picked_id_target))
corr = np.zeros((len(picked_id_source), 2))
corr[:, 0] = picked_id_source
corr[:, 1] = picked_id_target
# estimate rough transformation using correspondences
print("Compute a rough transform using the correspondences given by user")
p2p = o3d.pipelines.registration.TransformationEstimationPointToPoint()
trans_init = p2p.compute_transformation(source, target,
o3d.utility.Vector2iVector(corr))
# point-to-point ICP for refinement
print("Perform point-to-point ICP refinement")
threshold = 0.03 # 3cm distance threshold
reg_p2p = o3d.pipelines.registration.registration_icp(
source, target, threshold, trans_init,
o3d.pipelines.registration.TransformationEstimationPointToPoint())
draw_registration_result(source, target, reg_p2p.transformation)
print("")
if __name__ == "__main__":
demo_crop_geometry()
demo_manual_registration()
欢迎大家一起搬运优质Open3d的资源,方便大家快速上手!