Skip to content

fitEllipseDirect

geometryfunctionOpenCV 5.0.0
import { fitEllipseDirect } from '@banou/opencv-wasm'

Use after await initOpenCV(). See the initialization and named imports guide.

ARGUMENTSpoints
FUNCTIONfitEllipseDirect
RETURN TYPERotatedRect
Call structure. A void return can still write to destination arguments. The parameter descriptions define inputs, outputs and ownership.

Fits an ellipse around a set of 2D points.

The function calculates the ellipse that fits a set of 2D points. It returns the rotated rectangle in which the ellipse is inscribed. The Direct least square (Direct) method by [oy1998NumericallySD] is used.

For an ellipse, this basis set is \chi= \left(x^2, x y, y^2, x, y, 1\right), which is a set of six free coefficients A^T=\left\{A_{\text{xx}},A_{\text{xy}},A_{\text{yy}},A_x,A_y,A_0\right\}. However, to specify an ellipse, all that is needed is five numbers; the major and minor axes lengths (a,b), the position (x_0,y_0), and the orientation \theta. This is because the basis set includes lines, quadratics, parabolic and hyperbolic functions as well as elliptical functions as possible fits. The Direct method confines the fit to ellipses by ensuring that 4 A_{xx} A_{yy}- A_{xy}^2 > 0. The condition imposed is that 4 A_{xx} A_{yy}- A_{xy}^2=1 which satisfies the inequality and as the coefficients can be arbitrarily scaled is not overly restrictive.

f:{equation*}{ \epsilon ^2= A^T D^T D A \quad \text{with} \quad A^T C A =1 \quad \text{and} \quad C=\left(\begin{matrix} 0 & 0 & 2 & 0 & 0 & 0 \ 0 & -1 & 0 & 0 & 0 & 0 \ 2 & 0 & 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 \ 0 & 0 & 0 & 0 & 0 & 0 \end{matrix} \right) f:}

The minimum cost is found by solving the generalized eigenvalue problem.

f:{equation*}{ D^T D A = \lambda \left( C\right) A f:}

The system produces only one positive eigenvalue \lambda which is chosen as the solution with its eigenvector \mathbf{u}. These are used to find the coefficients

f:{equation*}{ A = \sqrt{\frac{1}{\mathbf{u}^T C \mathbf{u}}} \mathbf{u} f:} The scaling factor guarantees that A^T C A =1.

Note: Input point types are Point2i or Point2f and at least 5 points are required.

Note: getClosestEllipsePoints function can be used to compute the ellipse fitting error.

fitEllipseDirect(points: Mat): RotatedRect;
points

Input 2D point set, stored in std::vector<> or Mat

Returns

The RotatedRect result.

These signatures describe this package. Upstream documentation can mention optional backends that are absent from this build. Check runtime compatibility before choosing a backend or file format.