我有一个形状为[13044,]的npy文件,其中包含轨迹数据,其中“坐标”给出了轨迹数据的坐标。它具有以下16种dtypes
dtype([('frame_num', '<i4'), ('mean_x', '<f4'), ('mean_y', '<f4'), ('var_x', '<f4'), ('var_y', '<f4'), ('length', '<f4'), ('scale', '<f4'), ('x_pos', '<f4'), ('y_pos', '<f4'), ('t_pos', '<f4'), ('coords', '<f4', (16, 2)), ('trajectory', '<f4', (15, 2)), ('hog', '<f4', (96,)), ('hof', '<f4', (108,)), ('mbh_x', '<f4', (96,)), ('mbh_y', '<f4', (96,))])
tracks[0]
给予
(15, 186.33582, 92.24783, 2.6624618, 0.22911347, 9.241288, 1., 0.51759946, 0.38436598, 0.04166667, [[182. , 92. ], [182.5449 , 92.09226 ], [183.10237 , 91.61799 ], [183.64798 , 92.135086], [184.26779 , 92.16737 ], [185.04352 , 92.200066], [185.68985 , 92.19117 ], [185.94293 , 92.222855], [186.41994 , 92.30446 ], [187.35497 , 92.39461 ], [187.94893 , 92.39315 ], [188.32193 , 92.47165 ], [189.05319 , 92.40751 ], [189.58992 , 92.37913 ], [189.8503 , 92.6842 ], [190.59416 , 92.30396 ]], [[ 0.05896436, 0.0099837 ],...
第一个元素是最后一个frame_number,从第10个元素开始,它给出从第一个帧的第二个坐标开始到最后一个第15帧的轨迹的16个坐标
[182.5449 , 92.09226 ], 1st frame feature point
[183.10237 , 91.61799 ],trajectory of that feature point at second frame..
[183.64798 , 92.135086], [184.26779 , 92.16737 ], [185.04352 , 92.200066], [185.68985 , 92.19117 ], [185.94293 , 92.222855], [186.41994 , 92.30446 ], [187.35497 , 92.39461 ], [187.94893 , 92.39315 ], [188.32193 , 92.47165 ], [189.05319 , 92.40751 ], [189.58992 , 92.37913 ], [189.8503 , 92.6842 ], [190.59416 , 92.30396] trajectory of feature point at last frame
tracks[500]
(19, 101.504395, 133.30353, 4.431484, 0.41493973, 14.826186, 1.9999999, 0.28195664, 0.55543137, 0.06388889, [[ 93.99999 , 133.99998 ], [ 95.10257 , 133.91016 ], [ 96.17397 , 133.8509 ], [ 97.26843 , 133.84372 ], [ 98.360085, 133.75041 ], [ 99.31606 , 133.46017 ], [100.22412 , 133.29907 ], [101.10831 , 133.0922 ], [102.029564, 132.93976 ], [102.92373 , 132.95956 ], [103.90272 , 132.80684 ], [104.84618 , 132.84692 ], [105.69725 , 132.90834 ], [106.740944, 133.00175 ], [107.67655 , 133.08873 ], [108.69984 , 133.09813 ]], [[ 0.07436688,....
第一个元素-最后一帧为19 从第10个元素中得出16个轨迹坐标
[95.10257 , 133.91016 ],5th frame of a feature point
[ 96.17397 , 133.8509 ], [ 97.26843 , 133.84372 ], [ 98.360085, 133.75041 ], [ 99.31606 , 133.46017 ], [100.22412 , 133.29907 ], [101.10831 , 133.0922 ], [102.029564, 132.93976 ], [102.92373 , 132.95956 ], [103.90272 , 132.80684 ], [104.84618 , 132.84692 ], [105.69725 , 132.90834 ], [106.740944, 133.00175 ], [107.67655 , 133.08873 ], -18 th frmae
[108.69984 , 133.09813 ]-19 th frame of that trajectory point
我现在需要将npy保存为: 帧号坐标.........,最后帧号坐标
示例
[5, 95.10257 , 133.91016 ][ 6, 96.17397 , 133.8509 ], [7, 97.26843 , 133.84372 ], [ 8, 98.360085, 133.75041 ], [9, 99.31606 , 133.46017 ], [10, 100.22412 , 133.29907 ], [11, 101.10831 , 133.0922 ], [12, 102.029564, 132.93976 ], [13, 102.92373 , 132.95956 ], [14, 103.90272 , 132.80684 ], [15, 104.84618 , 132.84692 ], [16, 105.69725 , 132.90834 ], [17, 106.740944, 133.00175 ], [18, 107.67655 , 133.08873 ],
[19,108.69984 , 133.09813 ]
这样,我想保存在npy文件中。我只是想用_num框架制作新的npy文件,协调并删除其他数据
已编辑
f=np.zeros((len(tracks),15,3),float)
n_tracks,n_pts,_=tracks['coords'].shape
for i in range(len(tracks)):
get_frame_num=tracks['frame_num'][i]
for j in range((get_frame_num-14),get_frame_num+1):
print(j)
f[:,:,0]=float(j)
f[:,:,1:]=tracks['coords'][:n_tracks,:n_pts-1]
在数组[0]中,第一列为0、0、0。在此结果之后,我想更改为1,2,3在数组[1]中,第一列为1,1,1。在此结果之后,我想将2,3,4更改为轨迹坐标,使用嵌套的for循环会使ipython崩溃
答案 0 :(得分:1)
我将尝试说明如何使用像您这样的数组。
首先,我必须制作一个具有复合dtype的数组。您的简化:
In [729]: dt = np.dtype([('frame','i'), ('coord', 'f', (3,2))])
In [730]: arr = np.zeros((5,), dtype=dt)
In [731]: arr['frame'] = np.arange(5)
In [732]: arr['coord'].shape
Out[732]: (5, 3, 2)
In [733]: arr['coord'] = np.arange(30).reshape(5,3,2)
In [734]: arr
Out[734]:
array([(0, [[ 0., 1.], [ 2., 3.], [ 4., 5.]]),
(1, [[ 6., 7.], [ 8., 9.], [10., 11.]]),
(2, [[12., 13.], [14., 15.], [16., 17.]]),
(3, [[18., 19.], [20., 21.], [22., 23.]]),
(4, [[24., 25.], [26., 27.], [28., 29.]])],
dtype=[('frame', '<i4'), ('coord', '<f4', (3, 2))])
arr['frame']
和arr['coord']
就像您可以从数组中提取的字段一样。
我不太确定您是否想加入帧号和坐标,但这是一个猜测。首先创建空白数组以保存联接的数据。
In [735]: x = np.zeros((5,3,3),float)
并从arr
复制值:
In [736]: x[:,:,0] = arr['frame']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-736-7270535f17a6> in <module>()
----> 1 x[:,:,0] = arr['frame']
ValueError: could not broadcast input array from shape (5) into shape (5,3)
糟糕。 x[:,:,0]
是(5,3)数组,而frame
是(5,);我需要将(5,)更改为(5,1),以便可以将其广播为(5,3):
In [737]: x[:,:,0] = arr['frame'][:,None]
In [738]: x[:,:,1:] = arr['coord'] # (5,3,2) both sides
In [739]: x
Out[739]:
array([[[ 0., 0., 1.],
[ 0., 2., 3.],
[ 0., 4., 5.]],
[[ 1., 6., 7.],
[ 1., 8., 9.],
[ 1., 10., 11.]],
[[ 2., 12., 13.],
[ 2., 14., 15.],
[ 2., 16., 17.]],
[[ 3., 18., 19.],
[ 3., 20., 21.],
[ 3., 22., 23.]],
[[ 4., 24., 25.],
[ 4., 26., 27.],
[ 4., 28., 29.]]])
np.save(filename, x)
应该可以正常工作。 np.save
适用于任何numpy数组,甚至是3d数组。尝试其他问题时,无需迭代。 np.save
也可以处理arr
。
我还可以通过串联构造x
。但是首先我必须将frame
复制到兼容的形状。记住coord
是(5,3,2):
In [744]: f = np.repeat(arr['frame'][:,None],3,axis=1)
In [745]: f.shape
Out[745]: (5, 3)
In [746]: y = np.concatenate((f[:,:,None], arr['coord']), axis=2)
In [747]: y.shape
Out[747]: (5, 3, 3)
In [748]: np.allclose(x,y)
Out[748]: True
`