实时向点云添加新点 - Open3D

Pau*_*ink 9 python numpy open3d

我正在使用 Open3D 在 Python 中可视化点云。本质上,我想做的是以编程方式向点云添加另一个点,然后实时渲染它。

这是我到目前为止所拥有的。我找不到任何解决方案。

在下面的代码中,我展示了一种可能的解决方案,但它并不有效。第一个窗口关闭后,点将被添加并打开一个新窗口。这不是我想要的。我希望它能够动态显示新点,而无需再次关闭和打开。以及创建一个新变量的事实,我认为在处理越来越大的数据集时可能会出现问题

import open3d as o3d
import numpy as np

#Create two random points
randomPoints = np.random.rand(2, 3)

pointSet = o3d.geometry.PointCloud()

pointSet.points = o3d.utility.Vector3dVector(randomPoints)

#Visualize the two random points
o3d.visualization.draw_geometries([pointSet])

#Here I want to add more points to the pointSet
#This solution does not work effective

#Create another random set
p1 = np.random.rand(3, 3)

p2 = np.concatenate((pointSet.points, p1), axis=0)

pointSet2 = o3d.geometry.PointCloud()

pointSet2.points = o3d.utility.Vector3dVector(p2)

o3d.visualization.draw_geometries([pointSet2])

Run Code Online (Sandbox Code Playgroud)

有没有可能解决这个问题?

如果没有,我还可以查看哪些其他库能够实时渲染新的传入点。

Jav*_* TG 9

PointCloud通过扩展新坐标,可以将新点添加到 a 中并以交互方式可视化PointCloud.points

\n

这是因为当我们使用 numpy 数组时,我们需要创建一个Vector3dVector实例来实现方便的方法extend。来自文档

\n
\n

扩展(* args , ** kwargs )

\n

重载功能。

\n
    \n
  1. 扩展(自身:open3d.cpu.pybind.utility.Vector3dVector,L:\ nopen3d.cpu.pybind.utility.Vector3dVector) - >无
  2. \n
\n

通过附加给定列表中的所有项目来扩展列表

\n
    \n
  1. 扩展(自我:open3d.cpu.pybind.utility.Vector3dVector,L:可迭代)->\n无
  2. \n
\n

通过附加给定列表中的所有项目来扩展列表

\n
\n

所以我们可以使用不同的对象实例,例如,ndarrays等等。Vector3dVectorlists

\n

一个玩具示例及其结果:

\n

右像

\n
import open3d as o3d\nimport numpy as np\nimport time\n\n\n# create visualizer and window.\nvis = o3d.visualization.Visualizer()\nvis.create_window(height=480, width=640)\n\n# initialize pointcloud instance.\npcd = o3d.geometry.PointCloud()\n# *optionally* add initial points\npoints = np.random.rand(10, 3)\npcd.points = o3d.utility.Vector3dVector(points)\n\n# include it in the visualizer before non-blocking visualization.\nvis.add_geometry(pcd)\n\n# to add new points each dt secs.\ndt = 0.01\n# number of points that will be added\nn_new = 10\n\nprevious_t = time.time()\n\n# run non-blocking visualization. \n# To exit, press \'q\' or click the \'x\' of the window.\nkeep_running = True\nwhile keep_running:\n    \n    if time.time() - previous_t > dt:\n        # Options (uncomment each to try them out):\n        # 1) extend with ndarrays.\n        pcd.points.extend(np.random.rand(n_new, 3))\n        \n        # 2) extend with Vector3dVector instances.\n        # pcd.points.extend(\n        #     o3d.utility.Vector3dVector(np.random.rand(n_new, 3)))\n        \n        # 3) other iterables, e.g\n        # pcd.points.extend(np.random.rand(n_new, 3).tolist())\n        \n        vis.update_geometry(pcd)\n        previous_t = time.time()\n\n    keep_running = vis.poll_events()\n    vis.update_renderer()\n\nvis.destroy_window()\n
Run Code Online (Sandbox Code Playgroud)\n
\n

为什么不创建更新的几何图形并删除旧的几何图形呢?

\n

为了完整起见,其他(我认为不是更好)替代方法可以包括以下步骤:

\n
    \n
  1. 删除当前的PointCloud
  2. \n
  3. 连接OP问题中的新点
  4. \n
  5. 创建新的Pointcloud并将其添加到可视化工具中。
  6. \n
\n

这会产生更差的性能,并且几乎不允许与可视化进行交互。

\n

为了了解这一点,让我们看一下以下具有相同设置的比较(代码如下)。两个版本运行时间相同(约 10 秒)。

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
使用extend删除和创建PointCloud
\xe2\x9c\x94\xef\xb8\x8f 允许交互\xe2\x9d\x8c 交互困难
在此输入图像描述在此输入图像描述
平均执行时间(用于添加点):0.590 ms平均执行时间(用于添加点):1.550 ms
\n
\n

重现代码:

\n
import open3d as o3d\nimport numpy as np\nimport time\n\n# Global settings.\ndt = 3e-2 # to add new points each dt secs.\nt_total = 10 # total time to run this script.\nn_new = 10 # number of points that will be added each iteration.\n\n#---\n# 1st, using extend. Run non-blocking visualization.\n\n# create visualizer and window.\nvis = o3d.visualization.Visualizer()\nvis.create_window(height=480, width=640)\n\n# initialize pointcloud instance.\npcd = o3d.geometry.PointCloud()\n# *optionally* add initial points\npoints = np.random.rand(10, 3)\npcd.points = o3d.utility.Vector3dVector(points)\n# include it in the visualizer before non-blocking visualization.\nvis.add_geometry(pcd)\n\nexec_times = []\n\ncurrent_t = time.time()\nt0 = current_t\n\nwhile current_t - t0 < t_total:\n\n    previous_t = time.time()\n\n    while current_t - previous_t < dt:\n        s = time.time()\n\n        # Options (uncomment each to try it out):\n        # 1) extend with ndarrays.\n        pcd.points.extend(np.random.rand(n_new, 3))\n\n        # 2) extend with Vector3dVector instances.\n        # pcd.points.extend(\n        #     o3d.utility.Vector3dVector(np.random.rand(n_new, 3)))\n\n        # 3) other iterables, e.g\n        # pcd.points.extend(np.random.rand(n_new, 3).tolist())\n\n        vis.update_geometry(pcd)\n\n        current_t = time.time()\n        exec_times.append(time.time() - s)\n\n    vis.poll_events()\n    vis.update_renderer()\n\nprint(f"Using extend\\t\\t\\t# Points: {len(pcd.points)},\\n"\n      f"\\t\\t\\t\\t\\t\\tMean execution time:{np.mean(exec_times):.5f}")\n\nvis.destroy_window()\n\n# ---\n# 2nd, using remove + create + add PointCloud. Run non-blocking visualization.\n\n# create visualizer and window.\nvis = o3d.visualization.Visualizer()\nvis.create_window(height=480, width=640)\n\n# initialize pointcloud instance.\npcd = o3d.geometry.PointCloud()\npoints = np.random.rand(10, 3)\npcd.points = o3d.utility.Vector3dVector(points)\nvis.add_geometry(pcd)\n\nexec_times = []\n\ncurrent_t = time.time()\nt0 = current_t\nprevious_t = current_t\n\nwhile current_t - t0 < t_total:\n\n    previous_t = time.time()\n\n    while current_t - previous_t < dt:\n        s = time.time()\n\n        # remove, create and add new geometry.\n        vis.remove_geometry(pcd)\n        pcd = o3d.geometry.PointCloud()\n        points = np.concatenate((points, np.random.rand(n_new, 3)))\n        pcd.points = o3d.utility.Vector3dVector(points)\n        vis.add_geometry(pcd)\n\n        current_t = time.time()\n        exec_times.append(time.time() - s)\n\n    current_t = time.time()\n\n    vis.poll_events()\n    vis.update_renderer()\n\nprint(f"Without using extend\\t# Points: {len(pcd.points)},\\n"\n      f"\\t\\t\\t\\t\\t\\tMean execution time:{np.mean(exec_times):.5f}")\n\nvis.destroy_window()\n
Run Code Online (Sandbox Code Playgroud)\n