title: Mark edges in an image
body: <p>I wrote a function to mark the edges of objects in pixel-level labeled images:</p>
  
  <pre><code>import numpy as np
  
  def mark_edges(image, marker):
  
       axes = len(image.shape)
       edges = []
  
       for i in range(axes):
           shiftright = np.greater(image, np.roll(image, 1, axis=i))
           shiftleft = np.greater(image, np.roll(image, -1, axis=i))
           idx = np.where(shiftright != shiftleft)
           edges.append(idx)
  
       for idx in edges:
           image[idx] = marker
  
       return image
  </code></pre>
  
  <p>For isolated objects this works fine, since every label is greater than the background:</p>
  
  <pre><code>a = np.zeros(40).reshape(5,8)
  a[1:4, 1:7] = 2
  print(mark_edges(a, 99))
  
  [[  0.   0.   0.   0.   0.   0.   0.   0.]
   [  0.  99.  99.  99.  99.  99.  99.   0.]
   [  0.  99.   2.   2.   2.   2.  99.   0.]
   [  0.  99.  99.  99.  99.  99.  99.   0.]
   [  0.   0.   0.   0.   0.   0.   0.   0.]]
  </code></pre>
  
  <p>But if two differently labeled objects are next to each other the outcome is slightly different:</p>
  
  <pre><code>b = np.zeros(40).reshape(5,8)
  b[1:4, 1:4] = 2
  b[1:4, 4:7] = 4
  print(mark_edges(b, 99))
  
  [[  0.   0.   0.   0.   0.   0.   0.   0.]
   [  0.  99.  99.  99.  99.  99.  99.   0.]
   [  0.  99.   2.   2.  99.   4.  99.   0.]
   [  0.  99.  99.  99.  99.  99.  99.   0.]
   [  0.   0.   0.   0.   0.   0.   0.   0.]]
  </code></pre>
  
  <p>Ideally there should be another marker at position (2,3). Do I have to accept this inaccuracy or is there a way around?</p>