@@ -4066,6 +4066,88 @@ border of the containing Mat element.
4066
4066
*/
4067
4067
CV_EXPORTS_W RotatedRect fitEllipse ( InputArray points );
4068
4068
4069
+ /* * @brief Fits an ellipse around a set of 2D points.
4070
+
4071
+ The function calculates the ellipse that fits a set of 2D points.
4072
+ It returns the rotated rectangle in which the ellipse is inscribed.
4073
+ The Approximate Mean Square (AMS) proposed by @cite Taubin1991 is used.
4074
+
4075
+ For an ellipse, this basis set is \f$ \chi= \left(x^2, x y, y^2, x, y, 1\right) \f$,
4076
+ which is a set of six free coefficients \f$ A^T=\left\{A_{\text{xx}},A_{\text{xy}},A_{\text{yy}},A_x,A_y,A_0\right\} \f$.
4077
+ However, to specify an ellipse, all that is needed is five numbers; the major and minor axes lengths \f$ (a,b) \f$,
4078
+ the position \f$ (x_0,y_0) \f$, and the orientation \f$ \theta \f$. This is because the basis set includes lines,
4079
+ quadratics, parabolic and hyperbolic functions as well as elliptical functions as possible fits.
4080
+ If the fit is found to be a parabolic or hyperbolic function then the standard fitEllipse method is used.
4081
+ The AMS method restricts the fit to parabolic, hyperbolic and elliptical curves
4082
+ by imposing the condition that \f$ A^T ( D_x^T D_x + D_y^T D_y) A = 1 \f$ where
4083
+ the matrices \f$ Dx \f$ and \f$ Dy \f$ are the partial derivatives of the design matrix \f$ D \f$ with
4084
+ respect to x and y. The matrices are formed row by row applying the following to
4085
+ each of the points in the set:
4086
+ \f{align*}{
4087
+ D(i,:)&=\left\{x_i^2, x_i y_i, y_i^2, x_i, y_i, 1\right\} &
4088
+ D_x(i,:)&=\left\{2 x_i,y_i,0,1,0,0\right\} &
4089
+ D_y(i,:)&=\left\{0,x_i,2 y_i,0,1,0\right\}
4090
+ \f}
4091
+ The AMS method minimizes the cost function
4092
+ \f{equation*}{
4093
+ \epsilon ^2=\frac{ A^T D^T D A }{ A^T (D_x^T D_x + D_y^T D_y) A^T }
4094
+ \f}
4095
+
4096
+ The minimum cost is found by solving the generalized eigenvalue problem.
4097
+
4098
+ \f{equation*}{
4099
+ D^T D A = \lambda \left( D_x^T D_x + D_y^T D_y\right) A
4100
+ \f}
4101
+
4102
+ @param points Input 2D point set, stored in std::vector\<\> or Mat
4103
+ */
4104
+ CV_EXPORTS_W RotatedRect fitEllipseAMS ( InputArray points );
4105
+
4106
+
4107
+ /* * @brief Fits an ellipse around a set of 2D points.
4108
+
4109
+ The function calculates the ellipse that fits a set of 2D points.
4110
+ It returns the rotated rectangle in which the ellipse is inscribed.
4111
+ The Direct least square (Direct) method by @cite Fitzgibbon1999 is used.
4112
+
4113
+ For an ellipse, this basis set is \f$ \chi= \left(x^2, x y, y^2, x, y, 1\right) \f$,
4114
+ which is a set of six free coefficients \f$ A^T=\left\{A_{\text{xx}},A_{\text{xy}},A_{\text{yy}},A_x,A_y,A_0\right\} \f$.
4115
+ However, to specify an ellipse, all that is needed is five numbers; the major and minor axes lengths \f$ (a,b) \f$,
4116
+ the position \f$ (x_0,y_0) \f$, and the orientation \f$ \theta \f$. This is because the basis set includes lines,
4117
+ quadratics, parabolic and hyperbolic functions as well as elliptical functions as possible fits.
4118
+ The Direct method confines the fit to ellipses by ensuring that \f$ 4 A_{xx} A_{yy}- A_{xy}^2 > 0 \f$.
4119
+ The condition imposed is that \f$ 4 A_{xx} A_{yy}- A_{xy}^2=1 \f$ which satisfies the inequality
4120
+ and as the coefficients can be arbitrarily scaled is not overly restrictive.
4121
+
4122
+ \f{equation*}{
4123
+ \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}
4124
+ 0 & 0 & 2 & 0 & 0 & 0 \\
4125
+ 0 & -1 & 0 & 0 & 0 & 0 \\
4126
+ 2 & 0 & 0 & 0 & 0 & 0 \\
4127
+ 0 & 0 & 0 & 0 & 0 & 0 \\
4128
+ 0 & 0 & 0 & 0 & 0 & 0 \\
4129
+ 0 & 0 & 0 & 0 & 0 & 0
4130
+ \end{matrix} \right)
4131
+ \f}
4132
+
4133
+ The minimum cost is found by solving the generalized eigenvalue problem.
4134
+
4135
+ \f{equation*}{
4136
+ D^T D A = \lambda \left( C\right) A
4137
+ \f}
4138
+
4139
+ The system produces only one positive eigenvalue \f$ \lambda\f$ which is chosen as the solution
4140
+ with its eigenvector \f$\mathbf{u}\f$. These are used to find the coefficients
4141
+
4142
+ \f{equation*}{
4143
+ A = \sqrt{\frac{1}{\mathbf{u}^T C \mathbf{u}}} \mathbf{u}
4144
+ \f}
4145
+ The scaling factor guarantees that \f$A^T C A =1\f$.
4146
+
4147
+ @param points Input 2D point set, stored in std::vector\<\> or Mat
4148
+ */
4149
+ CV_EXPORTS_W RotatedRect fitEllipseDirect ( InputArray points );
4150
+
4069
4151
/* * @brief Fits a line to a 2D or 3D point set.
4070
4152
4071
4153
The function fitLine fits a line to a 2D or 3D point set by minimizing \f$\sum_i \rho(r_i)\f$ where
0 commit comments