我有来自多个点云文件的数百万个xyz坐标,我将其存储在一个二维的numpy数组中:[[x1, y1, z1], [x2, y2, z2],..., [xn, yn, zn]]
。
我想过滤4个坐标[[x1, y1], [x2, y2]]
描述的特定边界框内的所有点,即矩形的左下和右上坐标。
我已经找到了以下代码来过滤numpy的坐标,它几乎就是我想要的。唯一的区别是(如果我说得对)我的二维数组也有z坐标。
import random
import numpy as np
points = [(random.random(), random.random()) for i in range(100)]
bx1, bx2 = sorted([random.random(), random.random()])
by1, by2 = sorted([random.random(), random.random()])
pts = np.array(points)
ll = np.array([bx1, by1]) # lower-left
ur = np.array([bx2, by2]) # upper-right
inidx = np.all(np.logical_and(ll <= pts, pts <= ur), axis=1)
inbox = pts[inidx]
outbox = pts[np.logical_not(inidx)]
我如何修改上面的代码,使其与xyz坐标一起使用,用两个xy坐标描述的边界框进行过滤?
答案 0 :(得分:3)
选择点的X和Y坐标:
xy_pts = pts[:,[0,1]]
现在,只需在比较中使用xy_pts
代替pts
:
inidx = np.all((ll <= xy_pts) & (xy_pts <= ur), axis=1)
答案 1 :(得分:1)
我正在写一个Python library for working with point clouds我有这个功能,我认为这应该适合你:
def bounding_box(points, min_x=-np.inf, max_x=np.inf, min_y=-np.inf,
max_y=np.inf, min_z=-np.inf, max_z=np.inf):
""" Compute a bounding_box filter on the given points
Parameters
----------
points: (n,3) array
The array containing all the points's coordinates. Expected format:
array([
[x1,y1,z1],
...,
[xn,yn,zn]])
min_i, max_i: float
The bounding box limits for each coordinate. If some limits are missing,
the default values are -infinite for the min_i and infinite for the max_i.
Returns
-------
bb_filter : boolean array
The boolean mask indicating wherever a point should be keeped or not.
The size of the boolean mask will be the same as the number of given points.
"""
bound_x = np.logical_and(points[:, 0] > min_x, points[:, 0] < max_x)
bound_y = np.logical_and(points[:, 1] > min_y, points[:, 1] < max_y)
bound_z = np.logical_and(points[:, 2] > min_z, points[:, 2] < max_z)
bb_filter = np.logical_and(bound_x, bound_y, bound_z)
return bb_filter
以下是您要问的一个示例:
1000万点:
points = np.random.rand(10000000, 3)
您指定格式的矩形:
rectangle = np.array([[0.2, 0.2],
[0.4, 0.4]])
打开矩形包装:
min_x = rectangle[:,0].min()
max_x = rectangle[:,0].max()
min_y = rectangle[:,1].min()
max_y = rectangle[:,1].max()
在框内获取布尔数组标记点:
%%timeit
inside_box = bounding_box(points, min_x=min_x, max_x=max_x, min_y=min_y, max_y=max_y)
1 loop, best of 3: 247 ms per loop
这样您可以按如下方式使用数组:
points_inside_box = points[inside_box]
points_outside_box = points[~inside_box]