问题描述
我从多个点云文件中获取了数百万个xyz坐标,这些文件存储在二维numpy数组中: [[x1,y1,z1],[x2,y2,z2],...,[xn,yn,zn]]
.
I have millions of xyz-coordinates from multiple point cloud files which I am storing inside a 2-dimensional numpy array: [[x1, y1, z1], [x2, y2, z2],..., [xn, yn, zn]]
.
我要过滤由4个坐标 [[x1,y1],[x2,y2]]
描述的特定边界框内的所有点,即矩形.
I want to filter all the points which are inside a specific bounding box described by 4 coordinates [[x1, y1], [x2, y2]]
i.e. the lower left and upper right coordinates of a rectangle.
我已经找到以下代码来用numpy过滤坐标,这几乎是我想要的.唯一的区别是(如果我做对了)我的二维数组也具有z坐标.
I have already found the following piece of code to filter coordinates with numpy and it's almost what I want. The only difference is (if I'm getting it right) that my 2-dimensional array also has got z-coordinates.
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坐标所描述的边界框对其进行过滤?
How would I have to modifiy the code above to make it work with xyz-coordinates to be filtered by a bounding box described with two xy-coordinates?
推荐答案
选择点的X和Y坐标:
xy_pts = pts[:,[0,1]]
现在,在比较中只需使用 xy_pts
而不是 pts
:
Now, simply use xy_pts
instead of pts
in the comparisons:
inidx = np.all((ll <= xy_pts) & (xy_pts <= ur), axis=1)
这篇关于用numpy在边界框内查找点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!