0

I have a numpy array with shape (140, 23, 2) being 140 frames, 23 objects, and x,y locations. The data has been generated by a GAN and when I animate the movement it's very jittery. I want to smooth it by converting the coordinates for each object so every odd number index to be the mid-point between the even numbered indices either side of it. e.g.

x[1] = (x[0] + x[2]) / 2

x[3] = (x[2] + x[4]) / 2

Below is my code:

def smooth_coordinates(df):
    # df shape is (140, 23, 2)
    # iterate through each object (23)
    for j in range(len(df[0])):
        # iterate through 140 frames
        for i in range(len(df)):
            # if it's an even number and index allows at least 1 index after it
            if (i%2 != 0) and (i < (len(df[0])-2)):
                df[i][j][0] = ( (df[i-1][j][0]+df[i+1][j][0]) /2 )
                df[i][j][1] = ( (df[i-1][j][1]+df[i+1][j][1]) /2 )
    return df

Aside from it being very inefficient my input df and output df are identical. Any suggestions for how to achieve this more efficiently?

1 Answer 1

1
import numpy as np

a = np.random.randint(100, size= [140, 23, 2])  # input array
b = a.copy()

i = np.ogrid[1: a.shape[0]-1: 2]    # odd indicies
i

>>> [  1,   3,   5,   7,   9,  11,  13,  15,  17,  19,  21,  23,  25,
      27,  29,  31,  33,  35,  37,  39,  41,  43,  45,  47,  49,  51,
      53,  55,  57,  59,  61,  63,  65,  67,  69,  71,  73,  75,  77,
      79,  81,  83,  85,  87,  89,  91,  93,  95,  97,  99, 101, 103,
     105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129,
     131, 133, 135, 137]
(a == b).all()                  # testing for equality

>>> True
a[i] = (a[i-1] + a[i+1]) / 2    # averaging positions across frames
(a == b).all()                  # testing for equality again

>>> False
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Hammad that is certainly more succinct than my attempt. However I can't seem to get it to apply the averaging function check = test_example.copy() i = np.ogrid[1: test_example.shape[0]-1: 2] test_example[i] = (test_example[i-1] + test_example[i+1]) / 2 The check and the test_example are identical. Am I missing something obvious?
@Luke it should work just fine. what exact error are you receiving?
it's not throwing any error. If I use your example code above a stays the same before and after the line a[i] = (a[i-1] + a[i+1]) / 2 # averaging positions across frames. It doesn't seem to be modifying the values.
@Luke ive tested the code and it works fine, ive also updated the answer to include this
I created a new notebook with only your code and it ran correctly, as you said. It's still not working in my original notebook so must be an issue with the environment or maybe a variable name clash or something but that's a different problem. You answered the original question perfectly so I'll mark it as answered. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.