Gaussian along an ellipse

318 Views Asked by At

Best way to describe what I mean is with an example; I know how to generate a "Gaussian along a line":

$$e^{-h_2^2((x-h_5)\cos(h_3)+(y-h_6)\sin(h_3)).^2}$$

Some example matlab code:

h1 = 0.025;
h2 = 0.5;
h3 = pi/16;
h4 = pi/2.25;
h5 = 50;
h6 = 60;

[y,x] = ndgrid(1:120,1:120);

figure;
imshow(exp(-h2^2*((x-h5).*cos(h3)+(y-h6).*sin(h3)).^2),[])

enter image description here

This is a "Gaussian along a line". How would I go about doing the same thing, but for an ellipse instead? Preferably where the function is equal to 1 along the ellipse boundary like it is for this equation of the line:

$$(x-h_5)\cos(h_3)+(y-h_6)\sin(h_3)$$

and also has a parameter like $h_2$ where I can control the spread (i.e. variance).

EDIT:

When using:

h = 30;
k = 40;
a = 5;
b = 6;
h2 = 4;

figure;
imshow(exp(-h2^2*( (x-h).^2/a^2 + (y-k).^2/b^2 - 1).^2),[])


h = 30;
k = 40;
a = 25;
b = 30;
h2 = 4;

figure;
imshow(exp(-h2^2*( (x-h).^2/a^2 + (y-k).^2/b^2 - 1).^2),[])

Even though $h_2$ is the same the variance seems to increase:

enter image description here

2

There are 2 best solutions below

6
On

The trick is to build a function that cancels along the curve and is nonzero elsewhere.

In the case of a line,

$$f(x,y)=ax+by+c$$ is a perfect candidate.

For an axis aligned, centered ellipse,

$$f(x,y)=\frac{x^2}{a^2}+\frac{y^2}{b^2}-1.$$

In both cases, the Gaussian takes the form

$$e^{-h^2f^2(x,y)}$$ giving, for the ellipse

$$e^{-h^2\left(\frac{x^2}{a^2}+\frac{y^2}{b^2}-1\right)^2}.$$

0
On

One thing you can try is the following- let $C=C(s)$ be a curve defined for $s\in I$, so maybe a line, or a curve like $y=x^2$ or an ellipse. Then define for $\mathbf x = (x,y)$,

$$ f_C(\mathbf x) = e^{- \inf_{s\in I} |\mathbf x - C(s)|^2/(2\sigma^2)}$$

The quantity $\inf_{s\in I} |\mathbf x - C(s)|$ is the shortest distance to the curve $C$.

In the case $C$ is a line, this should be the same as yours after some redefining of constants, as the shortest distance to a line is obtained by dropping a perpendicular, and hence you will see the intensity die out as you move out perpendicularly from the line, with the exponential decay matching the 1D Gaussian with variance $\sigma^2$.

The function $g(s) := |\mathbf x - C(s)|^2$ is differentiable if $C$ is, with derivative

$$g'(s) = 2(\mathbf x - C(s))\cdot C'(s)$$ so the infimum is at a point $C_* = C(s_*)$ where the tangent of the curve is perpendicular to the line joining $\mathbf x$ and $C_*$, or in other words, $\mathbf x-C_*$ needs to be parallel to the normal vector of the curve. This is a nice picture, but I don't know how it translates into code.

In the case of an ellipse, say of the form

$$C(s) = (a \cos s, b \sin s), s \in [0,2π)$$ Then $C'(s) = (-a \sin s, b\cos s)$, and the equation to solve is

$$0 = (\mathbf x - C(s))\cdot C'(s) = -a(x-a\cos s)\sin s + b(y-b\sin s)\cos s$$

I don't see this having an easy-to-write-down solution. You'll also need to check that the second derivative is positive, and then choose the smallest value of $g(s)$ out of all the minimum points.

On a quick google, I found the following document https://www.geometrictools.com/Documentation/DistancePointEllipseEllipsoid.pdf which seems to go into detail for computing such closest points on an ellipse.

In the case of a circle of radius $R$, its not so hard. Recenter so that the circle $C$ is entered at the origin, and write $\mathbf x = r \mathbf u$ where $\mathbf u $ is a unit vector, and $r=r(\mathbf x) = |\mathbf x|$ is the distance of $\mathbf x$ to 0. Then

$$f_C(\mathbf x) = e^{-|r(\mathbf x)-R|^2 / 2\sigma^2}$$

For an arbitrarily located circle $\{\mathbf x \in \mathbb R^2 : |\mathbf x - \mathbf x_0| = R\}$, you just need to use $r(\mathbf x) = |\mathbf x - \mathbf x_0|$ instead. If $\mathbf x_0 = (x_0,y_0)$, then we arrive at

$$f_C(x,y) = \exp\left(\frac{-\left|\sqrt{(x-x_0)^2 + (y-y_0)^2} - R\right|^2}{2\sigma^2} \right)$$

Naively substituting the circle formula $|\mathbf x - \mathbf x_0| = R$ with the similar formula for the ellipse $|\mathbf x - \mathbf x_0| + |\mathbf x - \mathbf x_1| = R$ , $$f_C(\mathbf x) = \exp\left(\frac{-||\mathbf x - \mathbf x_0| + |\mathbf x - \mathbf x_1| - R|^2}{2\sigma^2}\right) ?$$

small medium large

gives you something that looks reasonable, but you can see that the level sets are not perfect. Regardless, the spread of the variance doesn't depend on how big the ellipse is.