在直方图中查找区域设置最小值(一维数组)(Python) [英] Find locale minimum in histogram (1D array) (Python)

查看:307
本文介绍了在直方图中查找区域设置最小值(一维数组)(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have processed radar image and to detect water I have to find local minimum in the histogram. Histogram is little bit different for every area so I have to automatically find local minimum based on every histogram.

我的输入数组是图像值的一维数组(0.82154、0.012211等).我知道如何在numpy和matplotlib中创建直方图,但是我不知道如何确定图片中显示的最小区域设置.我使用python scipy库.

My input array is 1D array of image values (0.82154, 0.012211,...). I know how to create histogram in numpy and matplotlib but I do not know what should I do to determine locale minimum which is showed in the picture. I use python scipy libraries.

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

First step should be to smooth the histogram for easier determination of minimum, could you tell me what to use to smooth data ? Something like this:

推荐答案

您可以使用numpy.convolve()的numpy平滑数据,也可以使用以下功能:

You can smooth the data with numpy with numpy.convolve() or you can use the following function:

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文档:

Also please take a look at the scipy documentation:

如果您要查找1d数组a中所有小于其邻居的条目,则可以尝试

If you are looking for all entries in the 1d array a smaller than their neighbors, you can try

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

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

In SciPy >= 0.11 you can use the following:

import numpy as np
from scipy.signal import argrelextrema

x = np.random.random(12)

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

这篇关于在直方图中查找区域设置最小值(一维数组)(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆