温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Finding points within a bounding box with numpy
numpy python coordinates point-clouds

python - 用numpy在边界框中查找点

发布于 2020-03-27 11:13:33

我有多个存储在二维numpy数组中的点云文件中的数百万个xyz坐标:[[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坐标描述的边界框进行过滤?

查看更多

查看更多

提问者
conste
被浏览
101
DYZ 2017-02-21 03:47

选择点的X和Y坐标:

xy_pts = pts[:,[0,1]]

现在,只需在比较中使用xy_pts而不是pts

inidx = np.all((ll <= xy_pts) & (xy_pts <= ur), axis=1)