简体   繁体   English

Convex Hull和SciPy

[英]Convex Hull and SciPy

I'm trying to use scipy (0.10.1) for a quick hack to visualize the convex hull. 我正在尝试使用scipy(0.10.1)来快速破解凸形船体。

I can get the convex hull using the following code: 我可以使用以下代码获取凸包:

vecs = [[-0.094218, 51.478927], [-0.09348,  51.479364], [-0.094218, 51.478927],
        ...
        [-0.094218, 51.478927], [-0.094321, 51.479918], [-0.094218, 51.478927],
        [-0.094222, 51.478837], [-0.094241, 51.478388], [-0.094108, 51.478116],
        [-0.09445,  51.480279], [-0.094256, 51.478028], [-0.094326, 51.500511]]
hull = scipy.spatial.Delaunay(vecs).convex_hull

the resulting array looks like this: 结果数组如下所示:

[[56,  9], [16,  1], [56,  1], [55,  9], [53, 55], [53, 16]]

the numbers are the vertex indices. 数字是顶点索引。 My problem is they are not ordered . 我的问题是他们没有订购 I'd need them to be in CW or CCW order in order to easily visualize them in KML. 我需要它们处于CW或CCW顺序,以便在KML中轻松地将它们可视化。

Is there any easy way to have scipy.spatial compute the proper clockwise order? 是否有任何简单的方法让scipy.spatial计算正确的顺时针顺序?

So this code seems to do the trick, but could be simpler... Essentially, I first collect the vertex numbers from the hull. 所以这段代码似乎可以解决问题,但可能更简单......基本上,我首先从船体中收集顶点数。 Then I compute the mean, recenter the dataset and sort it by the angle from the mean. 然后我计算平均值,重新定位数据集并按平均值的角度对其进行排序。

ps = set()
for x, y in hull:
    ps.add(x)
    ps.add(y)
ps = numpy.array(list(ps))
center = vecs[ps].mean(axis=0)
A = vecs[ps] - center
h = vecs[ps[numpy.argsort(numpy.arctan2(A[:,1], A[:,0]))]]

在当前dev的DOC(0.13.0.dev) scipy.spatial.ConvexHull ,有一个vertices属性,其是在逆时针方向2D。

I found out a nice method but it requires scipy 0.11.0 (sparse.csgraph) 我发现了一个很好的方法,但它需要scipy 0.11.0(sparse.csgraph)

Here is a full example, the actual sorting are the 2 lignes following the "sort hull ..." comment. 这是一个完整的例子,实际排序是“排序船体......”评论之后的2个lignes。

import numpy as np
import scipy as sp

# random point cloud and hull
X = np.random.randint(0,200,(30,2))
hull = sp.spatial.qhull.Delaunay(X).convex_hull

# sort hull indices using (sparse) adjacency matrix graph stuff
g = sp.sparse.csr_matrix((np.ones(hull.shape[0]),hull.T), shape=(hull.max()+1,)*2)
sorted_hull = sp.sparse.csgraph.depth_first_order(g,hull[0,0],directed=False)[0]

# display with matplotlib
from matplotlib import pyplot as plt
plt.plot(X[:,0],X[:,1],'.')
plt.plot(X[sorted_hull,0],X[sorted_hull,1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM