Local Maxima with circular window
<p>I am trying to compute a local maxima filter on a matrix, using a circular kernel.
The output should be the cells that are local maximas. For each pixel in the input 'data', I need to see if it is a local maximum by a circular window, thus returning a value of 1, otherwise 0.</p>

<p>I have this code, built upon answers from here:
<a href="http://stackoverflow.com/questions/8647024/how-to-apply-a-disc-shaped-mask-to-a-numpy-array">How to apply a disc shaped mask to a numpy array?</a></p>

<pre><code>import numpy as np
import scipy.ndimage as sc

radius = 2
kernel = np.zeros((2*radius+1, 2*radius+1))
y,x = np.ogrid[-radius:radius+1, -radius:radius+1]
mask2 = x**2 + y**2 &lt;= radius**2
kernel[mask2] = 1

def local_maxima(matrix, window_size):
    loc_max = sc.maximum_filter(matrix, window_size, mode='constant')
    return loc_max


data = np.array([(1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 4, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1)])

loc_max = sc.filters.generic_filter(data, local_maxima(data, np.shape(kernel)), footprint=kernel)
max_matrix = np.where(loc_max == data, 1, 0)
np.savetxt('.....\Local\Test_Local_Max.txt', max_matrix, delimiter='\t')
</code></pre>

<p>The kernel has this shape:</p>

<pre><code>[[ 0.  0.  1.  0.  0.]
 [ 0.  1.  1.  1.  0.]
 [ 1.  1.  1.  1.  1.]
 [ 0.  1.  1.  1.  0.]
 [ 0.  0.  1.  0.  0.]]
</code></pre>

<p>So the search cells will be only the ones that have value 1. The cells with 0 should be excluded from the local maxima search.</p>

<p>But the script gives the error below on line 21:</p>

<blockquote>
<pre><code>RuntimeError: function parameter is not callable
</code></pre>
</blockquote>

<p>Thanks for any help!</p>

<p>The second parameter of <code>sc.filters.generic_filter()</code> should be a function, you are passing it the value returned by the <code>local_maxima(data, np.shape(kernel))</code> call, i.e. a matrix.</p>

<p>I'm a bit confused as to what exactly you have done here, but I think you do not need the <code>generic_filter</code> call at all, <code>maximum_filter</code> should do what you want:</p>

<pre><code>import numpy as np
import scipy.ndimage as sc

radius = 2
kernel = np.zeros((2*radius+1, 2*radius+1))
y,x = np.ogrid[-radius:radius+1, -radius:radius+1]
mask2 = x**2 + y**2 &lt;= radius**2
kernel[mask2] = 1

data = np.array([(1, 1, 1, 1, 1, 1, 1, 1, 1), 
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 4, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1)])

loc_max = sc.maximum_filter(data, footprint=kernel, mode='constant')
max_matrix = np.where(loc_max == data, 1, 0)
np.savetxt('.....\Local\Test_Local_Max.txt', max_matrix, delimiter='\t')
</code></pre>

<p>(I do not have python installed on this computer so I have not tested this out, sorry)</p>

<p>Edit:
I've tested it and it seems to give the correct result:</p>

<pre><code>[[1, 1, 1, 1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1, 0, 1, 1, 1, 1],
 [1, 1, 1, 0, 0, 0, 1, 1, 1],
 [1, 1, 0, 0, 1, 0, 0, 1, 1],
 [1, 1, 1, 0, 0, 0, 1, 1, 1],
 [1, 1, 1, 1, 0, 1, 1, 1, 1],
 [1, 1, 1, 1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1, 1, 1, 1, 1]]
</code></pre>

python numpy filtering