【问题标题】:numpy 2-D array: efficient way to create circular masks at all given positionsnumpy 2-D 数组:在所有给定位置创建圆形掩码的有效方法
【发布时间】: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]

单仓特例:How to apply a disc shaped mask to a numpy array?

【问题讨论】:

  • 您能否提供一个数据示例以及您的屏蔽标准 究竟是什么?
  • 当然,但是如何最好地做到这一点。我想屏蔽主阵列中位于位置列表(x_i,y_i)的半径 r 内的任何像素,以便我可以从剩余像素中抽取随机样本。我将编辑问题。

标签: python arrays numpy


【解决方案1】:

Scikit-Image 有 a dilation function,可以满足您的目的。

【讨论】:

  • 太棒了 - 谢谢!所以我选择了类似的东西: mask=skimage.morphology.dilation(~mask.toarray(),skimage.morphology.disk(r)).astype(bool)
  • 虽然看起来很慢 - 所以任何其他想法都值得赞赏!
  • struct = scipy.ndimage.generate_binary_structure(2, 1); mask2=scipy.ndimage.morphology.binary_dilation(mask,structure=struct).astype(bool) 快很多
猜你喜欢
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 2016-08-28
相关资源
最近更新 更多