And*_*ndt 5 python numpy bounding-box coordinates
我有[(x1,y1),(x2,y2),...,(xn,yn)]
ndy 中的2d点列表,如何获得角点指定的边界框内的点列表((bx1,by1),(bx2,by2))
?
如果这是C++,我将使用boost几何中的OGC"within"规范来过滤列表.
现在我只是处理一个NxN 2d numpy数组的索引列表,所以我希望这应该是1-2行代码与numpy.
Ror*_*rke 12
使用的组合all
,logical_and
和<=
操作员可以在一行中表达主要想法.
import random
import numpy as np
from matplotlib import pyplot
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)]
# this is just for drawing
rect = np.array([[bx1, by1], [bx1, by2], [bx2, by2], [bx2, by1], [bx1, by1]])
pyplot.plot(inbox[:, 0], inbox[:, 1], 'rx',
outbox[:, 0], outbox[:, 1], 'bo',
rect[:, 0], rect[:, 1], 'g-')
pyplot.show()
Run Code Online (Sandbox Code Playgroud)