ellipse

General functions for working with ellipses.

mr_utils.utils.ellipse.check_fit(C, x, y)[source]

General quadratic polynomial function.

Parameters:
  • C (array_like) – coefficients.
  • x (array_like) – x coordinates assumed to be on ellipse.
  • y (array_like) – y coordinates assumed to be on ellipse.
Returns:

Measure of how well the ellipse fits the points (x, y).

Return type:

float

Notes

We want this to equal 0 for a good ellipse fit. This polynomial is called the algebraic distance of the point (x, y) to the given conic.

This equation is referenced in [1] and [2].

References

[1]Shcherbakova, Yulia, et al. “PLANET: an ellipse fitting approach for simultaneous T1 and T2 mapping using phase‐cycled balanced steady‐state free precession.” Magnetic resonance in medicine 79.2 (2018): 711-722.
[2](1, 2, 3) Halır, Radim, and Jan Flusser. “Numerically stable direct least squares fitting of ellipses.” Proc. 6th International Conference in Central Europe on Computer Graphics and Visualization. WSCG. Vol. 98. 1998.
mr_utils.utils.ellipse.do_planet_rotation(I)[source]

Rotate complex points to fit vertical ellipse centered at (xc, 0).

Parameters:I (array_like) – Complex points from SSFP experiment.
Returns:
  • xr (array_like) – x coordinates of rotated points.
  • yr (array_like) – y coordinates of rotated points.
  • cr (array_like) – Coefficients of rotated ellipse.
  • phi (float) – Rotation angle in radians of effective rotation to get ellipse vertical and in the x > 0 half plane.
mr_utils.utils.ellipse.fit_ellipse_fitzgibon(x, y)[source]

Python port of direct ellipse fitting algorithm by Fitzgibon et. al.

Parameters:
  • x (array_like) – y coordinates assumed to be on ellipse.
  • y (array_like) – y coordinates assumed to be on ellipse.
Returns:

Ellipse coefficients.

Return type:

array_like

Notes

See Figure 1 from [2].

Also see previous python port: http://nicky.vanforeest.com/misc/fitEllipse/fitEllipse.html

mr_utils.utils.ellipse.fit_ellipse_halir(x, y)[source]

Python port of improved ellipse fitting algorithm by Halir and Flusser.

Parameters:
  • x (array_like) – y coordinates assumed to be on ellipse.
  • y (array_like) – y coordinates assumed to be on ellipse.
Returns:

Ellipse coefficients.

Return type:

array_like

Notes

Note that there should be at least 6 pairs of (x,y).

From the paper’s conclusion:

“Due to its systematic bias, the proposed fitting algorithm cannot be used directly in applications where excellent accuracy of the fitting is required. But even in that applications our method can be useful as a fast and robust estimator of a good initial solution of the fitting problem…”

See figure 2 from [2].

mr_utils.utils.ellipse.fit_ellipse_nonlin(x, y, polar=False)[source]

Fit ellipse only depending on semi-major axis and eccentricity.

Parameters:
  • x (array_like) – y coordinates assumed to be on ellipse.
  • y (array_like) – y coordinates assumed to be on ellipse.
  • polar (bool, optional) – Whether or not coordinates are provided as polar or Cartesian.
Returns:

  • a (float) – Semi-major axis
  • e (float) – Eccentricity

Notes

Note that if polar=True, then x will be assumed to be radius and y will be assumed to be theta.

See: https://scipython.com/book/chapter-8-scipy/examples/non-linear-fitting-to-an-ellipse/

mr_utils.utils.ellipse.get_center(c)[source]

Compute center of ellipse from implicit function coefficients.

Parameters:c (array_like) – Coefficients of general quadratic polynomial function for conic funs.
Returns:
  • xc (float) – x coordinate of center.
  • yc (float) – y coordinate of center.
mr_utils.utils.ellipse.get_semiaxes(c)[source]

Solve for semi-axes of the cartesian form of the ellipse equation.

Parameters:c (array_like) – Coefficients of general quadratic polynomial function for conic funs.
Returns:
  • float – Semi-major axis
  • float – Semi-minor axis

Notes

https://en.wikipedia.org/wiki/Ellipse

mr_utils.utils.ellipse.rotate_coefficients(c, phi)[source]

Rotate coefficients of implicit equations through angle phi.

Parameters:
  • c (array_like) – Coefficients of general quadratic polynomial function for conic funs.
  • phi (float) – Angle in radians to rotate ellipse.
Returns:

Coefficients of rotated ellipse.

Return type:

array_like

Notes

http://www.mathamazement.com/Lessons/Pre-Calculus/09_Conic-Sections-and-Analytic-Geometry/rotation-of-axes.html

mr_utils.utils.ellipse.rotate_points(x, y, phi, p=(0, 0))[source]

Rotate points x, y through angle phi w.r.t. point p.

Parameters:
  • x (array_like) – x coordinates of points to be rotated.
  • y (array_like) – y coordinates of points to be rotated.
  • phi (float) – Angle in radians to rotate points.
  • p (tuple, optional) – Point to rotate around.
Returns:

  • xr (array_like) – x coordinates of rotated points.
  • yr (array_like) – y coordinates of rotated points.