Least Squares Fitting of Data - Geometric Tools

0 downloads 154 Views 254KB Size Report
Jul 15, 1999 - Define the error function for the least squares minimization to be ..... applied to solve these equations
Least Squares Fitting of Data David Eberly, Geometric Tools, Redmond WA 98052 https://www.geometrictools.com/ This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. Created: July 15, 1999 Last Modified: November 20, 2016

Contents 1 Linear Fitting of 2D Points of the Form (x, f (x))

2

2 Linear Fitting of nD Points Using Orthogonal Regression

2

3 Planar Fitting of 3D Points of the Form (x, y, f (x, y))

3

4 Hyperplanar Fitting of nD Points Using Orthogonal Regression

4

5 kD Flat Fitting of nD Points Using Orthogonal Regression

5

6 Fitting a Circle to 2D Points

7

6.1

Direct Least Squares Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

6.2

Estimation of Coefficients to a Quadratic Equation . . . . . . . . . . . . . . . . . . . . . . . .

8

7 Fitting a Sphere to 3D Points

9

7.1

Direct Least Squares Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

7.2

Estimation of Coefficients to a Quadratic Equation . . . . . . . . . . . . . . . . . . . . . . . .

10

8 Fitting an Ellipse to 2D Points

11

9 Fitting an Ellipsoid to 3D Points

11

10 Fitting a Paraboloid to 3D Points of the Form (x, y, f (x, y))

12

1

This document describes some algorithms for fitting 2D or 3D point sets by linear or quadratic structures using least squares minimization.

1

Linear Fitting of 2D Points of the Form (x, f (x))

This is the usual introduction to least squares fit by a line when the data represents measurements where the y-component is assumed to be functionally dependent on the x-component. Given a set of samples {(xi , yi )}m i=1 , determine A and B so that the line y = Ax + B best fits the samples in the sense that the sum of the squared errors between the yi and the line values Axi + B is minimized. Note that the error is measured only in the y-direction. Define the error function for the least squares minimization to be E(A, B) =

m X

[(Axi + B) − yi ]2

(1)

i=1

This function is nonnegative and its graph is a paraboloid whose vertex occurs when the gradient satistfies ∇E = (0, 0). This leads to a system of two linear equations in A and B which can be easily solved. Precisely, (0, 0) = ∇E = 2

m X

[(Axi + B) − yi ](xi , 1)

(2)

i=1

and so

 P m x2  Pi=1 i m i=1 xi

Pm

i=1 xi Pm i=1 1



 P  m xi yi i=1  = P  m B y i=1 i A



(3)

The solution provides the least squares solution y = Ax + B. If implemented directly, this formulation can linear system. To avoid this, you Pmlead to an ill-conditioned Pm 1 1 x and y ¯ = y and subtract them from the data, should first compute the averages x ¯ = m i i i=1 i=1 m xi ← xi − x ¯ and yi ← yi − y¯. The fitted line is y − y¯ = A(x − x ¯), where Pm (x − x ¯)(yi − y¯) Pm i A = i=1 (4) ¯)2 i=1 (xi − x An implementation is GteApprHeightLine2.h.

2

Linear Fitting of nD Points Using Orthogonal Regression

It is also possible to fit a line using least squares where the errors are measured orthogonally to the proposed line rather than measured vertically. The following argument holds for sample points and lines in n dimensions. Let the line be L(t) = tD + A where D is unit length. Define Xi to be the sample points; then ⊥ Xi = A + di D + pi D⊥ i , where di = D · (Xi − A) and Di is some unit length vector perpendicular to D with appropriate coefficient pi . Define Yi = Xi − A. The vector from Xi to its projection onto the line is

2

Yi − di D = pi D⊥ vector is p2i = (Yi − di D)2 . The error function for the least i . The squared length Pm of this 2 squares minimization is E(A, D) = i=1 pi . Two alternate forms for this function are m  X

h i  T YT Yi i I − DD

(5)

! m h i X T (Yi · Yi )I − Yi Yi D = DT M (A)D

(6)

E(A, D) =

i=1

and T

E(A, D) = D

i=1

Compute the derivative of equation (5) with respect to A to obtain m

iX h ∂E = −2 I − DDT Yi ∂A i=1

(7)

Pm Pm The derivative is zero when i=1 Yi = 0 in which case A = (1/m) i=1 Xi , the average of the sample points. In fact there are infinitely many solutions A + sD for any scalar s. This is simply a statement that the average is on the best-fit line, but any other point on the line may serve as the origin for that line. Equation (6) is a quadratic form DT M (A)D whose minimum is the smallest eigenvalue of M (A), computed using standard eigensystem solvers. A corresponding unit length eigenvector D P completes our construction m T of the least squares line. The covariance matrix of the input points is C = i=1 Yi Yi . Defining δ = Pm T i=1 Yi Yi , we see that M = δI − C, where I is the identity matrix. Therefore, M and C have the same eigenspaces. The eigenspace corresponding to the minimum eigenvalue of M is the same as the eigenspace corresponding to the maximum eigenvalue of C. In an implementation, it is sufficient to process C and avoid the additional cost to compute M . An implementation for 2D is GteApprOrthogonalLine2.h and an implementation for 3D is GteApprOrthogonalLine3.h.

3

Planar Fitting of 3D Points of the Form (x, y, f (x, y))

The assumption is that the z-component of the data is functionally dependent on the x- and y-components. Given a set of samples {(xi , yi , zi )}m i=1 , determine A, B, and C so that the plane z = Ax + By + C best fits the samples in the sense that the sum of the squared errors between the zi and the plane values Axi +Byi +C is minimized. Note that the error is measured only in the z-direction. Define the error function for the least squares minimization to be E(A, B, C) =

m X [(Axi + Byi + C) − zi ]2

(8)

i=1

This function is nonnegative and its graph is a hyperparaboloid whose vertex occurs when the gradient satistfies ∇E = (0, 0, 0). This leads to a system of three linear equations in A, B, and C which can be easily solved. Precisely, m X (0, 0, 0) = ∇E = 2 [(Axi + Byi + C) − zi ](xi , yi , 1) (9) i=1

3

and so

 P m 2 i=1 xi   Pm  xy  Pi=1 i i m i=1 xi

Pm

Pm

i=1 xi yi Pm 2 i=1 yi Pm i=1 yi

i=1 xi Pm i=1 yi Pm i=1 1

 P m i=1 xi zi      Pm   B  =  yz   Pi=1 i i  m C i=1 zi 

A



   . 

(10)

The solution provides the least squares solution z = Ax + By + C. If implemented directly, this formulation P can lead to an ill-conditioned linear system. To avoid this, you Pm Pm m 1 1 1 should first compute the averages x ¯= m x , y ¯ = y , and z ¯ = z , and then subtract i i i i=1 i=1 i=1 m m them from the data, xi ← xi − x ¯, yi ← yi − y¯, and zi ← zi − z¯. The fitted plane is z − z¯ = A(x − x ¯) + B(y − y¯), where    −1  P  Pm Pm m 2 (x − x ¯ )(y − y ¯ ) (x − x ¯ )(z − z ¯ ) A (x − x ¯ ) i i i=1 i   =  P i=1 i   Pi=1 i  (11) Pm m m 2 B ¯)(yi − y¯) ¯) ¯)(zi − z¯) i=1 (xi − x i=1 (yi − y i=1 (yi − y An implementation is GteApprHeightPlane3.h.

4

Hyperplanar Fitting of nD Points Using Orthogonal Regression

It is also possible to fit a plane using least squares where the errors are measured orthogonally to the proposed plane rather than measured vertically. The following argument holds for sample points and hyperplanes in n dimensions. Let the hyperplane be N · (X − A) = 0 where N is a unit length normal to the hyperplane and A is a point on the hyperplane. Define Xi to be the sample points; then Xi = A + λi N + pi N⊥ i , where λi = N · (Xi − A) and N⊥ is some unit length vector perpendicular to N with appropriate coefficient pi . i Define Yi = Xi − A. The vector from Xi to its projection onto the hyperplane is λi N. The squared length Pm of this vector is λ2i = (N · Yi )2 . The error function for the least squares minimization is E(A, N) = i=1 λ2i . Two alternate forms for this function are E(A, N) =

m  X

h i  T YT Yi i NN

(12)

i=1

and E(A, N) = N

T

m X

! Yi YT i

N = NT CN

(13)

i=1

where C =

Pm

i=1

Yi YT i.

Compute the derivative of equation (12) with respect to A to obtain m

 X ∂E = −2 NNT Yi ∂A i=1

(14)

Pm Pm The derivative is zero when i=1 Yi = 0 in which case A = (1/m) i=1 Xi , the average of the sample points. In fact there are infinitely many solutions A + W, where W is any vector perpendicular to N. This is simply a statement that the average is on the best-fit hyperplane, but any other point on the hyperplane may serve as the origin for that hyperplane. 4

Equation (13) is a quadratic form DT CD whose minimum is the smallest eigenvalue of C, computed using standard eigensystem solvers. A corresponding unit length eigenvector N completes our construction of the least squares hyperplane. An implementation for 3D is GteApprOrthogonalPlane3.h.

5

kD Flat Fitting of nD Points Using Orthogonal Regression

Orthogonal regression to fit n-dimensional points by a line or by a hyperplane may be generalized to fitting by a flat of affine subspace. To emphasize the dimension of the flat, sometimes the terminology used is k-flat, where the dimension is k with 0 ≤ k ≤ n. A line is a 1-flat and a hyperplane is a (n − 1)-flat. For dimension n = 3, we fit with flats that are either lines or planes. For dimensions n ≥ 4 and flat dimensions 2 ≤ k ≤ n − 2, the generalization of orthogonal regression is the following. The bases mentioned here are for the linear portion of the affine subspace; that is, the basis vectors are relative to an origin at a point A. The flat has an orthonormal basis {Fj }kj=1 and the orthogonal complement has an orthonormal n basis {Pj }n−k j=1 . The union of the two bases is an orthonormal basis for IR . Any input point Xi , 1 ≤ i ≤ m, can be represented by     k n−k n−k k X X X X fij Fj  +  pij Pj  pij Pj = A + (15) fij Fj + Xi = A + j=1

j=1

j=1

j=1

The left-parenthesized term is the portion of Xi that lives in the flat and the right-parenthesized is the portion that is the deviation of Xi from the flat. The least squares problem is about choosing the two bases so that the sum of squared lengths of the deviations is as small as possible. Define Yi = Xi − A. The basis coefficients are fij = Fj · Yi and pij = Pj · Yi . The squared length of Pn−k the deviation is j=1 p2ij . The error function for the least squares minimization is the sum of the squared Pm Pn−k lengths for all inputs, E = i=1 j=1 p2ij . Two alternate forms for this function are   m n−k X X   Yi E(A, P1 , . . . Pn−k ) = YT Pj PT (16) i j i=1

j=1

and E(A, P1 , . . . Pn−k ) =

n−k X

m X

PT j

j=1

where C =

P

i=1

! Yi YT i

Pj =

i=1

n−k X

Pj CPj

(17)

j=1

Yi YT i.

Compute the derivative of equation (16) with respect to A to obtain   n−k m X X ∂E  = 2 Pj PT Yi j ∂A j=1 i=1

(18)

Pm Pm The derivative is zero when i=1 Yi = 0 in which case A = (1/m) i=1 Xi , the average of the sample points. In fact there are infinitely many solutions A + W, where W is any vector in the orthogonal complement 5

of the subspace spanned by the Pj ; this subspace is that spanned by the Fj . This is simply a statement that the average is on the best-fit flat, but any other point on the flat may serve as the origin for that flat. Because we are choosing A to be the average of the input points, the matrix C is the covariance matrix for the input points. The last term in equation (17) is a sum of quadratic forms involving the matrix C and the vectors Pj that are a basis (unit-length, mutually perpendicular). The minimum value of the quadratic form is the smallest eigenvalue λ1 of C, so we may choose P1 to be a unit-length eigenvector of C corresponding to λ1 . We must choose P2 to be unit length and perpendicular to P1 . If the eigenspace for λ1 is 1-dimensional, the next smallest value we can attain by the quadratic form is the smallest eigenvalue λ2 for which λ1 < λ2 . P2 is chosen to be a corresponding unit-length eigenvector. However, if λ1 has an eigenspace of dimension larger than 1, we can choose P2 in that eigenspace but which is perpendicular to P1 . Generally, let {λ` }r`=1 be the r distinct eigenvalues of the covariance matrix C; we know Pr1 ≤ r ≤ n and λ1 < λ2 < · · · < λr . Let the dimension of the eigenspace for λ` be d` ≥ 1; we know that `=1 d` = n. List the eigenvalues and eigenvectors in order of increasing eigenvalue, including repeated values, λ1 V11 |

···

λ1

λ2

··· {z

V1d1

V21

d1 terms

}

|

···

λ2

··· {z

V2d2

d2 terms

··· }

|

λr

···

λr

Vr1

··· {z

Vrdr

dr terms

(19) }

` The list has n items. The eigenvalue d` has an eigenspace with orthonormal basis {V`j }dj=1 . In this list, choose the first k eigenvectors to be P1 through Pk and choose the last n − k eigenvectors to be F1 through Fn−k .

It is possible that one (or more) of the Pj and one (or more) of the Fj are in the eigenspace for the same eigenvalue. In this case, the fitted flat is not unique, and one should re-examine the choice of dimension k for the fitted flat. This is analogous to the following situations in dimension n = 3. • The input points are nearly collinear but you are trying to fit those points with a plane. The covariance matrix likely has two distinct eigenvalues λ1 < λ2 with d1 = 2 and d2 = 1. The basis vectors are P1 = V11 , F1 = V12 , and F2 = V21 . The first two of these are from the same eigenspace. • The input points are spread out over a portion of a plane (and are not nearly collinear) but you are trying to fit those points with a line. The covariance matrix likely has two distinct eigenvalues λ1 < λ2 with d1 = 1 and d2 = 2. The basis vectors are P1 = V11 , P2 = V21 , and F1 = V22 . The last two of these are from the same eigenspace. • The input points are not well fit by a flat of any dimension. For example, your input points are uniformly distributed over a sphere. The covariance matrix likely has one distinct eigenvalue λ1 of multiplicity d1 = 3. Neither a line nor a plane is a good fit to the input points—in either case, a P-vector and an F-vector are in the same eigenspace. The computational algorithm is to compute the average A and covariance matrix of the points. Use an eigensolver whose output eigenvalues are sorted in nondecreasing order. Choose the Fj to be the last n − k eigenvectors output by the eigensolver.

6

6

Fitting a Circle to 2D Points

There are several ways to define a least squares error function to try to fit a circle to points. The typical approach is to define a sum of squares function and use the Levenberg–Marquardt algorithm for iterative minimization. The methods proposed here do not follow this paradigm. The first section uses the sum of squares of the differences between the purported circle radius and the distance from an input point to the purported circle. The algorithm is effectively a fixed-point iteration. The second section estimates the coefficients of a quadratic equation of two variables. It reduces to computing the eigenvector associated with the minimum eigenvalue of a matrix. Regardless of the error function, in practice the fitting produces reasonable results when the input points are distributed over the circle. If the input points are restricted to a small arc of a circle, the fitting is typically not of good quality. The problem is that small perturbations in the input points can lead to large changes in the estimates for the center and radius.

6.1

Direct Least Squares Algorithm

2 2 2 Given a set of points {(xi , yi )}m i=1 , m ≥ 3, fit them with a circle (x − a) + (y − b) = r , where (a, b) is the circle center and r is the circle radius. An assumption of this algorithm is that not all the points are collinear. The error function to be minimized is

E(a, b, r) =

m X

(Li − r)2

(20)

i=1

where Li =

p

(xi − a)2 + (yi − b)2 . Compute the partial derivative with respect to r to obtain m X ∂E = −2 (Li − r) ∂r i=1

(21)

Setting the derivative equal to zero yields m

r=

1 X Li m i=1

(22)

Compute the partial derivatives with respect to a and b to obtain ∂E ∂a

= −2

∂E ∂b

=

Pm

i=1 (Li Pm −2 i=1 (Li

i − r) ∂L ∂a = 2 i − r) ∂L ∂b =

Pm

i=1 Pm 2 i=1

 i (xi − a) + r ∂L ∂a ,  i (yi − b) + r ∂L ∂b

(23)

Setting these derivatives equal to zero yields m

a=

m

m

m

1 X 1 X ∂Li 1 X 1 X ∂Li xi + r , b= yi + r m i=1 m i=1 ∂a m i=1 m i=1 ∂b

(24)

Replace r from equation (22) and using using ∂Li /∂a = (a − xi )/Li and ∂Li /∂b = (b − yi )/Li , we obtain two nonlinear equations in a and b, ¯L ¯ a = F (a, b), b = y¯ + L ¯L ¯ b = G(a, b) a=x ¯+L 7

(25)

where the last equality of each expression defines the functions F (a, b) and G(a, b) and where m

x ¯=

m

m

m

m

X X a − xi X b − yi 1 X 1 X ¯= 1 ¯a = 1 ¯b = 1 xi , y¯ = yi , L Li , L , L m i=1 m i=1 m i=1 m i=1 Li m i=1 Li

(26)

Fixed-point iteration can be applied to solve these equations: a0 = x ¯, b0 = y¯, and ai+1 = F (ai , bi ) and bi+1 = G(ai , bi ) for i ≥ 0. An implementation is GteApprCircle2.h.

6.2

Estimation of Coefficients to a Quadratic Equation

The quadratic equation that represents a circle is 0 = c0 + c1 x + c2 y + c3 (x2 + y 2 ) = (c0 , c1 , c2 , c3 ) · (1, x, y, x2 + y 2 ) = C · V

(27)

where the last equality defines V and a unit-length vector C = (c0 , c1 , c2 , c3 ) with c3 6= 0 (for a nondegenerate circle). Given a set of input points {(xi , yi )}m i=1 , the least squares error function is chosen to be ! m m X X E(C) = (C · Vi )2 = CT Vi VT C = CT MC (28) i i=1

where Vi = (1, xi , yi , ri2 ) with ri2 = x2i + yi2 .  P m i=1 1   Pm  i=1 xi M =  Pm  y  Pi=1 i m 2 i=1 ri

i=1

The last equality defines the 4 × 4 symmetric matrix M , Pm Pm Pm 2  i=1 xi i=1 yi i=1 ri  Pm 2 Pm Pm 2   x x y x r i=1 i i=1 i i i=1 i i  (29) Pm Pm 2 Pm 2   i=1 xi yi i=1 yi i=1 yi ri  Pm Pm Pm 4 2 2 i=1 xi ri i=1 yi ri i=1 ri

The error function is a quadratic form that is minimized when C is a unit-length eigenvector corresponding to the minimum eigenvalue of M . An eigensolver can be used to compute the eigenvector. The resulting equation appears not to be invariant to translations of input points. You Pthe Pmshould instead m 1 1 x and y ¯ = consider modifying the inputs by computing the averages x ¯ = m i=1 i i=1 yi and then m replacing xi ← xi − x ¯ and yi ← yi − y¯. The ri2 are then computed from the adjusted xi and yi values. The matrix becomes  P Pm 2  m 1 0 0 i=1 i=1 ri   Pm Pm Pm 2  2    0 x y x r x i=1 i i=1 i i i=1 i i  ˆ = (30) M Pm 2 Pm Pm    0 yi yi ri2  xi yi i=1 i=1 i=1  P Pm Pm Pm 4  m 2 2 2 r x r y r i i i i i=1 i i=1 i=1 i=1 ri The eigenvector C is computed. x, y¯) − (c1 , c2 )/(2c3 ) p The circle center in the original coordinate system is (¯ and the circle radius is r = c21 + c22 − 4c0 c3 /(2c3 ). An implementation is template class ApprQuadraticCircle2 in GteApprQuadratic2.h. 8

7

Fitting a Sphere to 3D Points

There are several ways to define a least squares error function to try to fit a sphere to points. The typical approach is to define a sum of squares function and use the Levenberg–Marquardt algorithm for iterative minimization. The methods proposed here do not follow this paradigm. The first section uses the sum of squares of the differences between the purported sphere radius and the distance from an input point to the purported sphere. The algorithm is effectively a fixed-point iteration. The second section estimates the coefficients of a quadratic equation of three variables. It reduces to computing the eigenvector associated with the minimum eigenvalue of a matrix. Regardless of the error function, in practice the fitting produces reasonable results when the input points are distributed over the sphere. If the input points are restricted to a small solid angle, the fitting is typically not of good quality. The problem is that small perturbations in the input points can lead to large changes in the estimates for the center and radius.

7.1

Direct Least Squares Algorithm

2 2 2 2 Given a set of points {(xi , yi , zi )}m i=1 , m ≥ 4, fit them with a sphere (x − a) + (y − b) + (z − c) = r where (a, b, c) is the sphere center and r is the sphere radius. An assumption of this algorithm is that not all the points are coplanar. The error function to be minimized is

E(a, b, c, r) =

m X (Li − r)2

(31)

i=1

where Li =

p

(xi − a)2 + (yi − b)2 + (zi − c). Compute the partial derivative with respect to r to obtain m X ∂E (Li − r). = −2 ∂r i=1

(32)

Setting the derivative equal to zero yields m

1 X r= Li . m i=1

(33)

Compute the partial derivatives with respect to a, b, and c to obtain ∂E ∂a

= −2

∂E ∂b

=

∂E ∂c

=

Pm

i=1 (Li Pm −2 i=1 (Li Pm −2 i=1 (Li

i − r) ∂L ∂a = 2 i − r) ∂L ∂b = i − r) ∂L ∂c =

Pm

i=1 Pm 2 i=1 Pm 2 i=1

 i (xi − a) + r ∂L ∂a ,  i (yi − b) + r ∂L ∂b ,  i (zi − c) + r ∂L ∂c

(34)

Setting these derivatives equal to zero yields m

a=

m

m

m

m

m

1 X 1 X ∂Li 1 X 1 X ∂Li 1 X 1 X ∂Li xi + r , b= yi + r , c= zi + r m i=1 m i=1 ∂a m i=1 m i=1 ∂b m i=1 m i=1 ∂c

9

(35)

Replacing r from equation (33) and using ∂Li /∂a = (a − xi )/Li , ∂Li /∂b = (b − yi )/Li , and ∂Li /∂c = (c − zi )/Li , we obtain three nonlinear equations in a, b, and c, ¯L ¯ a = F (a, b, c), b = y¯ + L ¯L ¯ b = G(a, b, c), c = z¯ + L ¯L ¯ c = H(a, b, c) a=x ¯+L

(36)

where the last equality defines functions F (a, b, c), G(a, b, c), and H(a, b, c) and where x ¯=

1 m

Pm

1 m

Pm

i=1

xi , y¯ =

Pm

1 m

i=1

yi , z¯ =

1 m

Pm

i=1 zi

(37) ¯= L

i=1

¯a = Li , L

1 m

Pm

i=1

a−xi Li ,

¯b = L

1 m

Pm

i=1

b−yi Li ,

¯c = L

1 m

Pm

i=1

c−zi Li

Fixed-point iteration can be applied to solving these equations: a0 = x ¯, b0 = y¯, c0 = z¯, and ai+1 = F (ai , bi , ci ), bi+1 = G(ai , bi , ci ), and ci+1 = H(ai , bi , ci ) for i ≥ 0. An implementation is GteApprSphere3.h.

7.2

Estimation of Coefficients to a Quadratic Equation

The quadratic equation that represents a sphere is 0 = c0 + c1 x + c2 y + c3 z + c4 (x2 + y 2 + z 2 ) = (c0 , c1 , c2 , c3 , c4 ) · (1, x, y, z, x2 + y 2 + z 2 ) = C · V

(38)

where the last equality defines V and a unit-length vector C = (c0 , c1 , c2 , c3 , c4 ) with c4 6= 0 (for a nondegenerate sphere). Given a set of input points {(xi , yi , zi )}m i=1 , the least squares error function is chosen to be ! m m X X C = CT MC (39) E(C) = (C · Vi )2 = CT Vi VT i i=1

where Vi =

(1, xi , yi , zi , ri2 )

ri2

with =  P m i=1 1   Pm  i=1 xi   Pm M = i=1 yi   Pm  z  Pi=1 i m 2 i=1 ri

i=1

x2i + yi2 + zi2 . Pm

xi

Pm

x2i

Pm

xi yi

Pm

xi zi

Pm

xi ri2

i=1

i=1

i=1 i=1 i=1

The last equality defines the 5 × 5 symmetric matrix M , Pm Pm Pm 2  y z i=1 i i=1 i i=1 ri  Pm Pm Pm 2   i=1 xi yi i=1 xi zi i=1 xi ri  Pm 2 Pm Pm 2  (40)  i=1 yi i=1 yi zi i=1 yi ri  Pm Pm 2 Pm  2  i=1 zi ri  i=1 yi zi i=1 zi P P Pm m m 2 4 2 i=1 yi ri i=1 zi ri i=1 ri

The error function is a quadratic form that is minimized when C is a unit-length eigenvector corresponding to the minimum eigenvalue of M . An eigensolver can be used to compute the eigenvector. The resulting equation appears not to be invariant to translations points. You should P instead Pmof the input P m m 1 1 1 ¯= m ¯= m consider modifying the inputs by computing the averages x ¯= m i=1 xi , y i=1 yi , and z i=1 zi and then replacing xi ← xi − x ¯, yi ← yi − y¯, and zi ← zi − z¯. The ri2 are then computed from the adjusted

10

xi , yi , and zi values. The matrix becomes  P m 0 i=1 1  Pm 2   0 i=1 xi  P  m ˆ = 0 M i=1 xi yi  Pm   0 i=1 xi zi  P P m m 2 2 i=1 xi ri i=1 ri

0 Pm

i=1 xi yi Pm 2 i=1 yi Pm i=1 yi zi Pm 2 i=1 yi ri

Pm

0 Pm

i=1 xi zi

Pm

i=1

yi zi

Pm

2 i=1 zi Pm 2 i=1 zi ri

i=1 Pm i=1 Pm i=1 Pm i=1 Pm i=1

ri2



  xi ri2   2  yi ri    zi ri2   4 ri

(41)

The eigenvector C is computed. x, y¯, z¯)−(c1 , c2 , c3 )/(2c4 ) p The sphere center in the original coordinate system is (¯ and the sphere radius is r = c21 + c22 + c23 − 4c0 c4 /(2c4 ). An implementation is template class ApprQuadraticSphere3 in GteApprQuadratic3.h.

8

Fitting an Ellipse to 2D Points

T T Given a set of points {Xi }m i=1 , m ≥ 3, fit them with an ellipse (X − U) R DR(X − U) = 1 where U is the ellipse center, R is an orthonormal matrix representing the ellipse orientation, and D is a diagonal matrix whose diagonal entries represent the reciprocal of the squares of the half-lengths lengths of the axes of the ellipse. An axis-aligned ellipse with center at the origin has equation (x/a)2 + (y/b)2 = 1. In this setting, U = (0, 0), R = I (the identity matrix), and D = diag(1/a2 , 1/b2 ). The error function to be minimized is

E(U, R, D) =

m X (Li − r)2

(42)

i=1

where Li is the distance from Xi to the ellipse with the given parameters. This problem is more difficult than that of fitting circles. The distance Li is computed according to the algorithm described in Distance from a Point to an Ellipse, an Ellipsoid, or a Hyperellipsoid. The function E is minimized iteratively using Powell’s direction-set method to search for a minimum. An implementation is GteApprEllipse2.h.

9

Fitting an Ellipsoid to 3D Points

T T Given a set of points {Xi }m i=1 , m ≥ 3, fit them with an ellipsoid (X − U) R DR(X − U) = 1 where U is the ellipsoid center and R is an orthonormal matrix representing the ellipsoid orientation. The matrix D is a diagonal matrix whose diagonal entries represent the reciprocal of the squares of the half-lengths of the axes of the ellipsoid. An axis-aligned ellipsoid with center at the origin has equation (x/a)2 + (y/b)2 + (z/c)2 = 1. In this setting, U = (0, 0, 0), R = I (the identity matrix), and D = diag(1/a2 , 1/b2 , 1/c2 ). The error function to be minimized is m X E(U, R, D) = (Li − r)2 (43) i=1

where Li is the distance from Xi to the ellipse with the given parameters.

11

This problem is more difficult than that of fitting spheres. The distance Li is computed according to the algorithm described in Distance from a Point to an Ellipse, an Ellipsoid, or a Hyperellipsoid. The function E is minimized iteratively using Powell’s direction-set method to search for a minimum. An implementation is GteApprEllipsoid3.h.

10

Fitting a Paraboloid to 3D Points of the Form (x, y, f (x, y))

Given a set of samples {(xi , yi , zi )}m i=1 and assuming that the true values lie on a paraboloid z = f (x, y) = p1 x2 + p2 xy + p3 y 2 + p4 x + p5 y + p6 = P · Q(x, y)

(44)

where P = (p1 , p2 , p3 , p4 , p5 , p6 ) and Q(x, y) = (x2 , xy, y 2 , x, y, 1), select P to minimize the sum of squared errors m X E(P) = (P · Qi − zi )2 (45) i=1

where Qi = Q(xi , yi ). The minimum occurs when the gradient of E is the zero vector, ∇E = 2

m X (P · Qi − zi )Qi = 0.

(46)

i=1

Some algebra converts this to a system of 6 equations in 6 unknowns: ! m m X X T zi Qi . Qi Qi P =

(47)

i=1

i=1

T The product Qi QT i is a product of the 6 × 1 matrix Qi with the 1 × 6 matrix Qi , the result being a 6 × 6 matrix. Pm Pm Define the 6 × 6 symmetric matrix A = i=1 Qi QT i and the 6 × 1 vector B = i=1 zi Qi . The choice for P is the solution to the linear system of equations AP = B. ThePentries of A and B indicate summations over m the appropriate product of variables. For example, s(x3 y) = i=1 x3i yi :      s(x4 ) s(x3 y) s(x2 y 2 ) s(x3 ) s(x2 y) s(x2 ) p1 s(zx2 )            s(x3 y) s(x2 y 2 ) s(xy 3 ) s(x2 y) s(xy 2 ) s(xy)   p2   s(zxy)             s(x2 y 2 ) s(xy 3 ) s(y 4 ) s(xy 2 ) s(y 3 ) s(y 2 )   p3   s(zy 2 )    =  (48)       s(x3 ) s(x2 y) s(xy 2 ) s(x2 ) s(xy) s(x)   p4   s(zx)             s(x2 y) s(xy 2 ) s(y 3 ) s(xy) s(y 2 ) s(y)   p5   s(zy)       s(x2 ) s(xy) s(y 2 ) s(x) s(y) s(1) p6 s(z)

An implementation is GteApprParaboloid3.h.

12