title: Finding first non-zero value in an image
body: <p>I'm pretty new to Python and I would like to locate the extremes of a binary image. There's a white shape in the middle of a black background and I would like to locate the top, bottom, left and right enclosing rectangle.</p>
  
  <p>My way of doing this is by finding the first non-zero pixels in all directions.</p>
  
  <p>My function is goes as this, but it only works on the Y axis. How can I manage to pass through the X axis?</p>
  
  <pre><code>def first_non_zero(img):
      width = img.shape[1]
      height = img.shape[0]
  
      idx = 0
      result = 0
  
      for j in range(0, height):
          idx = np.argmax(img[j])
  
          if idx &gt; 0:
              result = j
              break
  
      return result
  </code></pre>