$
\def \J {\Delta}
\def \half {\frac{1}{2}}
\def \kwart {\frac{1}{4}}
\def \hieruit {\quad \Longrightarrow \quad}
\def \slechts {\quad \Longleftrightarrow \quad}
\def \norm {\frac{1}{\sigma \sqrt{2\pi}} \; }
\def \EN {\quad \mbox{and} \quad}
\def \OF {\quad \mbox{or} \quad}
\def \wit {\quad \mbox{;} \quad}
\def \spekhaken {\iint}
\newcommand{\dq}[2]{\displaystyle \frac{\partial #1}{\partial #2}}
\newcommand{\oq}[2]{\partial #1 / \partial #2}
\newcommand{\qq}[3]{\frac{\partial^2 #1}{{\partial #2}{\partial #3}}}
\def \erf {\operatorname{Erf}}
$
Bounding Ellipse
When it comes to the preprocessing of domains and contours, Bounding Ellipses
are virtually indespensible. They are far more handsome than bounding boxes of
whatever kind in the first place. The only quantities to be determined are the
midpoint and the second order variances of the body (or its boundary curve)
under consideration. A bounding ellipse is then based upon the inverse
tensor of inertia / matrix of variances. The equation of an ellipse of inertia
is given by:
$$
\left[ \begin{array}{cc} (x-\mu_x) & (y-\mu_y) \end{array} \right]
\left[ \begin{array}{cc} \sigma_{xx} & \sigma_{xy} \\
\sigma_{xy} & \sigma_{yy} \end{array} \right]^{-1}
\left[ \begin{array}{c} (x-\mu_x) \\ (y-\mu_y) \end{array} \right] = 1
\hieruit
$$ $$
\frac{\sigma_{yy} (x-\mu_x)^2 - 2 \sigma_{xy} (x-\mu_x)(y-\mu_y) +
\sigma_{xx} (y-\mu_y)^2}{\sigma_{xx}\sigma_{yy}-\sigma_{xy}^2} = E
$$
Where $\sigma$ are the variances and $\mu$ are the midpoint coordinates.
A bounding ellipse is defined as an ellipse of inertia which its right hand side
modified: the constant $1$ is replaced by a another constant $E$.
Here $E$ should be adapted in such a way that the whole boundary of the area of
interest is contained inside the ellipse. Thus $E$ is the maximum of:
$$
\frac{\sigma_{yy} (x_k-\mu_x)^2 - 2 \sigma_{xy} (x_k-\mu_x)(y_k-\mu_y)
+ \sigma_{xx} (y_k-\mu_y)^2}{\sigma_{xx}\sigma_{yy}-\sigma_{xy}^2}
$$
where ($k$) runs through all vertices of the boundary contours.
Best Fit Ellipse
Quite another problem is whether and when an ellipse of inertia can be
considered as a Best Fit Ellipse. The latter shall be defined with help
of a Least Squares minimization principle, as follows:
$$
\sum_k w_k \left[
\frac{\sigma_{yy} (x_k-\mu_x)^2 - 2 \sigma_{xy} (x_k-\mu_x)(y_k-\mu_y) +
\sigma_{xx} (y_k-\mu_y)^2}{\sigma_{xx}\sigma_{yy}-\sigma_{xy}^2}
- E \right]^2
$$
$= \operatorname{minimum}(E)$ . Differentiating to $E$ simply gives:
$$
\sum_k w_k \left[
\frac{\sigma_{yy} (x_k-\mu_x)^2 - 2 \sigma_{xy} (x_k-\mu_x)(y_k-\mu_y) +
\sigma_{xx} (y_k-\mu_y)^2}{\sigma_{xx}\sigma_{yy}-\sigma_{xy}^2}
- E \right] = 0
$$
If and only if:
$$
\sigma_{yy} \sum_k w_k (x_k-\mu_x)^2
- 2 \sigma_{xy} \sum_k w_k (x_k-\mu_x)(y_k-\mu_y) +
\sigma_{xx} \sum_k w_k (y_k-\mu_y)^2
$$ $$
= E (\sigma_{xx}\sigma_{yy}-\sigma_{xy}^2)
$$
If and only if:
$$
\sigma_{yy} \sigma_{xx} - 2 \sigma_{xy} \sigma_{xy} +
\sigma_{xx} \sigma_{yy} = 2\,(\sigma_{xx}\sigma_{yy}-\sigma_{xy}^2)
= E (\sigma_{xx}\sigma_{yy}-\sigma_{xy}^2)
$$
If and only if $E = 2$ . Thus the equation of the Best Fit Ellipse is:
$$
\sigma_{yy} (x-\mu_x)^2 - 2 \sigma_{xy} (x-\mu_x)(y-\mu_y)
+ \sigma_{xx} (y-\mu_y)^2
$$ $$
= 2\,(\sigma_{xx}\sigma_{yy}-\sigma_{xy}^2)
$$
Or alternatively:
$$
\half\:\frac{\sigma_{yy} (x-\mu_x)^2 - 2 \sigma_{xy} (x-\mu_x)(y-\mu_y)
+ \sigma_{xx} (y-\mu_y)^2}{\sigma_{xx}\sigma_{yy}-\sigma_{xy}^2} = 1
$$