title: sorting points to form a continuous line
body: <p>I have a list of (x,y)-coordinates that represent a line skeleton.
  The list is obtained directly from a binary image:</p>
  
  <pre><code>import numpy as np    
  list=np.where(img_skeleton&gt;0)
  </code></pre>
  
  <p>Now the points in the list are sorted according to their position in the image along one of the axes.</p>
  
  <p>I would like to sort the list such that the order represents a smooth path along the line. (This is currently not the case where the line curves back).
  Subsequently, I want to fit a spline to these points.</p>
  
  <p>A similar problem has been described and solved using arcPy <a href="https://gis.stackexchange.com/questions/156888/changing-sequence-of-point-features-into-order-along-line-using-arcpy">here</a>. Is there a convenient way to achieve this using python, numpy, scipy, openCV (or another library?)</p>
  
  <p>below is an example image. it results in a list of 59 (x,y)-coordinates.
  <a href="https://i.stack.imgur.com/KOtXA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KOtXA.png" alt="enter image description here"></a></p>
  
  <p>when I send the list to scipy's spline fitting routine, I am running into a problem because the points aren't 'ordered' on the line:</p>
  
  <p><a href="https://i.stack.imgur.com/ynLXy.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ynLXy.png" alt="enter image description here"></a></p>