numpy二维数组:在所有给定位置创建圆形蒙版的有效方法

时间:2014-10-21 13:21:06

标签: python arrays numpy

我有一个与对象位置对应的稀疏(100k / 20000 ^ 2)2-D布尔numpy蒙版。

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

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

例如(见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]

单一职位的特例:How to apply a disc shaped mask to a numpy array?

1 个答案:

答案 0 :(得分:1)

Scikit-Image有a dilation function,这符合你的目的。