python – 找到邻近最大值的numpy数组坐标

我使用接受的答案
in this question来获取2个或更多维度的numpy数组中的局部最大值,因此我可以为它们分配标签.现在我还想根据梯度将这些标签分配给阵列中的相邻单元 – 即,单元获得与具有最高值的相邻单元相同的标签.这样我就可以迭代地为整个数组分配标签.

假设我有一个数组A

>>> A = np.array([[ 1. ,  2. ,  2.2,  3.5],
                  [ 2.1,  2.4,  3. ,  3.3],
                  [ 1. ,  3. ,  3.2,  3. ],
                  [ 2. ,  4.1,  4. ,  2. ]])

应用我得到的maximum_filter

>>> scipy.ndimage.filters.maximum_filter(A, size=3)
array([[ 2.4,  3. ,  3.5,  3.5],
       [ 3. ,  3.2,  3.5,  3.5],
       [ 4.1,  4.1,  4.1,  4. ],
       [ 4.1,  4.1,  4.1,  4. ]])

现在,对于这个数组中的每个单元格,我希望得到过滤器找到的最大值的坐标,即

array([[[1,1],[1,2],[0,3],[0,3]],
       [[2,1],[2,2],[0,3],[0,3]],
       [[3,1],[3,1],[3,1],[3,2]],
       [[3,1],[3,1],[3,1],[3,2]]])

然后我会使用这些坐标迭代地分配我的标签.

我可以使用循环来做两个维度,忽略边框

highest_neighbor_coordinates = np.array([[(argmax2D(A[i-1:i+2, j-1:j+2])+np.array([i-1, j-1])) for j in range(1, A.shape[1]-1)] for i in range(1, A.shape[0]-1)])

但是在看到scipy.ndimage中的许多过滤器函数后,我希望有一个更优雅和可扩展(到> = 3维)的解决方案.

最佳答案 我们可以使用具有反射元素的垫来模拟最大滤波器操作并使用
scikit-image‘s view_as_windows获得其上的滑动窗口,计算平坦的argmax指数,抵消具有范围值的那些以转换到全球范围 –

from skimage.util import view_as_windows as viewW

def window_argmax_global2D(A, size):
    hsize = (size-1)//2 # expects size as odd number
    m,n = A.shape
    A1 = np.pad(A, (hsize,hsize), mode='reflect')
    idx = viewW(A1, (size,size)).reshape(-1,size**2).argmax(-1).reshape(m,n)

    r,c = np.unravel_index(idx, (size,size))
    rows = np.abs(r + np.arange(-hsize,m-hsize)[:,None])
    cols = np.abs(c + np.arange(-hsize,n-hsize))
    return rows, cols    

样品运行 –

In [201]: A
Out[201]: 
array([[1. , 2. , 2.2, 3.5],
       [2.1, 2.4, 3. , 3.3],
       [1. , 3. , 3.2, 3. ],
       [2. , 4.1, 4. , 2. ]])

In [202]: rows, cols = window_argmax_global2D(A, size=3)

In [203]: rows
Out[203]: 
array([[1, 1, 0, 0],
       [2, 2, 0, 0],
       [3, 3, 3, 3],
       [3, 3, 3, 3]])

In [204]: cols
Out[204]: 
array([[1, 2, 3, 3],
       [1, 2, 3, 3],
       [1, 1, 1, 2],
       [1, 1, 1, 2]])

延伸到n-dim

我们将使用np.ogrid作为此扩展部分:

def window_argmax_global(A, size):
    hsize = (size-1)//2 # expects size as odd number
    shp = A.shape
    N = A.ndim
    A1 = np.pad(A, (hsize,hsize), mode='reflect')
    idx = viewW(A1, ([size]*N)).reshape(-1,size**N).argmax(-1).reshape(shp)

    offsets = np.ogrid[tuple(map(slice, shp))]
    out = np.unravel_index(idx, ([size]*N))
    return [np.abs(i+j-hsize) for i,j in zip(out,offsets)]
点赞