title: Fitting B-spline 'around' points
body: <p>I have a large number of points representing image contours, they are generated with scikit-image and look as such</p>
  
  <p>Source-image</p>
  
  <p><img src="https://i.stack.imgur.com/NaaJC.jpg" alt="Source-image"></p>
  
  <p>Contours</p>
  
  <p><img src="https://i.stack.imgur.com/hDVSa.png" alt="Contours"></p>
  
  <p>I would like to fit a B-spline around these points such that the resulting line best approximates the contours of the image</p>
  
  <pre class="lang-py prettyprint-override"><code>c_sample = cts.sample(50)
  
  tck, u = si.splprep(c_sample.values.T, u=None, s=1.0, per=1) 
  u_new = np.linspace(u.min(), u.max(), 1000)
  x_new, y_new = si.splev(u_new, tck, der=0)
  
  plt.plot(c_sample.values[:,0], c_sample.values[:,1], 'ro')
  plt.plot(x_new, y_new, 'b--')
  plt.show()
  </code></pre>
  
  <p>My process now is taking a sample of the points along the contour and using scipy splprep and splev to prepare and evaluate the curves. But the resulting spline darts all around the point cloud, and does not fill them in a neatly closed shape.I used the <em>per</em> keyword argument to denote the shape should be closed</p>
  
  <p>Fitted spline attempt</p>
  
  <p><img src="https://i.stack.imgur.com/dzlOI.png" alt="Fitted spline attempt"></p>
  
  <p>I am looking for some direction in how to achieve the desired result, are my points being used as control points and not fit around ? is the order of points to fit around important ?</p>
  
  <p>any direction or help would be greatly appreciated</p>