Skip to content

fitEllipseAMS

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

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

ARGUMENTSpoints
FUNCTIONfitEllipseAMS
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 Approximate Mean Square (AMS) proposed by [Taubin1991] 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. If the fit is found to be a parabolic or hyperbolic function then the standard #fitEllipse method is used. The AMS method restricts the fit to parabolic, hyperbolic and elliptical curves by imposing the condition that A^T ( D_x^T D_x + D_y^T D_y) A = 1 where the matrices Dx and Dy are the partial derivatives of the design matrix D with respect to x and y. The matrices are formed row by row applying the following to each of the points in the set: f:{align*}{ D(i,:)&=\left{x_i^2, x_i y_i, y_i^2, x_i, y_i, 1\right} & D_x(i,:)&=\left{2 x_i,y_i,0,1,0,0\right} & D_y(i,:)&=\left{0,x_i,2 y_i,0,1,0\right} f:} The AMS method minimizes the cost function f:{equation*}{ \epsilon ^2=\frac{ A^T D^T D A }{ A^T (D_x^T D_x + D_y^T D_y) A^T } f:}

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

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

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.

fitEllipseAMS(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.