Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Numpy:查找沿直线的点的平均坐标_Numpy_Interpolation - Fatal编程技术网

Numpy:查找沿直线的点的平均坐标

Numpy:查找沿直线的点的平均坐标,numpy,interpolation,Numpy,Interpolation,我在二维空间中有一堆点,它们都位于一条直线(多边形)上。如何计算直线上这些点的平均坐标 我指的不是二维空间中点的质心(正如@rth最初在his中提出的那样),而是点沿其所在直线的平均位置。因此,基本上,我可以将直线转换为一维轴,计算一维中的平均位置,然后将平均位置转换回二维空间 也许这些正是必要的步骤,但我认为(或希望)numpy/scipy中有一个函数可以让我一步完成这项工作。编辑:您在问题中描述的方法确实可能是解决此问题的最简单方法 这是一个实现,它计算1D中沿直线的顶点位置,取其平均值,最

我在二维空间中有一堆点,它们都位于一条直线(多边形)上。如何计算直线上这些点的平均坐标

我指的不是二维空间中点的质心(正如@rth最初在his中提出的那样),而是点沿其所在直线的平均位置。因此,基本上,我可以将直线转换为一维轴,计算一维中的平均位置,然后将平均位置转换回二维空间


也许这些正是必要的步骤,但我认为(或希望)numpy/scipy中有一个函数可以让我一步完成这项工作。

编辑:您在问题中描述的方法确实可能是解决此问题的最简单方法

这是一个实现,它计算1D中沿直线的顶点位置,取其平均值,最后使用参数插值计算相应的2D位置

import numpy as np
from scipy.interpolate import splprep, splev

vert = np.random.randn(1000, 2) # vertices definition here

# calculate the Euclidean distances between consecutive vertices
# equivalent to a for loop with
# dl[i] = ((vert[i+1, 0] - vert[i, 0])**2 + (vert[i+1,1] - vert[i,1])**2)**0.5
dl = (np.diff(vert, axis=0)**2).sum(axis=1)**0.5 

# pad with 0, so dl.shape[0] == vert.shape[0] for convenience
dl = np.insert(dl, 0, 0.0)
l = np.cumsum(dl) # 1D coordinates along the line
l_mean = np.mean(l) # mean in the line coordinates

# calculate the coordinate of l_mean in 2D space
# with parametric B-spline interpolation
tck, _ = splprep(x=vert.T,  u=l, k=3)
res = splev(l_mean, tck)
print(res)
Edit2:假设现在您的路径有一组高分辨率的点
vert_full
,以及一些近似测量值
vert_1
vert_2
,等等,您可以做以下操作

  • 顶点1
    等的每个点投影到精确路径上。假设
    vert_full
    vert_1
    有更多的数据点,我们只需在
    vert_full
    中查找
    vert_1
    的近邻即可:

    from scipy.spatial import cKDTree
    tr = cKDTree(vert_full)
    d, idx = tr.query(vert_1, k=1)
    vert_1_proj = vert_full[idx] # this gives the projected corrdinates onto vert_full
    # I have not actually run this, so it might require minor changes
    
  • 将上述平均值计算与新的
    vert_1_proj
    向量一起使用


与此同时,我找到了我问题的答案,虽然用Shapely代替Numpy

from shapely.geometry import LineString, Point

# lists of points as (x,y) tuples
path_xy = [...]
points_xy = [...] # should be on or near path

path = LineString(path_xy)            # create path object
pts = [Point(p) for p in points_xy]   # create point objects
dist = [path.project(p) for p in pts] # distances along path
mean_dist = np.mean(dist)             # mean distance along path
mean = path.interpolate(mean_dist)    # mean point

mean_xy = (mean.x,mean.y)
这很好用


(这也是为什么我不得不接受这个答案,尽管我非常感谢@rth的帮助!)

谢谢!您是否可以在两行“dl=”中添加注释?我想到时候我会弄明白他们在做什么,但这对其他人会有帮助,因为这些话并不那么直截了当。与此同时,我已经尝试过了,效果非常好。但是,仍然缺少一些东西(这也难怪,因为我在问题中没有提到):集合中的点没有完全指定路径,但我有路径的坐标。如何以完全分辨率指定路径?(我有许多点集,它们都位于同一条路径上,我希望所有的方法也都能在这条路径上结束,当然目前不是这样,因为这些点集定义的路径由于缺乏信息而与实际路径略有不同)。感谢编辑,我明天将尝试它。然而,我担心你可能误解了我。我不是说近似的测量。我的点都位于精确的路径上,但它们只表示构成路径的所有点的子集。现在,从这样一个子集重建的路径当然与原始路径不完全相同,因此得到的平均点将稍微偏离原始路径。现在剩下的挑战是,所有的平均点都精确地位于高分辨率路径上。@flotzilla上述解决方案应该仍然适用于任何一种方式。如果需要的话,你也可以按照这个总体思路进行调整。我试过了,但不幸的是,没有在最初的路径上获得所有的平均点。无论如何,与此同时,我想起了Shapely软件包,并确实找到了一个非常简单的解决方案,它完全满足了我的需求(我将把它作为一个单独的答案添加)。无论如何,非常感谢你的帮助!您是对的,
shapely
模块可能是这里最简单的方法。