简体   繁体   English

在python中应用具有周期性边界条件的圆形掩模

[英]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.

相关问题 具有周期性边界条件的DBSCAN python - DBSCAN python with periodic boundary conditions python中的有效符号函数,用于周期性边界条件的情况 - efficient sign function in python for case of periodic boundary conditions 优化Python距离计算,同时考虑周期性边界条件 - Optimizing Python distance calculation while accounting for periodic boundary conditions 考虑到数据中的周期性边界条件,如何在python移动平均中应用 - how to apply in python mobile averaging considering periodic boundary conditions in data 具有周期性边界条件的数据的直方图 - Histogram for data with periodic boundary conditions 具有周期边界条件的接触/协调数的计算 - Calculation of Contact/Coordination number with Periodic Boundary Conditions 在 Pyomo 约束中定义循环/周期性边界条件 - Defining cyclic/periodic boundary conditions in Pyomo constraints 在多维网格中实现周期性边界条件 - Implementing periodic boundary conditions in multidimensional grid 具有周期性边界条件的np.ndarray - np.ndarray with Periodic Boundary conditions 将边界条件应用于Keras输出 - Applying Boundary Conditions to Keras Output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM