【发布时间】:2014-12-16 17:47:56
【问题描述】:
我有一个稀疏的 (100k / 20000^2) 二维布尔 numpy 掩码,对应于对象的位置。
我想更新掩码以将原始掩码中某个 True 像素半径内的所有像素设置为 True。换句话说,将 delta 函数响应与每个位置的圆形孔径/内核(在这种情况下)响应进行卷积。
由于主阵列很大(即 20000 x 20000),并且有 100k 个位置,我需要速度和内存效率...
例如(见numpy create 2D mask from list of indices [+ then draw from masked array]):
import numpy
from scipy import sparse
xys=[(1,2),(3,4),(6,9),(7,3)]
master_array=numpy.ones((100,100))
coords = zip(*xys)
mask = sparse.coo_matrix((numpy.ones(len(coords[0])),coords),\
shape= master_array.shape, dtype=bool)
# Now mask all pixels within a radius r of every coordinate pair in the list
mask = cookieCutter(mask,r) # <--- I need an efficient cookieCutter function!
# Now sample the masked array
draws=numpy.random.choice(master_array[~mask.toarray()].flatten(),size=10)
谢谢!
(继续numpy create 2D mask from list of indices [+ then draw from masked array])
【问题讨论】:
-
您能否提供一个数据示例以及您的屏蔽标准 究竟是什么?
-
当然,但是如何最好地做到这一点。我想屏蔽主阵列中位于位置列表(x_i,y_i)的半径 r 内的任何像素,以便我可以从剩余像素中抽取随机样本。我将编辑问题。