title: Find distance to ones in a binary numpy array in python
body: <p>For a robotics project, I've used ultrasound as vision. From edge detection algorithms I've generated a binary numpy array. Now, I'm not sure what is the most cost efficient way of calculating the distance to the object.    Say I wanted to calculated the shortest distanse from a one to the top left corner? Would it be possible to use "np.where" and "dst = numpy.linalg.norm( )"?</p>
  
  <pre><code>import numpy as np
  from scipy import ndimage
  from PIL import Image
  
  
  Max_filtrated = np.where(result&gt;np.amax(result)*0.8,0,result)
  Band_filtrated = np.where(Max_filtrated&gt;np.amax(Max_filtrated)*0.11, 
  1,0)
  
  ####### Define connected region and remove noise ########
  mask = Band_filtrated&gt; Band_filtrated.mean()
  label_im, nb_labels = ndimage.label(mask)
  sizes = ndimage.sum(mask, label_im, range(nb_labels + 1))
  mean_vals = ndimage.sum(im, label_im, range(1, nb_labels + 1))
  mask_size = sizes &lt; 500
  remove_pixel = mask_size[label_im]
  label_im[remove_pixel] = 0
  Ferdig= np.where(label_im&gt;np.amax(label_im)*0.1,1,0)
  #########################################################
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/vvALd.png" alt="enter image description here"></p>
  
  <p>Thanks</p>