简体   繁体   中英

Python - Interpolate 2D point cloud using splines

I'm attempting to fit a 2D point-cloud (x and y coordinates). So far, I've had limited success with scipy's interpolation packages, most notable UnivariateSpline , which produced the following (sub-optimal) fit ( please ignore the colors ): 单变量样条

However, this is obviously the wrong package, since I need a final curve that can bend back in on itself (so no longer a 1d function) near the edges of my parabolic point-cloud.

I then read up on interp2d , for example, but don't understand what my z array would be. Is there a better class of packages that I'm perhaps overlooking?


Update 1 : as suggested in the comments, I've redone this using scipy.interpolate.splprep ; my general setup is:

from scipy.interpolate import splprep, splev
pts = np.vstack((X.ravel(), Y.ravel)) #X and Y contain my points
(tck, u), fp, ier, msg = splprep(pts, u=None, per=0, k=3, full_output=True) #s = optional parameter (default used here)
print('Spline score:',fp) #goodness of fit flatlines after a given s value (and higher), which captures the default s-value as well 
x_new, y_new = splec(u_new, tck, der=0)
plt.plot(x_new, y_new, 'k')
plt.show()

The plot is below. Can anyone suggest a method for automating the decision of s ... possible a loop while assessing coefficient of determination of each s plot? Or is there something baked in?

splprep


Update 2 : I've since re-run this on two different point-clouds, and found that the ordering of the points significantly alters the outcome. When I re-order the points in the point-cloud to be along an initial fit to a parabola, I get much better results with my spline. As well, the results are still sub-optimal, as can be seen below.

样条拟合 1(默认 s 值) 样条拟合 2(默认 s 值

Are there any further adjustments I can make with this method? Alternatively, does anyone have a suggestion for a competitive approach I can investigate?


Update 3 : actually, setting knots = 5 helps out tremendously: 节数=5

I faced a similar issue, and the solution to my problem was to use Principal Curves ( https://hastie.su.domains/Papers/Principal_Curves.pdf ).

This implementation available on GitHub could help you: https://github.com/zsteve/pcurvepy .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM