我正在尝试从 numpy 2D 矩阵中提取局部最大值的坐标。这些值是 0 到 1 之间的数字,表示对象位于该位置的可能性。
我尝试对矩阵进行阈值处理并提取 argmax 并保存坐标并将其值更改为 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)。然后将所有非抑制值(可能高于某个阈值)的坐标作为局部最大值的集合。
关于python - 有效地从二维似然矩阵(numpy 数组)中提取局部最大值(坐标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56943362/