如何将圆盘形遮罩应用于 NumPy 阵列? [英] How to apply a disc shaped mask to a NumPy array?

查看:48
本文介绍了如何将圆盘形遮罩应用于 NumPy 阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数组:

<预><代码>>>>np.ones((8,8))数组([[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.]])

我正在创建一个半径为 3 的圆盘形遮罩:

y,x = np.ogrid[-3: 3+1, -3: 3+1]掩码 = x**2+y**2 <= 3**2

这给出:

<代码>>>面具数组([[假,假,假,真,假,假,假],[假,真,真,真,真,真,假],[假,真,真,真,真,真,假],[真,真,真,真,真,真,真],[假,真,真,真,真,真,假],[假,真,真,真,真,真,假],[假,假,假,真,假,假,假]],dtype=bool)

现在,我希望能够将此掩码应用于我的数组,使用任何元素作为中心点.所以,例如,中心点在 (1,1),我想得到一个数组,如:

<预><代码>>>>新的_arr数组([[真, 真, 真, 真, 1., 1., 1., 1.],[真,真,真,真,真,1., 1., 1.],[真,真,真,真,1., 1., 1., 1.],[真,真,真,真,1., 1., 1., 1.],[ 1., 真, 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.]])

有没有简单的方法来敷这个面膜?

我不应该混合使用布尔值和浮点数 - 这是误导.

<预><代码>>>>新的_arr数组([[ 255., 255., 255., 255., 1., 1., 1., 1.],[255., 255., 255., 255., 255., 1., 1., 1.],[255., 255., 255., 255., 1., 1., 1., 1.],[255., 255., 255., 255., 1., 1., 1., 1.],[1., 255., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.],[ 1., 1., 1., 1., 1., 1., 1., 1.]])

这更符合我的要求.

array[mask] = 255

将使用中心点 (0+radius,0+radius) 屏蔽数组.

但是,我希望能够在任何点 (y,x) 放置任何尺寸的蒙版并自动修剪以适合.

解决方案

我会这样做,其中 (a, b) 是面具的中心:

将 numpy 导入为 npa, b = 1, 1n = 7r = 3y,x = np.ogrid[-a:n-a, -b:n-b]掩码 = x*x + y*y <= r*r数组 = np.ones((n, n))数组[掩码] = 255

I have an array like this:

>>> np.ones((8,8))
array([[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]])

I'm creating a disc shaped mask with radius 3 thus:

y,x = np.ogrid[-3: 3+1, -3: 3+1]
mask = x**2+y**2 <= 3**2

This gives:

>> mask
array([[False, False, False,  True, False, False, False],
       [False,  True,  True,  True,  True,  True, False],
       [False,  True,  True,  True,  True,  True, False],
       [ True,  True,  True,  True,  True,  True,  True],
       [False,  True,  True,  True,  True,  True, False],
       [False,  True,  True,  True,  True,  True, False],
       [False, False, False,  True, False, False, False]], dtype=bool)

Now, I want to be able to apply this mask to my array, using any element as a center point. So, for example, with center point at (1,1), I want to get an array like:

>>> new_arr
array([[ True,  True,  True,  True,    1.,  1.,  1.,  1.],
       [ True,  True,  True,  True,  True,  1.,  1.,  1.],
       [ True,  True,  True,  True,    1.,  1.,  1.,  1.],
       [ True,  True,  True,  True,    1.,  1.,  1.,  1.],
       [ 1.,    True,    1.,    1.,    1.,  1.,  1.,  1.],
       [ 1.,      1.,    1.,    1.,    1.,  1.,  1.,  1.],
       [ 1.,      1.,    1.,    1.,    1.,  1.,  1.,  1.],
       [ 1.,      1.,    1.,    1.,    1.,  1.,  1.,  1.]])

Is there an easy way to apply this mask?

Edit: I shouldn't have mixed booleans and floats - it was misleading.

>>> new_arr
array([[ 255.,  255.,  255.,  255.,    1.,  1.,  1.,  1.],
       [ 255.,  255.,  255.,  255.,  255.,  1.,  1.,  1.],
       [ 255.,  255.,  255.,  255.,    1.,  1.,  1.,  1.],
       [ 255.,  255.,  255.,  255.,    1.,  1.,  1.,  1.],
       [ 1.,    255.,    1.,    1.,    1.,  1.,  1.,  1.],
       [ 1.,      1.,    1.,    1.,    1.,  1.,  1.,  1.],
       [ 1.,      1.,    1.,    1.,    1.,  1.,  1.,  1.],
       [ 1.,      1.,    1.,    1.,    1.,  1.,  1.,  1.]])

This is more the result I require.

array[mask] = 255 

will mask the array using center point (0+radius,0+radius).

However, I'd like to be able to place any size mask at any point (y,x) and have it automatically trimmed to fit.

解决方案

I would do it like this, where (a, b) is the center of your mask:

import numpy as np

a, b = 1, 1
n = 7
r = 3

y,x = np.ogrid[-a:n-a, -b:n-b]
mask = x*x + y*y <= r*r

array = np.ones((n, n))
array[mask] = 255

这篇关于如何将圆盘形遮罩应用于 NumPy 阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆