我有一個二維的numpy數組,我想在幾個特定的元素周圍屏蔽圓形區域。這對於不在邊緣附近的元素很容易。但是當光盤在靠近數組邊緣的位置截斷時,掩碼操作會引發異常。在2D數組的邊緣截斷的圓形的圓形遮罩切割
掩蓋這些邊緣來源的好方法是什麼?定期填充..?謝謝!
下面是一些代碼:
import numpy as np
x_count = 20000
y_count = 20000
a = np.arange(y_count * x_count).reshape(y_count, x_count) # data
master_mask=np.zeros(a.shape).astype(bool)
coords=([10,50],[500,400])
nsrc=len(coords)
mask_radius=100 # 10, 20, etc.
for isrc in range(nsrc):
xc=coords[isrc][0]; yc=coords[isrc][1]
x,y = np.ogrid[-mask_radius:mask_radius+1,-mask_radius:mask_radius+1]
minimask = x*x + y*y <= mask_radius*mask_radius
box=master_mask[xc-mask_radius:xc+mask_radius+1,yc-mask_radius:yc+mask_radius+1]
assert(minimask.shape==box.shape), 'This assertion should fail for discs touching the edge'
master_mask[xc-mask_radius:xc+mask_radius+1,yc-mask_radius:yc+mask_radius+1] += minimask
print master_mask
我們展示你的代碼... – shx2 2014-12-27 20:07:28