Estimating aspect ratio of a convex hull
What would be the best way to approximate the aspect ratio of a convex hull in Python?开发者_如何学Python I have already tried doing this by fitting the vertices of the convex hull with an ellipse and taking the ratio of the semi- and major-axis. The results are not satisfactory though, so I'm now looking into deriving the aspect ratio directly from the convex hull. Any ideas or solutions would be much appreciated.
Cheers
Typically, you'd find the eigenvectors of the covariance matrix of the point cloud. The aspect ratio is the ratio of the largest to smallest eigenvalues.
As an example for a bunch of random points (you'd just apply the same thing to your convex hull, only using the vertices):
import matplotlib.pyplot as plt
import numpy as np
# Random data
num = 100
xy = np.random.random((2,num)) + 0.01 * np.arange(num)
eigvals, eigvecs = np.linalg.eig(np.cov(xy))
fig, (ax1, ax2) = plt.subplots(nrows=2)
x,y = xy
center = xy.mean(axis=-1)
for ax in [ax1, ax2]:
ax.plot(x,y, 'ro')
ax.axis('equal')
for val, vec in zip(eigvals, eigvecs.T):
val *= 2
x,y = np.vstack((center + val * vec, center, center - val * vec)).T
ax2.plot(x,y, 'b-', lw=3)
plt.show()
精彩评论