在三维数组中查找最大和的位置



我有一个2D NumPy数组的时间序列,我想找出具有特定窗口大小(n(的最大和的位置(中心(,

我试过下面这样的conv2:

from scipy.signal import convolve2d as conv2
def largest_sum_pos_app1(a, n):
idx = conv2(a, np.ones((n,n),dtype=int),'same').argmax()
return np.unravel_index(idx, a.shape)

然而,这将提供单个2D阵列的位置,现在我想根据历史序列找出窗口的位置。numpy或scipy中是否有内置模块来处理这样的3D阵列。

举个例子:的输入阵列

([[0 1 4 0] 
[1 2 5 1]
[2 3 6 0]],
[[1 2 9 4]
[2 4 6 2]
[1 5 1 3]],
[[0 2 3 1]
[0 3 5 0]
[1 4 6 1]])

取大小为3 x 3的窗口,每个窗口的总和为:

[[24 22]
[31 36]
[24 25]]

现在,当我们取总和为[79 83]时,我会选择第二个窗口。这是一个简单的例子,但我有一个更大的数组和数千个时间步长。有没有一种方法可以在没有任何循环的情况下处理这个问题。

您可能想要oaconvolve,它可以处理多个维度,并允许您选择要操作的维度。假设您有一个形状为(k, width, height)的数组a,其中k是平面数:

from scipy.signal import oaconvolve
c = oaconvolve(a, np.ones((1, n, n)), axes=(-2, -1), mode='same')
idx = c.reshape(a.shape[0], -1).argmax(axis=1)
result = np.unravel_index(idx, a.shape[1:])

这不允许您选择进行卷积的方法,因此它可能不是算法的最佳选择。

您的问题可能需要一点澄清,但我假设您的"窗口";是重叠的。在这种情况下,你可以做:

import numpy as np
import scipy.ndimage as ndi
def largest_sum_pos_app1(a, n):
# assumes that your data is arranged as (time, y, x)
# uniform_filter will essentially calculate the sum of all pixels in a
# neighborhood around each pixel in your original array
window_sums = ndi.uniform_filter(a, n)
# to find the index we use argmax, but that requires a little acrobatics
max_idx = window_sums.reshape((len(a), -1)).argmax(1)
# the result of unravel_index is designed to be used for NumPy fancy indexing,
# so we need to reshape it.
coords = np.array(np.unravel_index(max_idx, a.shape[1:])).T
# the result is the y, x coordinates for each time point
return coords

需要注意的一件事是uniform_filtermode参数,它决定了如何处理图像的边缘。默认情况是只在边上填充零,这可能是你想要的,也可能不是。

相关内容

最新更新