7

I'd like to find the "best" fit of an ellipse to contiguous, possibly concave shapes such as:

enter image description here

What have I tried?

I thought that one could assign the direction of the major and minor axes $\vec a, \vec b$ of the ellipse by mapping the pixel values to coordinates, mean subtracting, and saving the largest two eigenvectors from a PCA. The seems to work rather well in finding the direction:

enter image description here

My problem is determining the length of these two vectors. For now, I've used $\sqrt \lambda_1$ $\sqrt \lambda_2$, from the eigenvalues of the PCA. This seems to underestimate the length. How can I determine $|\vec a|$, and $|\vec b|$ or alternatively, best fit an ellipse to these shapes?

Hooked
  • 509
  • 3
  • 8
  • From what I understand, the eigenvalues measure the 'power' of the spectral radius of that particular eigenvector. I do not have the numbers you are using, but aren't $\lambda_1$ and $\lambda_2$ more in line with the actual lengths? – Spacey May 04 '12 at 20:23
  • You're right about the relation of $\lambda$ to the strength of the eigenvectors projection - but I couldn't think of anything else to use. Using $\lambda$ as opposed to $\sqrt \lambda$ scales the vectors off the chart by several orders of magnitude. I simply chose the $\surd$ as a starting guess. – Hooked May 04 '12 at 20:36
  • Since its only 2-dimensional, please post the eigen-vectors you have found, and their corresponding eigenvalues. Also, the max/min points on either edge of the drawing. – Spacey May 04 '12 at 20:49

2 Answers2

6

Following up on my deleted answer... If you take a filled ellipse and project all the points onto the $x$ axis, more points will be projected near the origin than on the extremes, in a circular-shaped distribution. Not a gaussian distribution, and not the uniform distribution I mentioned in the 1-D analogy in my deleted answer. The resulting distribution actually has pdf $p(x) = \sqrt{(1 - (\frac{x}{r})^2)}$, and from there you can compute that the standard deviation is $\frac{r}{2}$.

Thus, if data is uniformly distributed in the interior of an ellipse of radii $a, b$ (whose axes are the $x$ and $y$ axes), the standard deviation of the $x$ coordinate is $\frac{a}{2}$ and of the $y$ coordinate is $\frac{b}{2}$. So the correction factor you need to use is simply 2.

Here is a worked example in python recovering the center (translation matrix), rotation matrix, and radii of an ellipse from points randomly sampled from its interior:

import numpy

# Generate some points distributed uniformely along an ellipse
x = 2 * (numpy.random.rand(10000, 1) - 0.5)
y = 2 * (numpy.random.rand(10000, 1) - 0.5)
d = (x / 0.5) ** 2 + (y / 0.25) ** 2
inside = numpy.where(d < 1)[0]
x = x[inside]
y = y[inside]
data = numpy.hstack((x, y)).T

# Now rotate by 0.5 rad and translate it to (4, -8)
angle = 0.5
rotation = numpy.array([
    [numpy.cos(0.4), -numpy.sin(0.4)],
    [numpy.sin(0.4), numpy.cos(0.4)]])

data = numpy.dot(rotation, data)
data[0, :] += 4
data[1, :] -= 8

# Step 1: center the data to get the translation vector.
print 'Translation', data.mean(axis=1)
data -= numpy.reshape(data.mean(axis=1), (2, 1))

# Step 2: perform PCA to find rotation matrix.
scatter = numpy.dot(data, data.T)
eigenvalues, transform = numpy.linalg.eig(scatter)
print 'Rotation matrix', transform

# Step 3: Rotate back the data and compute radii.
# You can also get the radii from smaller to bigger
# with 2 / numpy.sqrt(eigenvalues)
rotated = numpy.dot(numpy.linalg.inv(transform), data)
print 'Radii', 2 * rotated.std(axis=1)
pichenettes
  • 19,413
  • 1
  • 50
  • 69
  • 1
    +1 Would you mind expanding on how exactly you derived the PDF $p(x) = \sqrt{1-(\frac{x}{r})^2}$? It goes to the heart of the matter. Thanks. – Spacey May 04 '12 at 23:42
  • The standard procedure for deriving a 1-D marginal distribution along one axis from a 2-D distribution (you integrate along the other axis). The integral is then the length of a section of the ellipse, orthogonal to the axis on which you project. – pichenettes May 05 '12 at 08:57
3

Check Direct Least Squares Fitting of Ellipses by Fitzgibbon et al. It's a simple eigenvalue problem, the size of which is not proportional to the number of pixels in your sample! The only step dependent on the number of pixels you throw at it is the computation of a scatter matrix, which is still $O(n)$.

pichenettes
  • 19,413
  • 1
  • 50
  • 69
  • Thank you for the reference @pichenettes, but that paper doesn't look like what I'm trying to solve (please correct me if I'm wrong). In the paper, they are trying to minimize the least-of-squares distance of a set of points to the curve of an ellipse. In my case, I have an area that I'd like to approximate with an ellipse in the best way possible. – Hooked May 04 '12 at 21:03
  • Ah I see... sorry – pichenettes May 04 '12 at 21:09
  • It's a useful link nonetheless, but I came up with similar papers when I did a Google search for a best fit ellipse. Apparently that problem is well defined! – Hooked May 04 '12 at 21:11
  • But couldn't you derive the contour of the area and apply @pichenettes paper to it? – Jean-Yves May 05 '12 at 17:27
  • I think there are corner cases (odd shapes?) where an ellipse which is a good match for the contour wouldn't be a good match for the surface. – pichenettes May 05 '12 at 18:16