Search code examples
pythoninterpolationpoint

Estimate missing points in a list of points


I'm generating a list of (x,y) coordinates from detecting a ball's flight in a video. The problem I have is for a few frames in the middle of the video the ball can't be detected, for these frames the list appends (-1,-1). Is there a way to estimate the true (x,y) coordinates of the ball for these points?

Eg tracked points list being:

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

Then returning an estimate of what the 3 (-1,-1) missing coordinates would be with context to the sourounding points (preserving the curve).


Solution

  • If it's a ball then theoretically it should have a parabolic path, you could try and fit a curve ignoring the (-1, -1) and then replace the missing values.

    Something like...

    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])
    

    giving ...

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