估计点列表中的缺失点

Estimate missing points in a list of points

我正在通过检测视频中球的飞行生成一个 (x,y) 坐标列表。我遇到的问题是视频中间的几帧无法检测到球,对于这些帧,列表附加了 (-1,-1)。 有没有办法估计这些点的球的真实 (x,y) 坐标?

例如跟踪点列表为:

pointList = [(60, 40), (55, 42), (53, 43), (-1, -1), (-1, -1), (-1, -1), (35, 55), (30, 60)]

然后返回对 3 (-1,-1) 个缺失坐标的估计值与周围点的上下文(保留曲线)。

您可以使用 scipys spline 来插入缺失值:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import splprep, splev
pointList = [(60, 40), (55, 42), (53, 43),
             (-1, -1), (-1, -1), (-1, -1), 
             (35, 55), (30, 60)]

# Remove the missing values
pointList = np.array(pointList)
pointList = pointList[pointList[:, 0] != -1, :]

def spline(x, n, k=2):
    tck = splprep(x.T, s=0, k=k)[0]
    u = np.linspace(0.0, 1.0, n)
    return np.column_stack(splev(x=u, tck=tck))

# Interpolate the points with a quadratic spline at 100 points
pointList_interpolated = spline(pointList, n=100, k=2)

plt.plot(*pointList.T, c='r', ls='', marker='o', zorder=10)
plt.plot(*pointList_interpolated.T, c='b')

如果它是一个球,那么理论上它应该有一条抛物线路径,您可以尝试拟合一条忽略 (-1, -1) 的曲线,然后替换缺失值。

有点像……

import numpy as np

pointList = [(60, 40), (55, 42), (53, 43), (-1, -1), (-1, -1), (-1, -1), (35, 55), (30, 60)]

x, y = list(zip(*[(x, y) for (x, y) in pointList if x>0]))

fit = np.polyfit(x, y, 2)
polynome = np.poly1d(fit)

# call your polynome for missing data, e.g.
missing = (55 - i*(55-35)/4 for i in range(3))
print([(m, polynome(m)) for m in missing])

给予...

[(55.0, 41.971982486554325), (50.0, 44.426515896714186), (45.0, 47.44514924300471)]

如果相机没有移动 - 只是球而你忽略了风,那么轨迹是抛物线的。参见:https://en.wikipedia.org/wiki/Trajectory#Uniform_gravity,_neither_drag_nor_wind

在这种情况下,将二次函数拟合到你知道的点,你会得到缺失的点。在拟合时,还将未知区域附近的边界点(点 53,43 和 35、55)的误差设置为 0 或接近 0(无误差,插值权重大),这样您的插值将通过这些点。

有一些用于多项式拟合的库。例如。 numpy.polyfit: https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.polynomial.polynomial.polyfit.html