我需要在不连续的步骤中平滑包含高程的2D numpy数组。这里我Test Execution
位于z-value = 1 (elevation)
但是,它可以是介于0和1之间的任何值。我的数组如下所示:
(0, 0)
我尝试使用['1.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
应用余弦内核,然后使用scipy
。像这样:
convolve2d
这导致数组看起来像这样:
import numpy as np
from scipy import signal
peak_array = np.array([
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
def elevation_grid():
"""Creates a smooth elevation-grid, from an array of peaks."""
def gkern():
"""Returns a 2D Triangle kernel array."""
# Decay is a cosine.
# It is centered, so multiply by two, and add one
# This way, we cover the entire grid, and assert
# equal smoothing (due to an odd width)
gkern1d_c = signal.cosine(peak_array.shape[0]*2 + 1)
gkern1d_r = signal.cosine(peak_array.shape[1]*2 + 1)
gkern2d = np.outer(gkern1d_c, gkern1d_r)
return gkern2d
kernel = gkern()
grad = signal.convolve2d(peak_array, kernel, mode='same')
# Normalize the grid
grad -= np.amin(grad)
grad /= np.amax(grad)
return grad
def print_readable(array):
"""Prints the map to a human-readable format."""
for row in range(0, array.shape[0]):
# Round to two decimals
r = ["%.2f" % array[col][row] for col in range(0, array.shape[1])]
print(r)
smooth_array = elevation_grid()
print_readable(smooth_array)
预期结果如何。但是,如果我在每个角落放置峰值:
['1.00', '0.99', '0.95', '0.90', '0.82', '0.72', '0.60', '0.47', '0.33', '0.18']
['0.99', '0.98', '0.94', '0.89', '0.81', '0.71', '0.60', '0.47', '0.33', '0.18']
['0.95', '0.94', '0.91', '0.85', '0.78', '0.68', '0.57', '0.45', '0.32', '0.17']
['0.90', '0.89', '0.85', '0.80', '0.73', '0.64', '0.54', '0.42', '0.29', '0.16']
['0.82', '0.81', '0.78', '0.73', '0.67', '0.59', '0.49', '0.38', '0.27', '0.14']
['0.72', '0.71', '0.68', '0.64', '0.59', '0.51', '0.43', '0.33', '0.23', '0.12']
['0.60', '0.60', '0.57', '0.54', '0.49', '0.43', '0.36', '0.28', '0.19', '0.09']
['0.47', '0.47', '0.45', '0.42', '0.38', '0.33', '0.28', '0.21', '0.14', '0.06']
['0.33', '0.33', '0.32', '0.29', '0.27', '0.23', '0.19', '0.14', '0.09', '0.03']
['0.18', '0.18', '0.17', '0.16', '0.14', '0.12', '0.09', '0.06', '0.03', '0.00']
他们在中间加起来导致中心的一个更大的峰值,我期望中间的最低值......
['1.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '1.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00']
['1.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '1.00']
如何抚平我的山峰? 它需要高效,因为我有大型阵列(高达1000x1000)。
答案 0 :(得分:1)
因此,如果k是你的内核,并且*是卷积,并且v1,... v4输入矩阵就像你发布的第一个矩阵一样,你可以将它们相加并获得另一个矩阵作为w
w = v1 + v2 + v3 + v4
您正在应用卷积,这是一个线性操作。
输出= k * w = k *(v1 + v2 + v3 + v4)= k * v1 + k2 * v2 ...
如果你注意到,你在拐角处输入四个1(Inp2)与初始矩阵的四个总和基本相同,只有一个1(Inp1)。
你可以通过简单地旋转90度Inp1和求和(在numpy,transposes和fliprl中)来构建Inp2。 因此,您可以旋转第一个计算的结果并将它们相加以找到正确的最终结果。
Inp1的四个旋转版本的总和不太可能与适用于Inp2
的卷积不同。
如果是这种情况,则可能是一个数字问题(如果您使用这些数字则不应该发生)。检查如何使用max和min重新缩放数据。你除以max,所以要确保它是非负的,超过eps