# Fitting an ellipse to a cloud of points

Is there a simple way in Igor to fit an ellipse to a cloud of points to arrive at an estimate of ellipse area, length of the major axis, and minor axis? I have attached sample data as a example.

Ah, I replied before looking at your data. You want to estimate an ellipse that surrounds, or nearly surrounds, a cloud of data. That's a different problem to fitting an ellipse through the data. You might be able to get away with fitting an implicit function to the convex hull of the data, but the data you supplied may not be well-behaved enough for that to work.

It won't be of much help to you, but what you're doing is a bit like what I did with the encircle project.

Hi Joseph, there are a few ways to approach this.

Very simple: you could fit a convex hull to the data and use this to estimate the area. The points of the hull could then be used for fitting an ellipse?

More involved (but worth it IMO): do a PCA on the data, the eigenvectors will be the major and minor axes of your point cloud. A walkthrough with an example is described by A.G. in this thread https://www.wavemetrics.com/forum/general/variance-covariance-matrix-he…

Hey Joseph,

I realize I'm probably too late, but here is a little function to do what you were looking for. It plots a Bivariate Contour Ellipse and calculates its area (BCEA).

I excised this bit from a much larger function that I wrote for a paper a couple years ago. It takes three inputs, two waves (x and y data) and a decimal percentage between 0 and 1 (representing the intended confidence interval).

The function outputs a graph of the data surrounded by an ellipse of the appropriate size and reports the ellipse area in arbitrary units to the command line.

Hope it helps if you're still interested in the topic. May help others as well.

Regards,

John

John R. Economides, PhD

Beckman Vision Center

University of California, San Francisco

Thanks Tony. I wish I had seen this a couple months ago, might have been of some help to the original poster.

BTW, if anyone downloaded this function within a few minutes of when I initially posted it, please download it again. There was a bug in the area calculation that I have since fixed.

John

Thanks John for the post and the function.  It was exactly what I was looking for. I had fooled around with the code from other analysis software but was struggling to port it over to the world of Igor which I prefer to use for all of our analyses and generating figures.  I had gone through the tedious manual process and was incorporating that into a macro when I received word regarding your post.  Just got around to trying it and it's great and I will be using that to sort out our data.  I am really grateful for your help as well as all the others just trying to lend a hand.  This discussion forum really works.

I also see we have an overlap in our academic travels.  I held my first faculty appointment in otolaryngology at UTMB from 2005-2008 before moving to Rochester, NY.  Small world I suppose.  Again thanks for all the help.

Happy to be of help Joseph. Guessing you got chased out of Galveston by Ike. Nasty business that. But from what I can tell UTMB is bouncing back fairly well.

John

In reply to by John Economides

Hi John,

Sorry to be a pain but I am reaching out to you for what I hope will be some quick advice.  Your function works perfect for what we are trying to do.  I have cross-validated against several other approaches and it lives up to its promise.  I would like to extract a few more items from the ellipse fit if possible.  I would like to know what is the length of the major and minor axis of the ellipse as well as the angle (0-360 degrees) of the ellipse with respect to the horizontal axis.  Is this possible to acquire this with your function?

Chris

Hi Chris, no problem.

The values you want are the ones used to calculate the ellipse in my code. I've rejiggered the function to write the following to the command history:

major axis length in arbitrary units

minor axis length in arbitrary units

angle psi in degrees relative to the horizontal axis (note can be positive or negative rotation)

ellipse area in arbitrary units squared

Regards,

John

Dear Chris,

I just realized who you are. We don't know each other but we have some friends and colleagues in common. I was saddened to hear of Jay Goldberg's passing. I always admired his staggering contributions to our understanding of the vestibular system. You have my condolences.

I'm an oculomotor and vision person myself, but have dabbled in some linear translations and rotations in the past.

Sincerely,

John Economides

Forum

Support

Gallery