如何使用dtw在python中对齐多个轨迹?

2024-05-26 04:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有5个不同的轨迹为我的项目。首先我从文件中读取它们,然后通过解析文件将它们保存到一个列表中。之后我把这些值数字化了。我想分别对齐x和y坐标,并在网格上同时绘制x和y坐标。你知道吗

这就是我到目前为止所做的。我在python中使用dtw包,但它需要两个列表并以数组的形式给出路径。如何将其转换为5个不同列表的对齐轨迹?你知道吗

x_dict = {}
y_dict = {}
for x in ["1", "2", "3", "4", "5"]:
    file = open("data-" + x + ".txt", encoding="latin-1")
    data = file.read()
    pos_list = re.findall(r'position:(.*?)orientation:', data, re.DOTALL)
    or_list = re.findall(r'orientation:(.*?)scale:', data, re.DOTALL)

    for i in range(len(pos_list)):
        pos_list[i] = pos_list[i].replace('\n','')

    regexx = re.compile(r'x: (.*?) y:')
    regexy = re.compile(r'y: (.*?) z:')

    posx_list = [m.group(1) for l in pos_list for m in [regexx.search(l)] if m]
    posx_list = list(map(float, posx_list))

    posy_list = [m.group(1) for l in pos_list for m in [regexy.search(l)] if m]
    posy_list = list(map(float, posy_list))

    bins = numpy.linspace(-1, 1, 100)
    digitized_x = numpy.digitize(posx_list, bins)
    digitized_y = numpy.digitize(posy_list, bins)

    x_dict[x] = digitized_x
    y_dict[x] = digitized_y

dist, cost, acc, path = dtw(y_dict["5"], y_dict["4"], dist= euclidean)

plt.imshow(acc.T, origin='lower', cmap=cm.gray, interpolation='nearest')
plt.plot(path[0], path[1], 'w')
plt.xlim((-0.5, acc.shape[0]-0.5))
plt.ylim((-0.5, acc.shape[1]-0.5))

Tags: inposrenumpy列表fordataplt