2

我有一个方形 numpy 数组,我想从数组中心点周围的环形区域中提取值。我想根据点到中心的距离来设置环的半径。我使用 numpy.indices 检索了数组索引,但无法找到构建过滤器的有效方法。如果您分享您的意见/建议,我将不胜感激。

indices = numpy.indices((5, 5))
print indices
[[[0 0 0 0 0]
[1 1 1 1 1]
[2 2 2 2 2]
[3 3 3 3 3]
[4 4 4 4 4]]

[[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]]]

现在我想提取那些索引与中心点距离为 1 的点的值,即 (2,2)。

4

1 回答 1

2
pt = (2, 2)
distance = 1
mask = (indices[0] - pt[0]) ** 2 + (indices[1] - pt[1]) ** 2 <= distance ** 2
result = my_array[mask]
于 2013-10-30T17:30:34.693 回答