[英]Applying circular mask with periodic boundary conditions in python
This question is related to: How to apply a disc shaped mask to a numpy array? 这个问题涉及到: 如何将圆盘形掩模应用于numpy阵列?
From the solution: https://stackoverflow.com/a/8650741/4484153 , is it possible to obtain circular mask in the following manner: 从解决方案: https : //stackoverflow.com/a/8650741/4484153 ,是否可以通过以下方式获得圆形掩码:
>>> new_arr
array([[ True, True, True, True, 1., 1., 1., True],
[ True, True, True, True, True, 1., True, True],
[ True, True, True, True, 1., 1., 1., True],
[ True, True, True, True, 1., 1., 1., True],
[ 1., True, 1., 1., 1., 1., 1., 1. ],
[ 1., 1., 1., 1., 1., 1., 1., 1. ],
[ 1., True, 1., 1., 1., 1., 1., 1. ],
[ True, True, True, True, 1., 1., 1., True]])
in such a way that the array wraps around its columns and rows? 以这种方式,数组包裹其列和行?
One way could just be to create the mask of required size in the center of the array and then use np.roll
to shift the mask along an axis (this causes the mask to wrap around the edges of the array). 一种方法可能是在数组的中心创建所需大小的掩码,然后使用np.roll
沿着轴移动掩码(这会导致掩码环绕数组的边缘)。
Following the method in the linked question and answer: 按照链接问题和答案中的方法:
ones = np.ones((8, 8))
a, b = 3, 3
n = 8
r = 3
mask = x**2 + y**2 <= r**2
constructs mask
like this: 像这样构造mask
:
array([[False, False, False, True, False, False, False, False],
[False, True, True, True, True, True, False, False],
[False, True, True, True, True, True, False, False],
[ True, True, True, True, True, True, True, False],
[False, True, True, True, True, True, False, False],
[False, True, True, True, True, True, False, False],
[False, False, False, True, False, False, False, False],
[False, False, False, False, False, False, False, False]], dtype=bool)
Then rolling mask
two places up and two places left and using it on ones
... 然后滚动mask
两个地起身离开两个地方,并使用它ones
...
>>> rolled_mask = np.roll(np.roll(mask, -2, axis=0), -2, axis=1)
>>> ones[rolled_mask] = 255
>>> ones
array([[ 255., 255., 255., 255., 1., 1., 1., 255.],
[ 255., 255., 255., 255., 255., 1., 255., 255.],
[ 255., 255., 255., 255., 1., 1., 1., 255.],
[ 255., 255., 255., 255., 1., 1., 1., 255.],
[ 1., 255., 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1., 1., 1.],
[ 1., 255., 1., 1., 1., 1., 1., 1.],
[ 255., 255., 255., 255., 1., 1., 1., 255.]])
If you want to create the mask directly, the following works: 如果要直接创建蒙版,以下工作方式:
>>> N = 10
>>> row, col = 8, 7
>>> radius = 4
>>> rows = np.arange(N) - row
>>> rows = np.minimum(np.abs(rows), rows % N)
>>> cols = np.arange(N) - col
>>> cols = np.minimum(np.abs(cols), cols % N)
>>> (rows[:, None]**2 + cols**2 <= radius**2).astype(int)
array([[1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 1, 1, 1, 1, 1, 1]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.