# How to identify ellipses in grainy images and measure their position, semi-axes and orientation?

I am trying to analyze images of elliptical laser beams that are striking 'ground glass' surfaces. I need to determine the size, position and orientation of the ellipse that represents the boundary of the beam. The images are grainy due to random scattering from the irregular surface of the ground glass and in some cases, only a partial ellipse is visible (see the example image below). I would also like to use a mostly circular reference feature (see CAD drawing here) in the image which I know has a diameter of 25.4 mm to convert the spatial coordinates from pixels to mm.

### Adjust Image Brightness and Contrast

I use ImageAdjust to make the circular reference feature and the elliptical beam shape stand out more clearly in the image. FindImageShapes (available in v13.3) appears to successfully locate the two features.

img = Import["https://i.sstatic.net/UGqaR.png"];
HighlightImage[imgBRIGHT, Graphics@MaximalBy[e, RegionMeasure, 2]]

The output for e is a GeometricTransformation that I do not understand. I took a look at ComponentMeasurements but I couldn't figure out how to apply this to e.

Is there a simple way to extract the centers, major and minor axes, and orientation of the found ellipses?

• The PDF of CAD drawing you provided doesn't work. Commented Jul 21, 2023 at 21:10
• The link to the CAD drawing was fixed. Commented Jul 22, 2023 at 0:55

You can directly extract the centroid and semi-axes from the Circle and orientation angle from the rotation (!) matrix inside GeometricTransform. The following function extractEllipse will return a list with centroid, semi-axes, and rotation angle.

(* {GeometricTransformation[
Circle[{456.704, 539.323}, {132.729, 92.6068}],
{{{0.0693787, 0.99759}, {-0.99759, 0.0693787}}, {-113.005, 957.508}}],
GeometricTransformation[
Circle[{449.805, 546.919}, {228.447, 228.122}],
{{{0.148538, 0.988907}, {-0.988907, 0.148538}}, {-157.86, 910.496}}]} *)

extractEllipse[GeometricTransformation[Circle[{x_, y_}, {rx_, ry_}], {m_, v_}]] :=
{{x, y}, {rx, ry}, ArcTan @@ (m . {1, 0})}

ellipses = extractEllipse /@ e
(* {{{456.704, 539.323}, {132.729, 92.6068}, -1.50136},
{{449.805, 546.919}, {228.447, 228.122}, -1.42171}} *)

To convince yourself that the result is correct, you can recreate the ellipses via Circle and RotationTransform:

HighlightImage[imgBRIGHT,
Graphics[GeometricTransformation[Circle[#[[1]], #[[2]]],
RotationTransform[#[[3]], #[[1]]]]] & /@ ellipses]

which gives the same result.

• That does seem to work. Thanks! m appears to be the rotation angle relative to the horizontal axis but I'm confused about what v is supposed to be? ellipses doesn't seem to return anything for that parameter, so maybe it's not a big deal. Is this some kind of stretching? Commented Jul 21, 2023 at 22:07
• Look at the documentation for GeometricTransform. It represents the transformation of point r to point m.r + v. In our case, m is a rotation matrix around the origin, and v is a translation vector. But this two-step transformation can be equivalently represented with a rotation around the centroid. Commented Jul 21, 2023 at 22:18