通过视频录制,可以提取行人穿过瓶颈的头的轨迹。因为,行人在移动轨道时摇摆包含令人讨厌的振荡,我想消除。 (平滑算法与此问题无关)
结果显示在figure。
中所以,我的目标是使用蓝点(称为零点)获得平滑的轨迹。为此,我想到了样条。不幸的是,scipy的函数 splrep 有限制,数据应该相对于x轴排序(为什么?)。
然而在我的情况下,排序数据点不是一个好主意,然后它会改变真实的轨迹。样条的结果排序后的看起来like。
(图的最后一部分是第二部分,用于展示振荡。)
从数学的角度来看, splrep 的结果可能是正确的,但它们没有物理相关性,这是因为我没有必要操纵轨迹。
如何用python(scipy)正确解决这个问题?
如果有人想要播放数据,这是我的代码。
from matplotlib.pyplot import *
from numpy import *
from scipy import interpolate
# trajectory
path = array([
[ 3.83911987e+02, -3.63506010e+01],
[ 3.80407013e+02, -3.70444980e+01],
[ 3.77910004e+02, -3.73224980e+01],
[ 3.75592010e+02, -3.72258990e+01],
[ 3.72606995e+02, -3.68973010e+01],
[ 3.68860992e+02, -3.64117010e+01],
[ 3.64709991e+02, -3.54816020e+01],
[ 3.60441986e+02, -3.46052020e+01],
[ 3.56470001e+02, -3.33995020e+01],
[ 3.53148010e+02, -3.17936990e+01],
[ 3.50332001e+02, -2.99004990e+01],
[ 3.48214996e+02, -2.76462990e+01],
[ 3.45072998e+02, -2.56490000e+01],
[ 3.41787994e+02, -2.45536990e+01],
[ 3.38785004e+02, -2.41054990e+01],
[ 3.36031006e+02, -2.40007990e+01],
[ 3.33045013e+02, -2.41030010e+01],
[ 3.29737000e+02, -2.44983010e+01],
[ 3.26299011e+02, -2.53087010e+01],
[ 3.22807007e+02, -2.65634990e+01],
[ 3.19296997e+02, -2.82255000e+01],
[ 3.15854004e+02, -3.02551990e+01],
[ 3.12652008e+02, -3.22993010e+01],
[ 3.09871002e+02, -3.41482010e+01],
[ 3.07183990e+02, -3.56874010e+01],
[ 3.04494995e+02, -3.66516990e+01],
[ 3.01890015e+02, -3.71099010e+01],
[ 2.99341003e+02, -3.74341010e+01],
[ 2.96681000e+02, -3.77743990e+01],
[ 2.93803986e+02, -3.79683000e+01],
[ 2.90785004e+02, -3.77564010e+01],
[ 2.87817993e+02, -3.69802020e+01],
[ 2.84980988e+02, -3.57428020e+01],
[ 2.82131989e+02, -3.40877990e+01],
[ 2.79335999e+02, -3.21539990e+01],
[ 2.76933014e+02, -3.00962010e+01],
[ 2.74431000e+02, -2.81432000e+01],
[ 2.71325012e+02, -2.63622000e+01],
[ 2.68205994e+02, -2.47146000e+01],
[ 2.65588013e+02, -2.33606000e+01],
[ 2.63167999e+02, -2.23937000e+01],
[ 2.60816010e+02, -2.16775000e+01],
[ 2.58609985e+02, -2.10203990e+01],
[ 2.56585999e+02, -2.06432990e+01],
[ 2.54651001e+02, -2.06508010e+01],
[ 2.52639008e+02, -2.09992010e+01],
[ 2.50608002e+02, -2.17117000e+01],
[ 2.48576004e+02, -2.28300990e+01],
[ 2.46585999e+02, -2.42358000e+01],
[ 2.44641006e+02, -2.59650990e+01],
[ 2.42638000e+02, -2.79718000e+01],
[ 2.40481995e+02, -2.97957990e+01],
[ 2.38220993e+02, -3.15263000e+01],
[ 2.35929993e+02, -3.32879980e+01],
[ 2.33899994e+02, -3.47654000e+01],
[ 2.32324005e+02, -3.58550000e+01],
[ 2.30934006e+02, -3.66335980e+01],
[ 2.29505005e+02, -3.73493000e+01],
[ 2.28022995e+02, -3.80163000e+01],
[ 2.26539993e+02, -3.84826010e+01],
[ 2.25024002e+02, -3.86341020e+01],
[ 2.23253998e+02, -3.84396020e+01],
[ 2.21259995e+02, -3.78853990e+01],
[ 2.19130997e+02, -3.69795990e+01],
[ 2.16889999e+02, -3.58032990e+01],
[ 2.14451996e+02, -3.43969990e+01],
[ 2.11820007e+02, -3.27537000e+01],
[ 2.09186996e+02, -3.10618990e+01],
[ 2.07384995e+02, -2.93244990e+01],
[ 2.04218002e+02, -2.81264000e+01],
[ 2.01115997e+02, -2.68684010e+01],
[ 1.98009003e+02, -2.56004010e+01],
[ 1.95281006e+02, -2.47794000e+01],
[ 1.92839005e+02, -2.42327000e+01],
[ 1.90753006e+02, -2.39883000e+01],
[ 1.88904007e+02, -2.39496990e+01],
[ 1.87065002e+02, -2.40893000e+01],
[ 1.85164001e+02, -2.51790010e+01],
[ 1.83408005e+02, -2.58622000e+01],
[ 1.81880997e+02, -2.68085000e+01],
[ 1.80416000e+02, -2.82280010e+01],
[ 1.79287994e+02, -3.01436000e+01],
[ 1.78574997e+02, -3.22188990e+01],
[ 1.77757004e+02, -3.42841000e+01],
[ 1.76931000e+02, -3.64440990e+01],
[ 1.76029007e+02, -3.81259990e+01],
[ 1.75113998e+02, -3.96713980e+01],
[ 1.74244003e+02, -4.08652000e+01],
[ 1.73044998e+02, -4.15327990e+01],
[ 1.72110001e+02, -4.16768000e+01],
[ 1.70936005e+02, -4.17703020e+01],
[ 1.69544006e+02, -4.12965010e+01],
[ 1.67804993e+02, -4.05098000e+01],
[ 1.66028000e+02, -3.96259000e+01],
[ 1.64475998e+02, -3.81104010e+01],
[ 1.63324005e+02, -3.61517980e+01],
[ 1.62007004e+02, -3.43326990e+01],
[ 1.60423996e+02, -3.27486990e+01],
[ 1.58707993e+02, -3.09353010e+01],
[ 1.56770996e+02, -2.94423010e+01],
[ 1.54835999e+02, -2.79886000e+01],
[ 1.52953003e+02, -2.67169000e+01],
[ 1.50912994e+02, -2.57530990e+01],
[ 1.48996002e+02, -2.54090000e+01],
[ 1.47061996e+02, -2.53249000e+01],
[ 1.45320007e+02, -2.55256000e+01],
[ 1.43707993e+02, -2.60972000e+01],
[ 1.41876007e+02, -2.68913990e+01],
[ 1.40018997e+02, -2.78764000e+01],
[ 1.38016998e+02, -2.91917000e+01],
[ 1.36063004e+02, -3.06130010e+01],
[ 1.34001999e+02, -3.22636990e+01],
[ 1.31807999e+02, -3.42985000e+01],
[ 1.29455002e+02, -3.65051990e+01],
[ 1.27331001e+02, -3.85568010e+01],
[ 1.25116997e+02, -4.05253980e+01],
[ 1.23258003e+02, -4.22365990e+01],
[ 1.21709000e+02, -4.36623000e+01],
[ 1.20507004e+02, -4.48983990e+01],
[ 1.19530998e+02, -4.56188010e+01],
[ 1.18888000e+02, -4.60003010e+01],
[ 1.18030998e+02, -4.58804020e+01],
[ 1.17032997e+02, -4.53919980e+01],
[ 1.15643997e+02, -4.47540020e+01],
[ 1.14077003e+02, -4.39210010e+01],
[ 1.12264000e+02, -4.29716000e+01],
[ 1.10446999e+02, -4.18083990e+01],
[ 1.08487000e+02, -4.06045990e+01],
[ 1.06468002e+02, -3.88120990e+01],
[ 1.04453003e+02, -3.67605020e+01],
[ 1.02263000e+02, -3.47210010e+01],
[ 9.98833010e+01, -3.29178010e+01],
[ 9.76293030e+01, -3.16152000e+01],
[ 9.55121000e+01, -3.09032000e+01],
[ 9.33936000e+01, -3.05140000e+01],
[ 9.14021000e+01, -3.02255000e+01],
[ 8.95216980e+01, -3.02507990e+01],
[ 8.76836010e+01, -3.06539000e+01],
[ 8.58259960e+01, -3.10028990e+01],
[ 8.40342030e+01, -3.14128000e+01],
[ 8.25109020e+01, -3.24874990e+01],
[ 8.16477970e+01, -3.37783010e+01],
[ 8.10721970e+01, -3.54217000e+01],
[ 8.02864000e+01, -3.69583020e+01],
[ 7.93555980e+01, -3.81934010e+01],
[ 7.82388990e+01, -3.90952990e+01],
[ 7.72057040e+01, -3.95537990e+01],
[ 7.61997990e+01, -3.97468990e+01],
[ 7.54201970e+01, -3.95574000e+01],
[ 7.46388020e+01, -3.92573010e+01],
[ 7.36848980e+01, -3.86537020e+01],
[ 7.25225980e+01, -3.76381990e+01],
[ 7.13074040e+01, -3.64692990e+01],
[ 7.02815020e+01, -3.50397990e+01],
[ 6.93098980e+01, -3.34804990e+01],
[ 6.82506030e+01, -3.18008000e+01],
[ 6.69611970e+01, -3.02639010e+01],
[ 6.55927960e+01, -2.89077000e+01],
[ 6.44023970e+01, -2.77817990e+01],
[ 6.31432990e+01, -2.70891990e+01],
[ 6.19845010e+01, -2.66151010e+01],
[ 6.08012010e+01, -2.63731990e+01],
[ 5.95279010e+01, -2.63244000e+01],
[ 5.81848980e+01, -2.66625000e+01],
[ 5.67341000e+01, -2.70667000e+01],
[ 5.53522000e+01, -2.76193010e+01],
[ 5.40955010e+01, -2.89263990e+01],
[ 5.30055010e+01, -3.03295990e+01],
[ 5.19216000e+01, -3.15974010e+01],
[ 5.08109020e+01, -3.27397990e+01],
[ 4.95959010e+01, -3.36008990e+01],
[ 4.83475990e+01, -3.42673000e+01],
[ 4.69407010e+01, -3.46990010e+01],
[ 4.56283000e+01, -3.50008010e+01],
[ 4.45952990e+01, -3.53820990e+01],
[ 4.35136990e+01, -3.55634990e+01],
[ 4.24670980e+01, -3.54066010e+01],
[ 4.11960980e+01, -3.49855000e+01],
[ 3.94818000e+01, -3.43259010e+01],
[ 3.75299990e+01, -3.32298010e+01],
[ 3.54291000e+01, -3.18832000e+01],
[ 3.35419010e+01, -3.01863990e+01],
[ 3.18060000e+01, -2.83223990e+01],
[ 3.03379000e+01, -2.64284990e+01],
[ 2.90386010e+01, -2.48046000e+01],
[ 2.77717000e+01, -2.35111010e+01],
[ 2.66282010e+01, -2.24167000e+01],
[ 2.56504000e+01, -2.15105990e+01],
[ 2.45620990e+01, -2.09494990e+01],
[ 2.36200010e+01, -2.05372010e+01],
[ 2.25006010e+01, -2.02623000e+01],
[ 2.12878000e+01, -2.01607000e+01],
[ 1.99335990e+01, -2.02614990e+01],
[ 1.84620000e+01, -2.04685990e+01],
[ 1.67920000e+01, -2.09810010e+01],
[ 1.50698000e+01, -2.15951000e+01],
[ 1.31372000e+01, -2.24368000e+01],
[ 1.08794000e+01, -2.30415000e+01],
[ 8.29891000e+00, -2.31980000e+01],
[ 5.61593000e+00, -2.27952000e+01],
[ 2.89633000e+00, -2.16718010e+01],
[ 8.38298000e-01, -2.05924000e+01],
[-9.50327000e-01, -1.92878000e+01],
[-2.68694000e+00, -1.77108000e+01],
[-4.20826000e+00, -1.59780000e+01],
[-5.74339000e+00, -1.44248000e+01],
[-7.13617000e+00, -1.34790000e+01],
[-8.80355000e+00, -1.29467000e+01],
[-1.09091000e+01, -1.23274000e+01],
[-1.32822000e+01, -1.15982000e+01],
[-1.57716000e+01, -1.08766000e+01],
[-1.82273010e+01, -1.04894000e+01],
[-2.05464000e+01, -1.03020000e+01],
[-2.24237000e+01, -9.95550000e+00],
[-2.38675990e+01, -9.45586000e+00],
[-2.50128990e+01, -8.74390000e+00],
[-2.58752990e+01, -7.79803000e+00],
[-2.64664000e+01, -6.62279000e+00],
[-2.66699010e+01, -5.05463000e+00],
[-2.65776000e+01, -3.41969000e+00],
[-2.63474010e+01, -1.57144000e+00],
[-2.61133000e+01, 6.46999000e-01],
[-2.58085990e+01, 3.23445000e+00],
[-2.55969010e+01, 5.78099000e+00],
[-2.55000000e+01, 8.36744000e+00],
[-2.54680000e+01, 1.11505000e+01],
[-2.53899000e+01, 1.41447000e+01],
[-2.53566000e+01, 1.71451000e+01],
[-2.58187010e+01, 2.05758000e+01],
[-2.65830990e+01, 2.44065000e+01],
[-2.79578990e+01, 2.84622990e+01],
[-2.95126990e+01, 3.26220020e+01],
[-3.12829000e+01, 3.70005990e+01],
[-3.31349980e+01, 4.12872010e+01],
[-3.51132010e+01, 4.54515000e+01],
[-3.70420000e+01, 4.90353010e+01],
[-3.85575980e+01, 5.27621990e+01],
[-3.94586980e+01, 5.64221990e+01],
[-3.97930980e+01, 5.98333020e+01],
[-3.98658980e+01, 6.29235000e+01],
[-3.94589000e+01, 6.64207990e+01],
[-3.88973010e+01, 6.96651990e+01],
[-3.81236990e+01, 7.34041980e+01],
[-3.68148000e+01, 7.73573990e+01],
[-3.54217990e+01, 8.15375980e+01],
[-3.38608020e+01, 8.63582990e+01],
[-3.15564000e+01, 9.19246980e+01],
[-2.93428990e+01, 9.76631010e+01],
[-2.72516990e+01, 1.02860001e+02],
[-2.59647010e+01, 1.08068001e+02],
[-2.51345010e+01, 1.12709999e+02],
[-2.48617990e+01, 1.16903000e+02],
[-2.48806990e+01, 1.20731003e+02],
[-2.52887990e+01, 1.24375000e+02],
[-2.62332990e+01, 1.28048004e+02],
[-2.74224000e+01, 1.31630005e+02],
[-2.93577000e+01, 1.35138000e+02],
[-3.18421000e+01, 1.38580002e+02],
[-3.43532980e+01, 1.42533997e+02],
[-3.68244020e+01, 1.46210999e+02],
[-3.83947980e+01, 1.49451996e+02],
[-3.94422990e+01, 1.52266006e+02],
[-4.00191000e+01, 1.54524002e+02],
[-4.01052020e+01, 1.56955002e+02],
[-4.01661990e+01, 1.59701996e+02],
[-3.94771000e+01, 1.62824997e+02],
[-3.84656980e+01, 1.65957993e+02],
[-3.69557000e+01, 1.69384995e+02],
[-3.54382020e+01, 1.72932007e+02],
[-3.37109990e+01, 1.77065002e+02],
[-3.12064990e+01, 1.81628006e+02],
[-2.85994000e+01, 1.86921005e+02],
[-2.57090000e+01, 1.91203003e+02],
[-2.33045010e+01, 1.95505005e+02],
[-2.18305000e+01, 1.99636993e+02],
[-2.07416990e+01, 2.03414993e+02],
[-2.00366990e+01, 2.06847000e+02],
[-1.97754000e+01, 2.11009003e+02],
[-1.97579000e+01, 2.15139999e+02],
[-1.99501990e+01, 2.19901001e+02],
[-2.07133010e+01, 2.24968994e+02],
[-2.17415010e+01, 2.30507004e+02],
[-2.29566990e+01, 2.36494003e+02],
[-2.40505010e+01, 2.42442001e+02],
[-2.48687990e+01, 2.48417007e+02],
[-2.52953000e+01, 2.54072006e+02],
[-2.54123000e+01, 2.59537994e+02],
[-2.54193000e+01, 2.64915985e+02],
[-2.56805990e+01, 2.71098999e+02],
[-2.59018990e+01, 2.77950989e+02],
[-2.64182000e+01, 2.84119995e+02],
[-2.65342010e+01, 2.89997009e+02],
[-2.60492990e+01, 2.95882996e+02],
[-2.51636010e+01, 3.02220001e+02],
[-2.36994990e+01, 3.09553986e+02],
[-2.22290000e+01, 3.16851990e+02],
[-2.17989010e+01, 3.23812012e+02],
[-2.21809010e+01, 3.30109985e+02],
[-2.24906010e+01, 3.36039001e+02],
[-2.24252000e+01, 3.41493011e+02],
[-2.26278990e+01, 3.46933990e+02],
[-2.34319000e+01, 3.52662994e+02],
[-2.43871990e+01, 3.58191010e+02],
[-2.52982010e+01, 3.64041992e+02],
[-2.64419000e+01, 3.70739990e+02],
[-2.79559990e+01, 3.77709991e+02],
[-2.87983000e+01, 3.84595001e+02],
[-2.86500000e+01, 3.90970001e+02],
[-2.82661990e+01, 3.96442993e+02],
[-2.81972010e+01, 4.01808014e+02]])
#nullpoints
A = array([
[ 3.50332001e+02, -2.99004990e+01],
[ 3.15854004e+02, -3.02551990e+01],
[ 2.76933014e+02, -3.00962010e+01],
[ 2.42638000e+02, -2.79718000e+01],
[ 2.11820007e+02, -3.27537000e+01],
[ 1.77757004e+02, -3.42841000e+01],
[ 1.63324005e+02, -3.61517980e+01],
[ 1.27331001e+02, -3.85568010e+01],
[ 1.06468002e+02, -3.88120990e+01],
[ 8.10721970e+01, -3.54217000e+01],
[ 7.02815020e+01, -3.50397990e+01],
[ 5.30055010e+01, -3.03295990e+01],
[ 3.03379000e+01, -2.64284990e+01],
[ 1.67920000e+01, -2.09810010e+01],
[-2.61133000e+01, 6.46999000e-01],
[-3.31349980e+01, 4.12872010e+01],
[-3.38608020e+01, 8.63582990e+01],
[-3.18421000e+01, 1.38580002e+02],
[-3.12064990e+01, 1.81628006e+02],
[-2.17415010e+01, 2.30507004e+02],
[-2.17415010e+01, 2.30507004e+02],
[-2.51636010e+01, 3.02220001e+02],
[-2.43871990e+01, 3.58191010e+02]])
isSort = 1
# isSort = 0 --> problems with splrep
# File "/usr/local/lib/python2.6/dist-packages/scipy/interpolate/fitpack.py",
# line 466, in splrep
# raise _iermess[ier][3](_iermess[ier][0])
# ValueError: Error on input data
if isSort:
I = lexsort((A[:,1], A[:,0]) )
A = A[I]
a1 = A[:,0]
a2 = A[:,1]
subplot(311)
plot(path[:,0], path[:,1], "-r", lw=2, label="path")
plot(a1, a2, "ob", ms = 6, label = "nullpoints")
legend();grid()
subplot(312)
# -------------------- spline ------------------------
xnew = np.arange( min(a1), max(a1), 1)
for s, color in zip([abs(max(a2)- min(a2))*32, abs(max(a2)- min(a2))*8], ["-m", "-g"]):
tck = interpolate.splrep(a1, a2, s = s)
ynew = interpolate.splev(xnew, tck)
plot(xnew, ynew, "%s"%color, lw=2, label = "splrep, s=%d"%s)
#------------------------------------------------------
plot(a1, a2, "ob", ms = 6)
legend();grid()
# zoom around the oscillating part
subplot(313)
for s, color in zip([abs(max(a2)- min(a2))*32, abs(max(a2)- min(a2))*8], ["-m", "-g"]):
tck = interpolate.splrep(a1, a2, s = s)
ynew = interpolate.splev(xnew, tck)
plot(xnew, ynew, "%s"%color, lw=2, label = "splrep, s=%d"%s)
xlim([min(a2), -20])
plot(a1, a2, "ob", ms = 6, label = "nullpoints")
grid();savefig("dummy.png"); show()
答案 0 :(得分:4)
您的数据问题似乎是重复的:当两个连续点相等时,您无法正确插值。
在“nullpoints”数据的末尾附近,有两个相同的行:
[-2.17415010e+01, 2.30507004e+02], [-2.17415010e+01, 2.30507004e+02],
另请注意,由于您拥有2D数据,因此需要使用splprep()
(请注意额外的p
)。
对于我的项目,我使用以下函数来插值2D点集(折线):
import numpy as np
import scipy.interpolate as interp
def interpolate_polyline(polyline, num_points):
duplicates = []
for i in range(1, len(polyline)):
if np.allclose(polyline[i], polyline[i-1]):
duplicates.append(i)
if duplicates:
polyline = np.delete(polyline, duplicates, axis=0)
tck, u = interp.splprep(polyline.T, s=0)
u = np.linspace(0.0, 1.0, num_points)
return np.column_stack(interp.splev(u, tck))
我可以成功将此函数应用于“nullpoints”数据:
>>> B = interpolate_polyline(A, 100)
>>> B.shape
(100, 2)
>>> plot(B[:, 0], B[:, 1])