def neighbors(arr, x, y, N):
left = max(0, x - N)
right = min(arr.shape[0], x + N)
top = max(0, y - N)
bottom = min(arr.shape[1], y + N)
window = arr[left:right+1,top:bottom+1]
fillval = window.mean()
result = np.empty((2*N+1, 2*N+1))
result[:] = fillval
ll = N - x
tt = N - y
result[ll+left:ll+right+1,tt+top:tt+bottom+1] = window
return result
你可以pad你的数组。填充将使用所需的边界条件扩展数组(有关所有可能的选项,请参见
mode
参数):如您所见,原始数组位于矩阵的中心,由2行2列(
N//2 = 5//2 = 2
,从lef/right和bottom/top)填充。填充元素被反射。在对于这个新数组,您可以通过正常索引数组来访问所需的邻居窗口:
^{pr2}$您可以选择其他填充方法,mean是选项之一。在
这里有一种使用^{} 和^{} -
样本运行-
^{pr2}$我想这不算太远:
相关问题 更多 >
编程相关推荐