title: Fast way to find locally maximal gradient values in a numpy array?
body: <p>I have a 2-d array for which I want to detect all locally maximal array indices. That is, given an index (i, j), its maximum gradient is the largest absolute change from any of its 8 neighboring values:</p>
  
  <pre><code>Index: (i, j)
  
  Neighbors:
  (i-1,j+1)  (i,j+1)  (i+1,j+1)
  (i-1,j)    [index]    (i+1,j)
  (i-1,j-1)  (i,j-1)  (i+1,j-1)
  
  Neighbor angles:
  315           0            45
  270        [index]         90
  225          180          135
  
  MaxGradient(i,j) = Max(|Val(index) - Val(neighbor)|)
  </code></pre>
  
  <p>The index is said to be <em>locally maximal</em> if its MaxGradient is at least as large as any of its neighbors' own MaxGradients.</p>
  
  <p>The output of the algorithm should be a 2-d array of tuples, or a 3-d array, where for each index in the original array, the output array contains a value indicating if that index was locally maximal and, if so, the angle of the gradient.</p>
  
  <p>My initial implementation simply passed over the array twice, once to calculate the max gradients (stored in a temporary array) and then once over the temp array to determine the locally maximal indices. Each time, I did this via for loops, looking at each index individually.</p>
  
  <p>Is there some more efficient way to do this in numpy?</p>