从2D可能的矩阵(numpy阵列)中提取局部最大值(坐标)



我正在尝试从numpy 2D矩阵中提取本地最大值的坐标。这些值在0到1之间表示对象位于该位置的可能性。

我尝试了阈值 - 矩阵并提取grgmax并保存坐标并将其值更改为0并循环直到遇到阈值。

 detections = []
 while True:
    maxloc = np.unravel_index(np.argmax(scmap),
                              scmap.shape)
    if scmap[maxloc] > 0.9:
        # other code ..
        detections.append(maxloc)
        scmap[maxloc] = 0
# after that, what i did is calculating the euclidean distance 
# between each pair and excluded the ones that does not meet the  
# threshold

我对此不满意,我认为有更有效的优雅方法来提取本地最大值。想法?

定位本地最大值是Scikit-image的内置功能,它位于某个预定义的距离内最大值的值。

from skimage.feature import peak_local_max
coordinates = peak_local_max(scmax, min_distance=5)

我不确定如何实际实现它,但是一种实现方法是执行非最大抑制(即遍历矩阵中的每个值并与半径内的所有值进行比较。如果该值在该窗口中不是最大的值然后将其设置为一些预定的值,例如零或-inf(。然后将所有非抑制值(可能高于某个阈值(的坐标作为本地最大值的集合。

如果要提取符合特定阈值的所有值的numby矩阵的坐标,则可以将阈值与整个矩阵进行比较。

import numpy as np
data = np.array([
    [0, 0.5, 0.95],
    [0, 0.95, 0.5],
    [0.95, 0.5, 0]
])
thresholded_coordinates = np.argwhere(data > 0.9)
# array([[0, 2], [1,1], [2, 0]])

阈值_coordinates的输出是坐标的成对集合。(0,2(表示它是第一行(0索引(中的第三个值。输出在最后一行的评论中指示。