1

I have a set of points, all together they create a track, where the sequence is crucial. I can plot the track using lines, how could I smooth it after or while new points are fetching? Track might look like 1 pic:

Picture One Picture One

Picture Two Picture Two

Picture Three Picture Three

2 picture is what I want to have in the end. I tried to interpolate with scipy.interpolate but it didn't work, because it requires sorted sequence (I only achieved pic3 in the end)

rassar
  • 5,412
  • 3
  • 25
  • 41
  • possible duplicate of https://stackoverflow.com/questions/20618804/how-to-smooth-a-curve-in-the-right-way – quant Nov 15 '18 at 22:19
  • @quant as I see, this post uses interpolation of the line with direction from left to right, but my data (track) could be from left to right/right to left and others and have loops. So with interpolation it will be the same as pic2 from my post. – Sergey Vladimirovich Nov 15 '18 at 22:32
  • In this case, I would probably use Bézier curves - What do you think of something like this: http://www.efg2.com/Lab/Graphics/Jean-YvesQueinecBezierCurves.htm ? – quant Nov 15 '18 at 22:38
  • yep, it close to the answer, but still smth not I want. Because the smoothed line will be far from the corners. In my case, the smoothed line should be close to straight lines and should round corners as close as possible to the corner. – Sergey Vladimirovich Nov 15 '18 at 23:05

2 Answers2

4

It sounds like a different interpolation method or approach might get what you want. A cubic spline will get you straighter lines with curves at the vertices, as utilized from the scipy libary and this sample set of loop points:

import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate

arr = np.array([[0,0],[2,.5],[2.5, 1.25],[2.6,2.8],[1.3,1.1]])
x, y = zip(*arr)
#in this specific instance, append an endpoint to the starting point to create a closed shape
x = np.r_[x, x[0]]
y = np.r_[y, y[0]]
#create spline function
f, u = interpolate.splprep([x, y], s=0, per=True)
#create interpolated lists of points
xint, yint = interpolate.splev(np.linspace(0, 1, 100), f)
plt.scatter(x, y)
plt.plot(xint, yint)
plt.show()

enter image description here

And original straight line would look like this:

enter image description here

AlecZ
  • 546
  • 5
  • 9
  • how should we go to evaluate the spline fits in specific values (instead of 100 evenly spaced distance values)? – AJMA Apr 19 '19 at 15:53
0

I was facing the same issue to smooth a path, the shapelysmooth package did the trick for me.

In my case, the best output was using Chaikins-Algorithm as I just wanted to remove some corners of the path.
In the issue case (keep the corners), the Catmull-Rom Spline might be a better option.