Python numpy二维阵列:在所有给定位置创建圆形遮罩的有效方法

Python numpy二维阵列:在所有给定位置创建圆形遮罩的有效方法,python,arrays,numpy,Python,Arrays,Numpy,我有一个稀疏的(100k/20000^2)二维布尔numpy掩码,对应于对象的位置 我想更新遮罩,将原始遮罩中某个真像素半径内的所有像素设置为真。换句话说,在每个位置用圆孔/内核(在本例中)响应卷积增量函数响应 由于主阵列很大(即20000 x 20000),并且有100k个位置,因此我需要速度和内存效率 例如(见): 导入numpy 从scipy导入稀疏 xys=[(1,2)、(3,4)、(6,9)、(7,3)] 主数组=numpy.one((100100)) coords=zip(*xys)

我有一个稀疏的(100k/20000^2)二维布尔numpy掩码,对应于对象的位置

我想更新遮罩,将原始遮罩中某个真像素半径内的所有像素设置为真。换句话说,在每个位置用圆孔/内核(在本例中)响应卷积增量函数响应

由于主阵列很大(即20000 x 20000),并且有100k个位置,因此我需要速度和内存效率

例如(见):

导入numpy
从scipy导入稀疏
xys=[(1,2)、(3,4)、(6,9)、(7,3)]
主数组=numpy.one((100100))
coords=zip(*xys)
mask=sparse.coo_矩阵((numpy.one)(len(coords[0])),coords)\
shape=master\u array.shape,dtype=bool)
#现在遮罩列表中每个坐标对半径r内的所有像素

mask=cookieCutter(mask,r)#Scikit Image有,可以满足您的需要。

您能否提供一个数据示例,以及您的屏蔽标准是什么?当然,但如何最好地做到这一点。我想屏蔽主阵列中位于位置列表(x_I,y_I)半径r范围内的任何像素,以便从剩余像素中随机抽取样本。我会编辑这个问题。太棒了-谢谢!所以我使用了类似的东西:mask=skimage.morphomatics.diability(~mask.toarray(),skimage.morphomatics.disk(r)).astype(bool),虽然它看起来很慢,所以任何其他想法都值得赞赏!struct=scipy.ndimage.generate_binary_结构(2,1);mask2=scipy.ndimage.morphics.binary_膨胀(mask,structure=struct).astype(bool)要快得多
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)