嗨,我正在尝试在一个3D的numpy数组中找到局部极大值,但是我似乎找不到一个简单的方法来使用numpy、scipy或其他任何东西。在
现在我使用scipy.signal.argrelexrema
实现了它。但处理大数组的时间很长,而且只在分离轴上工作。在
import numpy as np
from scipy.signal import argrelextrema
def local_maxima_3D(data, order=1):
"""Detects local maxima in a 3D array
Parameters
---------
data : 3d ndarray
order : int
How many points on each side to use for the comparison
Returns
-------
coords : ndarray
coordinates of the local maxima
values : ndarray
values of the local maxima
"""
# Coordinates of local maxima along each axis
peaks0 = np.array(argrelextrema(data, np.greater, axis=0, order=order))
peaks1 = np.array(argrelextrema(data, np.greater, axis=1, order=order))
peaks2 = np.array(argrelextrema(data, np.greater, axis=2, order=order))
# Stack all coordinates
stacked = np.vstack((peaks0.transpose(), peaks1.transpose(),
peaks2.transpose()))
# We keep coordinates that appear three times (once for each axis)
elements, counts = np.unique(stacked, axis=0, return_counts=True)
coords = elements[np.where(counts == 3)[0]]
# Compute values at filtered coordinates
values = data[coords[:, 0], coords[:, 1], coords[:, 2]]
return coords, values
我知道这个解决方案远不是最优的,而且只适用于阶数为1的情况。在python中,有没有更好的方法可以在3D数组中找到局部极大值?在
编辑:
我现在使用以下方法,这实际上要快得多,而且在订单>1时也可以使用:
^{pr2}$
假设你的数据有一些统计表示,你应该能够执行一个三维局部最大,像这样。希望这能回答你的问题。在
相关问题 更多 >
编程相关推荐