# Fitting an ellipse to a cloud of points

joseph_holt

Thu, 04/04/2019 - 03:24 am

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.

Forum

Support

Gallery

### Igor Pro 8

Learn More

### Igor XOP Toolkit

Learn More

### Igor NIDAQ Tools MX

Learn More

This thread may be helpful:

https://www.wavemetrics.com/node/20572

April 4, 2019 at 04:30 am - Permalink

In reply to This thread may be helpful: … by tony

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.

April 4, 2019 at 04:45 am - Permalink

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…

April 4, 2019 at 05:26 am - Permalink

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

June 6, 2019 at 11:02 am - Permalink

In reply to Hey Joseph, I realize I'm… by John Economides

Very nice! I struggled for a while to figure out how to do this with PCA and gave up. This seems to do exactly what the OP was looking for.

June 7, 2019 at 01:21 am - Permalink

In reply to Very nice! I struggled for a… by tony

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

June 7, 2019 at 10:47 am - Permalink

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.

June 10, 2019 at 08:34 am - Permalink

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

June 10, 2019 at 11:15 am - Permalink

In reply to Happy to be of help Joseph… 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

June 25, 2019 at 02:26 pm - Permalink

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

June 27, 2019 at 01:28 pm - Permalink

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

June 28, 2019 at 11:23 am - Permalink