【问题标题】:Calculate center of mass of surface from set of x, y coordinates从一组 x, y 坐标计算表面的质心
【发布时间】:2018-10-06 22:07:14
【问题描述】:

我正在尝试确定由一组随机非等距 x、y 点确定的表面的质心。 这是一个快速测试集来说明我的意思。

from scipy.spatial import ConvexHull
import numpy as np
import matplotlib.pyplot as plt

def PolyArea(x, y):
    return 0.5*np.abs(np.dot(x, np.roll(y,1))-np.dot(y, np.roll(x,1)))

points = np.random.rand(30, 2)   # 30 random points in 2-D
hull = ConvexHull(points)

plt.plot(points[:,0], points[:,1])
for simplex in hull.simplices:
    plt.plot(points[simplex, 0], points[simplex, 1])

plt.plot(points[hull.vertices, 0], points[hull.vertices, 1], 'r--', lw=2)
plt.show()

从这里我们得到:

x = points[hull.vertices, 0]
y = points[hull.vertices, 1]

surface_size = PolyArea(x, y)

我希望从点集 (x, y) 中确定区域的质心,而不是点的平均值。我知道这是通过曲面的双积分来计算的(参见:http://tutorial.math.lamar.edu/Classes/CalcII/CenterOfMass.aspx),但我不知道如何在 Python 中实现它。

提前致谢。

【问题讨论】:

  • 感谢您对 caramiriel 的评论,但是,我正在寻找非等距点 ('x_n, y_n') 的质心,因此,相关建议的帖子还不够。我正在尝试实现双积分方法,但是,我真的不知道在 Python 中从哪里开始。
  • @LeonvandenHoven 你不需要任何积分,除非你想计算你的多边形包围的表面的质心并且表面具有不同的强度/质量。有一个简单的几何公式​​可以计算多边形的质心,您应该可以在 numpy 中轻松编写它:en.wikipedia.org/wiki/Centroid#Centroid_of_a_polygon
  • 如果您需要非均匀强度表面的质心,那么我的答案实际上可能很有用(有几个微不足道的变化)
  • 如果你有一个表面网格,这真的很容易做到,你不必自己实现它。 github.com/mikedh/trimesh

标签: python numpy scipy area surface


【解决方案1】:

肯定有一个更优雅的解决方案,但这里有一个快速而肮脏,可能缓慢且过度杀伤力的基于图像的解决方案。

import skimage.measure
import skimage.draw

GRIDW = 1000
GRIDH = 1000

img = np.zeros((GRIDW, GRIDH))

rr, cc = skimage.draw.polygon(x*GRIDW,y*GRIDH)
img[rr,cc] = 1

label = skimage.measure.label(img)
rprops = skimage.measure.regionprops(label)

print rprops[0].centroid / np.asarray([GRIDW, GRIDH])

【讨论】:

    猜你喜欢
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2011-10-01
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多