【问题标题】:numpy filter points within bounding box边界框中的numpy过滤点
【发布时间】:2016-01-08 04:02:32
【问题描述】:

我在 numpy 中有一个二维点列表 [(x1,y1),(x2,y2),...,(xn,yn)],我如何获得由角 ((bx1,by1),(bx2,by2)) 指定的边界框内的点列表?

如果这是 C++,我会使用 OGC "within" specification in boost geometry 来过滤列表。

现在我只是在处理一个 NxN 2d numpy 数组的索引列表,所以我希望这应该只是 1-2 行 numpy 代码。

【问题讨论】:

标签: python numpy coordinates bounding-box


【解决方案1】:

使用alllogical_and<= 运算符的组合,一个 可以用1行表达主要思想。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2021-01-24
    相关资源
    最近更新 更多