def detect_local_minima(arr):
    """
    Takes an array and detects the troughs using the local maximum filter.
    Returns a boolean mask of the troughs (i.e. 1 when
    the pixel's value is the neighborhood maximum, 0 otherwise)
    """
neighborhood = morphology.generate_binary_structure(len(arr.shape), 2)
local_min = filters.minimum_filter(arr, footprint=neighborhood) == arr
background = arr == 0
eroded_background = morphology.binary_erosion(background, structure=
    neighborhood, border_value=1)
detected_minima = local_min - eroded_background
return np.where(detected_minima)
Как эффективно найти локальные минимумы гладкого многомерного массива в NumPy?