Python:插入线并获得欧几里德距离

时间:2018-02-08 22:22:06

标签: python numpy matplotlib scipy interpolation

我有以下问题。我有一条线的坐标。现在我想连接它们或插入它们,这样我就可以在一个房间里连续排列并计算到其余盒子坐标的欧氏距离,例如(21,41,91)形状的numpy数组(即不应该是scipy,skimage等的问题)。

所以我想在3D中做这样的事情:

Distance Transformation

Distances on grid

因此,线体素将获得零,其余所有坐标都为零。我不想要一条线的距离或线的总长度

这是一个可视化点如何在房间内传播的可视化

Spreaded points in room

enter image description here

例如,这里是一行中的一个numpy数组中的坐标。它们按正确的顺序排列:

import numpy as np

line1 = np.array([[ 14,11,35],
 [ 13,14,37],
 [ 11,17,38],
 [ 11,19,41],
 [ 12,21,43],
 [ 15,24,46],
 [ 18,27,46],
 [ 19,30,45],
 [ 20,33,45],
 [ 21,36,46],
 [ 22,37,47],
 [ 24,37,47],
 [ 23,40,49],
 [ 24,42,51],
 [ 21,44,50],
 [ 19,46,48],
 [ 18,47,45]])

1 个答案:

答案 0 :(得分:1)

IIUC:

In [108]: from mpl_toolkits.mplot3d import Axes3D

In [109]: fig = plt.figure()
     ...: ax = fig.add_subplot(111, projection='3d')


In [112]: ax.plot(line1[:, 0], line1[:, 1], line1[:, 2])
Out[112]: [<mpl_toolkits.mplot3d.art3d.Line3D at 0x13913a20>]

结果:

enter image description here

更新:将其另存为TIFF:

In [128]: from PIL import Image

In [129]: from io import BytesIO

In [130]: buf = BytesIO()

In [131]: fig = plt.figure()
     ...: ax = fig.add_subplot(111, projection='3d')
     ...:

In [132]: ax.plot(line1[:, 0], line1[:, 1], line1[:, 2])
Out[132]: [<mpl_toolkits.mplot3d.art3d.Line3D at 0x14cdccc0>]

In [133]: fig.savefig(buf)

In [134]: Image.open(buf).save(r'd:/temp/out.tiff')