numpy 2-D数组:高效创建所有给定位置的圆形掩码的方法

0 投票
1 回答
3022 浏览
提问于 2025-04-28 19:09

我有一个稀疏的二维布尔型numpy掩码,大小是100k / 20000^2,这个掩码对应着物体的位置。

我想更新这个掩码,把原始掩码中所有True像素周围一定半径内的像素都设置为True。换句话说,就是在每个位置用一个圆形的窗口对这个掩码进行处理。

因为这个主数组很大(也就是20000 x 20000),而且有10万个位置,所以我需要速度快和节省内存...

举个例子(可以参考 如何从索引列表创建二维掩码 [+ 然后从掩码数组中绘制]):

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数组?

暂无标签

1 个回答

3

Scikit-Image 有一个叫做 膨胀函数,这个函数可以满足你的需求。

撰写回答