Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Estimating aspect ratio of a convex hull

What would be the best way to approximate the aspect ratio of a convex hull in 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

like image 748
ebressert Avatar asked Aug 14 '11 21:08

ebressert


1 Answers

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()

enter image description here

like image 122
Joe Kington Avatar answered Sep 21 '22 08:09

Joe Kington