Open3d学习计划——高级篇 12(交互式可视化)–最终章

Open3d学习计划——高级篇 12(交互式可视化)–最终章

本篇教程介绍了Open3d的可视化窗口的交互功能。

# examples/Python/Advanced/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("../../TestData/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) Afther picking points, press q for 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("../../TestData/ICP/cloud_bin_0.pcd")
    target = o3d.io.read_point_cloud("../../TestData/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.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.registration.registration_icp(
        source, target, threshold, trans_init,
        o3d.registration.TransformationEstimationPointToPoint())
    draw_registration_result(source, target, reg_p2p.transformation)
    print("")


if __name__ == "__main__":
    demo_crop_geometry()
    demo_manual_registration()

这个脚本执行了用户交互的两个程序:demo_crop_geometry()demo_manual_registration()

裁剪几何体

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("../../TestData/ICP/cloud_bin_0.pcd")
    o3d.visualization.draw_geometries_with_editing([pcd])

这个函数简单读取了一个点云数据然后调用了draw_geometries_with_editing函数,该函数提供了顶点选择和裁剪功能。

Note
Open3d有一个继承Visualizer类的VisualizerWithEditing类。它提供了图形用户交互功能。同样的例子在自定义可视化中,VisualizerWithEditing()是可以显示的替换掉draw_geometries_with_editing([pcd]).

在几何体显示之后,按两次Y可以将几何体与Y轴的负半轴对齐。在调整好观看视角之后,按K键锁定视图并切换到选择模式。
Open3d学习计划——高级篇 12(交互式可视化)–最终章_第1张图片

Tip
在实际的选取区域的操作中,一般都是使用正交投影模型(orthographic projection model.)将几何体与任意轴对齐。这个技巧可以避免由于透视投影带来自遮挡问题,使得选取变得容易。

在选择区域时,可以用鼠标拖动(矩形区域)或者 ctrl + 鼠标左键点击(选取多边形区域)。下面的例子展现了多边形选取。
Open3d学习计划——高级篇 12(交互式可视化)–最终章_第2张图片
注意选择的区域为深色阴影,如果要保存选取的区域并且丢弃其余的,请按C。他会弹出一个对话框去保存裁剪的区域。裁剪的结果将在显示之后保存。

Open3d学习计划——高级篇 12(交互式可视化)–最终章_第3张图片
Open3d学习计划——高级篇 12(交互式可视化)–最终章_第4张图片
按F可以结束选择模式进入自由浏览模式。
Open3d学习计划——高级篇 12(交互式可视化)–最终章_第5张图片

手动配准

选取对应点

下面的代码使用点对点的ICP去配准两个点云。它通过人工交互来获得初始的对齐。

def demo_manual_registration():
    print("Demo for manual ICP")
    source = o3d.io.read_point_cloud("../../TestData/ICP/cloud_bin_0.pcd")
    target = o3d.io.read_point_cloud("../../TestData/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)

这个脚本读取两组点云,并且在对齐之前可视化。
Open3d学习计划——高级篇 12(交互式可视化)–最终章_第6张图片

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) Afther picking points, press q for 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()

函数pick_points(pcd)创造了一个VisualizerWithEditing实例去模仿draw_geometries,他创造可视化窗口,添加几何图形,可视化几何图形和结束。VisualizerWithEditing提供一种新的交互函数get_picked_points(),他可以返回用户选取的顶点的索引。

在窗口中点击 shift + 左键可以选取顶点。当顶点被选取的时候,可视化窗口会在上面覆盖一个球形。比如,下图是在源点云上选取了三个顶点之后的结果。
Open3d学习计划——高级篇 12(交互式可视化)–最终章_第7张图片
将会打印出:

Picked point #58481 (2.14, 1.56, 1.53) to add in queue.
Picked point #77321 (2.86, 1.92, 1.09) to add in queue.
Picked point #42639 (3.28, 1.53, 1.45) to add in queue.

按 q 关闭窗口,之后在目标点云上选取相应的对应点。这个球体的颜色有助于识别相同的对应点。
Open3d学习计划——高级篇 12(交互式可视化)–最终章_第8张图片
将会打印出:

Picked point #54028 (1.62, 1.81, 1.23) to add in queue.
Picked point #97115 (2.45, 2.19, 1.11) to add in queue.
Picked point #47467 (2.75, 1.71, 1.45) to add in queue.

Tip
为了有一个好的配准结果,应该去选取场景中均匀分散的三个对应点。选取拐角区域的顶点有助于选取高质量的对应点。

使用用户选取的对应关系配准

    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.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.registration.registration_icp(
        source, target, threshold, trans_init,
        o3d.registration.TransformationEstimationPointToPoint())
    draw_registration_result(source, target, reg_p2p.transformation)
    print("")

Demo的后续部分是根据用户提供的对应关系来计算初始化转换。这个脚本通过使用Vector2iVector(corr)建立了成对的对应关系。他利用TransformationEstimationPointToPoint.compute_transformation去计算一个初始化的变换。之后再此基础上使用registration_icp微调。
配准结果如下:
Open3d学习计划——高级篇 12(交互式可视化)–最终章_第9张图片

关于翻译大家有更好的意见欢迎评论一起学习!!!

欢迎大家加入知识星球一起学习。

Open3d学习计划——高级篇 12(交互式可视化)–最终章_第10张图片

你可能感兴趣的:(open3d,python,点云,可视化,open3d)