[SciPy-user] Extrema finding

Alan Jackson alan@ajackson....
Fri Oct 10 20:14:49 CDT 2008


On Thu, 9 Oct 2008 13:06:33 -0600
David Bolme <bolme1234@comcast.net> wrote:

> This is a version for finding extrema in a 2D array.  It requires the  
> ndimage maximum/minimum filters.  For the one dimensional case  
> substitute size=[3] or use maximum_filter1d.  I keep writing this code  
> over and over.  I am surprised that there is not a general purpose  
> extrema finding routine in scipy.
> 
>      def localMax(mat):
>          mx = maximum_filter(mat, size=[3,3])
>          mn = minimum_filter(mat, size=[3,3])
> 	
>          # (mat == mx) true if pixel is equal to the local max
>          # The next computation suppresses responses where
>          # the function is flat.
>          local_maxima = ((mat == mx) & (mat != mn))
> 	
>          # Get the indices of the maxima
>          extrema = nonzero(local_maxima)
>          return extrema
> 
> 
Here's one I wrote a few months back...

def extrema(trace)
    a = np.sign(np.diff(trace))
    zerolocs = np.transpose(np.where(a[1:]+a[0:-1]==0.)).flatten() + 1
    if zerolocs[0] < 1:
        zerolocs = zerolocs[1:]
    if zerolocs[-1]>len(a)-2:
        zerolocs = zerolocs[0:-1]
    return zerolocs

-- 
-----------------------------------------------------------------------
| Alan K. Jackson            | To see a World in a Grain of Sand      |
| alan@ajackson.org          | And a Heaven in a Wild Flower,         |
| www.ajackson.org           | Hold Infinity in the palm of your hand |
| Houston, Texas             | And Eternity in an hour. - Blake       |
-----------------------------------------------------------------------


More information about the SciPy-user mailing list