Hello there.

I’ve an idea for a new image filter, but I am a bit stuck with one of the main step, that is:

Finding the largest ellipse (allowed to have an arbitrary orientation) within a binary shape.

## 1. A simpler problem:

I’ve tried a few things, and found out that solving this problem when considering circles (instead of ellipses) is relatively easy, and can be done with the following code (taking advantage of the distance function):

```
# Approximate a binary shape with a set of circles.
approx_circles :
shape_cupid 400 # Input binary shape
is0:=is
100%,100%
do
+distance[0] 0 x,y,r:=xM,yM,iM rm.
circle[1] $x,$y,$r,1,{1+$>}
circle[0] $x,$y,$r,1,0
while is#0>0.05*$is0
k. map lines,2
```

This script transforms the following binary shape:

into this set of circles :

## 2. Solving it with ellipses?

Now, the question : how to do the same thing with a (ideally optimal) set of ellipses ?

Currently, I know how to “approximate” a binary shape with an ellipse, by computing a covariance matrix and get the corresponding eigenvalues/eigenvectors that are related to the ellipse radii/orientation.

But that’s not what I want, because the approximated ellipse will most of the time contain background pixels, while an inscribed ellipse will obviously only cover white pixels of the binary shape.

So, how would you solve this, dear smart people of pixlsus ?

Any idea ?