在直方图中查找语言环境最小值(一维数组)(Python)

Find locale minimum in histogram (1D array) (Python)

我已经处理了雷达图像,为了检测水,我必须在直方图中找到局部最小值。每个区域的直方图都略有不同,因此我必须根据每个直方图自动找到局部最小值。

我的输入数组是图像值的一维数组(0.82154、0.012211、...)。我知道如何在 numpy 和 matplotlib 中创建直方图,但我不知道我应该怎么做才能确定图片中显示的语言环境最小值。我使用 python scipy 个库。

第一步应该是平滑直方图以便于确定最小值,你能告诉我用什么来平滑数据吗?像这样:

您可以使用 numpy.convolve() 使用 numpy 平滑数据,或者您可以使用以下函数:

import numpy

def smooth(x,window_len=11,window='hanning'):
    if x.ndim != 1:
        raise ValueError, "smooth only accepts 1 dimension arrays."

    if x.size < window_len:
        raise ValueError, "Input vector needs to be bigger than window size."


    if window_len<3:
        return x


    if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
        raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"


    s=numpy.r_[x[window_len-1:0:-1],x,x[-2:-window_len-1:-1]]
    #print(len(s))
    if window == 'flat': #moving average
        w=numpy.ones(window_len,'d')
    else:
        w=eval('numpy.'+window+'(window_len)')

    y=numpy.convolve(w/w.sum(),s,mode='valid')
    return y

另请查看 scipy 文档:

如果您正在寻找 1d 数组中的所有条目 a 小于它们的邻居,您可以尝试

numpy.r_[True, a[1:] < a[:-1]] & numpy.r_[a[:-1] < a[1:], True]

在 SciPy >= 0.11 您可以使用以下内容:

import numpy as np
from scipy.signal import argrelextrema

x = np.random.random(12)

# for local minima
argrelextrema(x, np.less)