Sunteți pe pagina 1din 30

MAE674-Optimal Estimation Methods

An Analysis of different Estimation tools for parameter estimation in ellipse fitting to 2D edge images
Project Report
by
Rajmohan Asokan
Person No:50097503
E-Mail ID: rasokan@buffalo.edu
Department of Mechanical and Aerospace Engineering

Contents
1 Introduction
1.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . .

2
2
2

2 Least Squares based Ellipse Fitting


2.1 Minimizing the Algebraic Distance . . . . . . . . . . . . . . . . .
2.2 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3
3
4

3 Numerically Stable Direct Least Squares


3.1 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9
11

4 Ellipse Fitting based on Approximate Maximum Likelihood


method(AML)
14
4.1 Levenberg-Marquardt Algorithm . . . . . . . . . . . . . . . . . . 15
4.2 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5 Conclusion

18

6 Appendix
19
6.1 Ellipse Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.2 Least squares ellipse fitting based on minimization of algebraic
distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.3 Numerically stable Direct Least Squares Fitting of Ellipses . . . 21
6.4 Approximate Maximum Likelihood Method . . . . . . . . . . . . 23

1
1.1

Introduction
Motivation

The field of parameter estimation has been a major research area in the field of
computer vision and graphics. Especially the parameter estimation of quadratic
curves fitted to data extracted through the application of computer vision techniques has been found important. For example, curve fitting has seen an important application in Industrial Machine Vision for parts identification and has
also been found to be useful in non destructive testing of hardware components.
The concentration of my masters research is on the application of estimation
techniques to estimate the parameters of the shape of solid objects. One such
application is in the field of archaeological sciences where excavated objects are
often pieces of a larger object which requires an efficient reconstruction.

1.2

Problem Statement

One of the important geometric primitives in computer vision is ellipse. The


problem of fitting ellipses to the image space X-Y pixel coordinates of the edge
of a solid object using the methods of Least Squares, Direct Least Squares
and Numerically Stable Direct Least Squares is analysed. Both synthetic data
and real world data are used to check the effectiveness of the methods for this
project.

Least Squares based Ellipse Fitting

The least squares approach to fitting ellipses can be categorized into geometric fit
and algebraic fit[1]. The geometric fit is obtained by minimizing the sum of the
squares of the distances to the given points whereas the algebraic fit estimates
the parameters of the conic equation ax2 + 2bxy + cy 2 + 2dx + 2ey + f = 0 using
least squares.

2.1

Minimizing the Algebraic Distance

The conic equation is given by


ax2 + 2bxy + cy 2 + 2dx + 2ey + f = 0
The above equation can be represented in matrix form as


T a b
[x y]
[x y] + [2d 2e]T [x y] + f = 0
b c
xT Ax + bT x + c = 0

(2.1)

(2.2)
(2.3)

The coordinate is rotated and translated to represent new coordinate


[x y]T = Q[
x y]T + t

(2.4)

Then
T

(Q
x + t)T A(Q
x + t) + bT (Q
x + t) + c = 0

(2.5)

(2.6)

x
(Q AQ)
x + (2t A + b )Q
x + t At + b t + c = 0
Therefore

x + bT x
x
T A
+ c = 0
(2.7)
Q is chosen in such a way that A = diag(1 , 2 ) and since the conic is ellipse t
is chosen such that b = 0.
1 x
21 + 2 x
22 + c = 0

(2.8)

The center and axes of the ellipse are given by


z=t
r

c
a=
1
r

c
b=
2

(2.9)
(2.10)
(2.11)

The eigenvalues of 1 and 2 of the matrices A and A are same because QQT = I
and is invariant to coordinate transformations. The matrix A is slightly modified
to


a11 a12
A=
(2.12)
a12 a22
3

Therefore
A = a11 a22 a212 = 1 2

(2.13)

traceA = a11 + a22 = 1 + 2

(2.14)

The ellipse axes quotient is given by


k = a/b
21

(2.15)
22

+
1
=
2
k
1 2
Which can be summarized into the form
p
k 2 = 2 1
k2 +

(2.16)

(2.17)

where is given by
(trace A)2
1
2 det A
Now the constrained least squares problem given by
=

(2.18)

xT Ax + bT x + c 0

(2.19)

21 + 22 = 1

(2.20)

with the constraint


Reconstructing the equation to the form
S[v w]T 0

(2.21)

where S is the coefficient matrix and the vectors v and w are given by
v = [b1 , b2 , c]T

w = [a11 , 2a12 , a22 ]T


The QR decomposition of the coefficient matrix S results in

 
R11 R12 v
0
0
R22 w

(2.22)
(2.23)

(2.24)

Therefore
R22 w 0

(2.25)

which must satisfy the constraint kwk = 1 and this may be solved using singular
value decomposition
X
R22 = U
VT
(2.26)
where w is given by w = v3 and v is solved by
1
v = R11
R12 w

Thus the parameters of the ellipse is obtained.

2.2

Results

The true ellipse is generated as below


4

(2.27)

Truth
3

2.5

1.5

0.5

0.5

1.5

2
4.5

3.5

2.5

1.5

0.5

Figure 1: True Ellipse


Now the dataset from the synthetic ellipse generated is used see if an ellipse
is getting generated
Least Squares Ellipse FittingBookstein Constraint
3
Linear LS Fit
Data Points

2.5
2
1.5
1
0.5
0
0.5
1
1.5
2
4.5

3.5

2.5

1.5

0.5

Figure 2: Ellipse for Partial Data-1


The algorithm was able to pick up the partial data and generate the true
ellipse parameters. Now the algorithm is implemented on the edge pixel data
of a 2D image.
The below figure is obtained by applying the canny edge detector in MATLAB
on a mug placed on desk.

Figure 3: Edge Image


From the above figure, using bwselect command in MATLAB, the edge from
the top of the mug is obtained.

Figure 4: Selected Edge


To this edge data, the ellipses are fit using the algorithm.
Least Squares Ellipse FittingBookstein Constraint
300
Linear LS Fit
Data Points
280

260

240

220

200

180

160
140

160

180

200

220

240

260

Figure 5: Partial Edge Pixel data-1


The algorithm was once again able to generate the ellipse parameters for
noisy edge data.

Least Squares Ellipse FittingBookstein Constraint


170
Linear LS Fit
Data Points
169

168

167

166

165

164
175

180

185

190

195

200

205

210

215

220

225

Figure 6: Partial Edge Pixel data-2


In this case, the algorithm was not able to generate the ellipse parameters
that could satisfy the ellipse constraint from the partial data provided. It will be
seen that the following two methods in successive sections are able to generate
ellipses for this same partial dataset.

Numerically Stable Direct Least Squares

The numerically stable version of Direct Least Squares by Halr and Flusser [2]
is the stable version of the method by Fitzgibbon et al [3]. The ellipse equation is
F (x, y) = ax2 + bxy + cy 2 + dx + ey + f = 0

(3.1)

and the ellipse constraint equation is given by b 4ac < 0. The coefficients
of the ellipse are a, b, c, d, e, f and x, y are the coordinates of the points.The
polynomial equation is the algebraic distance of points x, y to the ellipse.
The polynomial equation is given by
Fa (X) = X.a = 0

(3.2)

where a = [a, b, c, d, e, f ]T and X = [x2 , xy, y 2 , x, y, 1]. The ellipse constraint is


scaled as
4ac b2 = 1
(3.3)
The fitting problem is formulated as
mina ||D.a||2

(3.4)

aT Ca = 1

(3.5)

The constraint is given by


where D is the design matrix and C is the constraint equation. The design
matrix, D, is decomposed into
D = (D1 |D2 )
where

and

x21
.
D1 =
.
x2N

x1 y1
.
.
xN yN

y1
.
.
yN

x1
.
D2 =
.
xN
Now, consider a scatter matrix, S, as

S
S = T1
S2

S2
S3

(3.6)

y12
.

.
2
yN

(3.7)

1
.

.
1

(3.8)


(3.9)

where S1 = D1T D1 , S2 = D1T D2 , S3 = D2T D2 The constraint matrix can also be


re-framed as


C1 0
C=
(3.10)
0 0
9


0
where C1 = 0
2

0
1
0

2
0 The coefficients of the ellipse can also be expressed as
0
a=

 
a1
a2



a
d
where a1 = b and a2 = e
c
f
By using a lagrange multiplier

 

S1 S2 a1
C
= 1
0
S2T S3 a2

(3.11)

 
0 a1
0 a2

(3.12)

The above can be expressed as


S1 a1 + S2 a2 = C1 a1

(3.13)

S2T a1 + S3 a2 = 0

(3.14)

Therefore a2 from Eq. (2.14) can be expressed as,


a2 = S31 S2T a1

(3.15)

Substituting Eq. (2.15) in Eq. (2.13)


(S1 S2 S31 S2T )a1 = C1 a1

(3.16)

Since matrix C1 is regular,


C11 (S1 S2 S31 S2T )a1 = a1

(3.17)

Therefore the constraint equations are given by


M a1 = a1

(3.18)

aT1 C1 a1 = 1

(3.19)

M = C11 (S1 S2 S31 S2T )

(3.20)

where
known as the reduced scatter matrix. The optimal solution corresponds to the
eigen vector a1 of matrix M .
Once the coefficients a of the ellipse is obtained, the ellipse is plotted with the
set of equations given to compute the aspects of the ellipse [A.1].

10

3.1

Results

The synthetic true ellipse data is generated as below


Truth
3

2.5

1.5

0.5

0.5

1.5

2
4.5

3.5

2.5

1.5

0.5

Figure 7: Truth
The data from the true elliptic arc is taken as input to the Direct Least
Squares Method.
Fitted Ellipse & Data Points
3
Ellipse Fit
Data Points
2.5

1.5

0.5

0.5

1.5

2
4.5

3.5

2.5

Figure 8: Partial data input-1

11

1.5

0.5

Fitted Ellipse & Data Points


3
Ellipse Fit
Data Points
2.5

1.5

0.5

0.5

1.5

2
4.5

3.5

2.5

1.5

0.5

Figure 9: Partial data input-2

Fitted Ellipse & Data Points


3
Ellipse Fit
Data Points
2.5

1.5

0.5

0.5

1.5

2
4.5

3.5

2.5

1.5

0.5

Figure 10: Partial data input-3


As seen from the above figures, it is evident that the Stable Direct Least
Squares is able to generate the same ellipse which conforms with the truth even
if the data sets are from different elliptic arcs. The below figures are generated
using the datasets obtained from the edge image shown in the previous section.

12

DLS Fitted Ellipse & Edge Data Points


300
Ellipse Fit
Data Points
280

260

240

220

200

180

160
140

160

180

200

220

240

260

Figure 11: Full Edge pixel data

DLS Fitted Ellipse & Edge Data Points


184
Ellipse Fit
Data Points
182

180

178

176

174

172

170

168
175

180

185

190

195

200

205

210

215

220

225

Figure 12: Partial Edge pixel Data


From the above two figures it is seen that the partial noisy edge data does not
result in the ellipse that was obtained with the complete edge data. The aspects
of the ellipse i.e. axes and center have in fact been shrunk/ changed. This
contradicts with the results of synthetic data where both complete and partial
datasets resulted in the same ellipse. Therefore the increase in the dominance
of noise in the dataset due to the reduction in the available dataset can result
in non conforming ellipses. But the advantage of this method is that still only
an ellipse is obtained.

13

Ellipse Fitting based on Approximate Maximum Likelihood method(AML)

In the previous section, the parameters were obtained by minimising an algebraic


distance using a simple procedure and the solution is ellipse specific. In this
section the ellipse fitting based on approximate maximum likelihood function is
discussed [4]. Consider the same conic function from (2.1)
ax2 + bxy + cy 2 + dx + ey + f = 0

(4.1)

The parameters to be estimated is given by


tp = [a, b, c, d, e, f ]T

(4.2)

ux = [x2 , xy, y 2 , x, y, 1]T

(4.3)

And
The constraint function is represented in the same way as in section 2
tTp F tp > 0

(4.4)

where F is given by

0
0

2
F =
0

0
0

2 0 0 0
0 0 0 0

2 0 0 0

2 0 0 0

2 0 0 0
2 0 0 0

0
1
0
0
0
0

(4.5)

The approximate maximum likelihood(AML) cost function is given by


CostAM L =

N
X
tTp An tp
tT B t
n=1 p n p

(4.6)

where An and Bn are given by


An = ux uTx

(4.7)

ux
ux T
cov
(4.8)
x
x
where cov provides the uncertainty in the edge data points. The authors use
a merit function for optimising the cost function CostAM L with respect to the
ellipse constraint and is given by
Bn =

M erit = CostAM L +

ktp k4
tTp F tp

(4.9)

The M erit function becomes infinite when tTp F tp = 0. Therefore if M erit is


started from a feasible point tTp F tp < 0 and proceeded in short steps the local
14

minimiser negotiated is feasible and if is small, then the local minimum is


convincing and is a good approximation of the local minimum of CostAM L .
The authors start the process with being a very small number and the parameters from Direct Least Squares Ellipse Fit is used as initial guess to iterate the
Levenberg-Marquardt Algorithm (LMA) to optimise the M erit function. If the
minimum is feasible satisfying tTp F tp < 0 then it is considered to be the solution. If it is not, then one step is predated in the iteration that has a minimum
and from there another iteration is performed and the new feasible minimum is
considered to be the solution.

4.1

Levenberg-Marquardt Algorithm

The LMA is used to optimise the merit function. Consider a function of tp given
by
!1
tTp An tp 2
(4.10)
rn (tp ) =
tTp Bn tp
for n = 1toN and
1

rN +1 (tp ) = 2

ktp k2
tTp F tp

The following are standard steps of Gauss-Newton and LMA


#
"
T
tTp An tp
rn (tp )
An
1
Bn tp
= rn (tp ) T
T
tp
tp Bn tp
(tp Bn tp )2


1
rN +1 (tp )T
I
ktp k2
2
F F
= 2
T
tp
tTp F tp
(tp F tp )2

(4.11)

(4.12)

(4.13)

The algorithm starts with an initial guess tp0 from Direct Least Squares Method
and the updation rule is given by
tp k+1 = tp k + k
"
#1 
T
T
r(tp k )
r(tp k )
(tp k )
k =
+ k I
r(tp )
tp
tp
tp

(4.14)
(4.15)

where k is the step size. In the rarest of cases, the LMA could overshoot and
obtain tp k+1 that is not feasible. In that scenario, the algorithm predates to the
previous feasible tp k and initiates another interative procedure to obtain better
feasible estimate. The update condition becomes
tp k+1 = tp k + l k

(4.16)

The step length l is chosen such that both tp k+1 is feasible and the cost function
is reduced. The step size is chosen using the line search method. The k is given
by
#+ 
"


tp k T
tp k T tp k
r(tp k )
(4.17)
k =
tp
tp
tp
15

and it has a pseudo-inverse term. The definition of k is prompted by


"
k = lim

r(tp k )
tp

T

r(tp k )
+ I
tp

#1 

tp k
tp

T
r(tp k )

(4.18)

where the k becomes unstable as the damping parameter approaches 0

4.2

Results

The below figures are to be compared to the true ellipse and the edge image
shown in the previous sections
Approximate Maximum Likelihood Fitted Ellipse & Edge Data Points
0.5
AMLEllipse Fit
Data Points
0

0.5

1.5

2
4.2

3.8

3.6

3.4

3.2

2.8

2.6

Figure 13: AML:Partial data input-1

Approximate Maximum Likelihood Fitted Ellipse & Edge Data Points


3
AMLEllipse Fit
Data Points

2.5
2
1.5
1
0.5
0
0.5
1
1.5
2
4.5

3.5

2.5

1.5

0.5

Figure 14: AML:Partial data input-2


Once again the synthetic data produces ellipses which are identical to the
ellipses created by Direct Least Squares and conforms to the true ellipse.

16

Approximate Maximum Likelihood Fitted Ellipse & Edge Data Points


300
AMLEllipse Fit
Data Points
280

260

240

220

200

180

160
140

160

180

200

220

240

260

Figure 15: AML:Full Edge Pixel data


Approximate Maximum Likelihood Fitted Ellipse & Edge Data Points
240
AMLEllipse Fit
Data Points
230

220

210

200

190

180

170

160
150

160

170

180

190

200

210

220

230

240

250

Figure 16: AML:Partial Edge Pixel data


Again the real datasets also produce ellipses that are identical to the ellipses
produced by Direct Least Squares.
Note : Extreme cases were not checked where one method (Direct Least Squares)
failed and the other method (Approximate Maximum Likelihood) succeeded,
vice-versa except in the case of Linear Least Squares fitting where ellipse was
not generated for the partial edge pixel data.

17

Conclusion

The three methods used in this project were able to fit the same ellipse to ample
dataset. But when the dataset from edge data became partial, the first method
failed to generate an ellipse while the other two methods generated identical
ellipses. While the partial synthetic data resulted in an ellipse from all three
methods. The ellipses generated by both DLS and AML are identical in a sense
that each of the method confirms the result of the other method. As noted in the
previous section, extreme cases were not checked to see how the two methods
differ. Also the methods performance can be better evaluated under uncertainty
by adding a synthetic white gaussian noise to the dataset and look at their
performance with respect to noisy dataset. With regard to the computation
time, both the first and second method are fast owing to their non-iterative
structure while the third method is little slower due to the iterative process
(LMA) involved. But from literature it is evident that the AML produces better
result than DLS even though not on par with ellipses generated by minimizing
the geometric distance. As far as the dataset in this project is considered, both
the DLS and AML performed fair while the least squares method fell short with
regard to partial datasets.

18

6
6.1

Appendix
Ellipse Plotting

The ellipse parameters are a, b, c, d, f, g. The centre is given by


x0 =

cd bf
b2 ac

(6.1)

af bd
b2 ac
The semi-major and semi-minor axes are
s
2(af 2 + cd2 + gb2 2bdf acg)
p
a0 =
(b2 ac)[ (a c)2 + 4b2 (a + c)]
y0 =

s
b0 =

2(af 2 + cd2 + gb2 2bdf acg)


p
(b2 ac)[ (a c)2 + 4b2 (a + c)]

(6.2)

(6.3)

(6.4)

The angle between the major axis and the X-axis is given by
=0

(6.5)

= 0.5

(6.6)

for b = 0 and a < c


for b = 0 and a > c
= 0.5cot1 (

ac
)
2b

for b 6= 0 and a < c


= 0.5 + 0.5cot1 (

ac
)
2b

(6.7)

(6.8)

for b 6= 0 and a > c


Therefore, in parametric form
X(t) = x0 + a0cos(t)cos() b0sin(t)sin()

(6.9)

Y (t) = y0 + a0cos(t)sin() + b0sin(t)sin()

(6.10)

19

6.2

Least squares ellipse fitting based on minimization of


algebraic distance

function book(uv)
m = size(uv, 2);
x = uv(1, 1:end)';
y = uv(2, 1:end)';
S = [x, y, ones(m, 1), x.2, sqrt(2) * x .* y, y.2];
[Q, R] = qr(S);
R11 = R(1:3, 1:3);
R12 = R(1:3, 4:6);
R22 = R(4:6, 4:6);
[U, Sig, V] = svd(R22);
w = V(:, 3);
v = -R11 \ R12 * w;
A
= zeros(2);
A(1)
= w(1);
A([2 3]) = 1 / sqrt(2) * w(2);
A(4)
= w(3);
b
= v(1:2);
c
= v(3);
[Q, D] = eig(A);
Q = Q';
if prod(diag(D)) <= 0
disp('Not an ellipse');
end
t = -0.5 * (A \ b);
c h = t' * A * t + b' * t + c;
z = t;
a = sqrt(-c h / D(1,1));
b = sqrt(-c h / D(2,2));
alpha = atan2(Q(1,2), Q(1,1));
phi=alpha;
t = linspace(0,2*pi,1000);
xaxis=z(1)+a*cos(t)*cos(phi)-b*sin(t)*sin(phi);
yaxis=z(2)+a*cos(t)*sin(phi)+b*sin(t)*cos(phi);
plot(xaxis,yaxis,'LineWidth',2.5);
hold on
plot(uv(1,1:end)', uv(2,1:end)', 'ro')
title('Least Squares Ellipse Fitting-Bookstein Constraint');
legend('Linear LS Fit','Data Points')
end

20

6.3

Numerically stable Direct Least Squares Fitting of Ellipses

clc;
clear all;
I=imread('binary image.jpg');
subplot(1,2,1),imshow(I);title('Original Image')
I=rgb2gray(I);
% figure,imshow(I);title('Binary Image')
the edge=edge(I,'canny');
subplot(1,2,2),imshow(the edge);title('Detected Edge');
[L,M]=find(the edge);
x=L;
y=M;
XY=[L M];
A=fit ellipse(L,M);
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
a = A(1);
b = A(2)/2;
c = A(3);
d = A(4)/2;
f = A(5)/2;
g = A(6);
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
if b==0 && a<c
phi=0;
elseif b==0 && a>c
phi=0.5*pi;
elseif b~=0 && a<c
phi = 0.5*acot((a-c)/(2*b));
elseif b~=0 && a>c
phi= (pi/2)+0.5*acot((a-c)/(2*b));
else
disp('No conditions satisfied, exiting');
end
delta = b2-a*c;
x0 = (c*d - b*f)/delta;
y0 = (a*f - b*d)/delta;
nom = 2*(a*f2 + c*d2 + g*b2 - 2*b*d*f - a*c*g);
s = sqrt((a-c)2 + (4*b2));
a prime = sqrt(nom/(delta* ( s -(a+c))));
b prime = sqrt(nom/(delta* (-s -(a+c))));
%%%%%%%

21

%%%%%%%
t = linspace(0,2*pi,1000);
a p = max(a prime, b prime);
b p = min(a prime, b prime);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xaxis=x0+a p *cos(t)*cos(phi)-b p *sin(t)*sin(phi);
yaxis=y0+a p *cos(t)*sin(phi)+b p *sin(t)*cos(phi);
figure;
subplot(1,2,1),plot(xaxis,yaxis);title('Fitted Ellipse')
subplot(1,2,2),plot(M,L,'ro');title('Data Points')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

22

6.4

Approximate Maximum Likelihood Method

function [params] = fitaml(init,XY)


lambda = 0.01;
lagrange = 1.2;
gamma = 0.0005;
len = length(XY(1,1:end));
F = [0 0 2;0 -1 0;2 0 0];
F(1:3,4:6)=0;
F(4:6,1:6)=0;
I = eye(6);
alpha = 1e-3;

cond continue = true;


condition = false;
t p update=false;
% step-size tolerance
tolDelta = 1e-7;
% cost tolerance
tolCost = 1e-7;
% parameter tolerance
tolTheta = 1e-7;
cost = zeros(1,200);
% allocate space for the parameters of each iteration
t p = zeros(6,200);
% allocate space for the parameter direction of each iteration
delta = zeros(6,200);
% make parameter vector a unit norm vector for numerical stability
init = init / norm(init);
k = 1;
t p(:,k) = init;
delta(:,k) = ones(6,1);
while(cond continue && k<200)
t whi = t p(:,k);
r n = zeros(len+1,1);
for j=1:len
x = XY(1,j);
y = XY(2,j);
u x = [x2,x*y,y2,x,y,1]';
u xdel = [2*x,y,0,1,0,0; 0,x,2*y,0,1,0]';
A n = u x * u x';
cov = eye(2);
B n = u xdel *cov*u xdel';
num = t whi'* A n * t whi;
den = t whi'* B n * t whi;
r n(j) = sqrt(num/den);
X n = (A n/den) - (B n *(num/den2));

23

r ndel = (X n * t whi)/sqrt(num/den);
jacobian(j,:) = r ndel;
end
r n(end) = alpha*((norm(t whi))2/(t whi'*F* t whi));
Y = (eye(6)/(t whi'*F* t whi))-(F*((t whi'* t whi)/(t whi'*F* t whi)2));
r enddel = 2*alpha*Y* t whi;
jacobian merit(1,:) = r enddel;
jacobian total = [jacobian;jacobian merit];
hessian = jacobian total'*jacobian total;
cost(k) = r n'* r n;
if (~condition)
[jacobian,jacobian merit,r n,I,lambda,delta,lagrange,F,...
t p,cost,alpha,XY,len,k] = LMA(jacobian,jacobian merit,r n,I,...
lambda,delta,lagrange,F,...
t p,cost,alpha,XY,len,k);
else
[jacobian,jacobian merit,r n,I,lambda,delta,lagrange,F,...
t p,cost,alpha,XY,len,k,tolDelta,gamma] = LSA(jacobian,...
jacobian merit,r n,I,lambda,delta,lagrange,F,...
t p,cost,alpha,XY,len,k,tolDelta,gamma);
end
% To Check if the latest update overshot the barrier term
if (t p(:,k+1)' * F * t p(:,k+1) <= 0)
condition = true;
lambda = 0;
t p(:,k+1) = t p(:,k);
if (k > 1)
t p(:,k) = t p(:,k-1);
end
1;
% Check for various stopping criteria to end the main loop
elseif (min(norm(t p(:,k+1)-t p(:,k)),...
norm(t p(:,k+1)+t p(:,k))) < tolTheta && t p update)
cond continue = false;
elseif (abs(cost(k) - cost(k+1)) < tolCost && t p update)
cond continue = false;
elseif (norm(delta(:,k+1)) < tolDelta && t p update)
cond continue = false;
end
k = k + 1;
end
1;

24

params = t p(:,k);
params = params / norm(params);

function [jacobian,jacobian merit,r n,I,lambda,delta,lagrange,F,...


t p,cost,alpha,XY,len,k] = LMA(jacobian,jacobian merit,r n,I,lambda,delta,lagrange,F,...
t p,cost,alpha,XY,len,k)
jacobian l = jacobian;
jacobian merit l = jacobian merit;
r n l = r n;
I l = I;
lambda l = lambda;
delta l = delta(k);
lagrange l = lagrange;
F l = F;
t p l = t p(:,k);
cost l = cost(k);
alpha l = alpha;
XY l = XY;
len l = len;
jacob l = [jacobian l;jacobian merit l]'* r n l;
num l = [((jacobian l'*jacobian l) + lambda l* I l)...
(t p l '* F l * t p l)4*(jacobian merit l'*jacobian merit l) ;...
I l -(t p l '* F l * t p l)4* I l];
den l = -[jacob l ; zeros(6,1)];
update 1 = num l\den l;
num 2l = [((jacobian l'*jacobian l) + (lambda l/lagrange l)* I l)...
(t p l '* F l * t p l)4*(jacobian merit l'*jacobian merit l) ;...
I l -(t p l '* F l * t p l)4* I l];
den 2l = -[jacob l ; zeros(6,1)];
update 2 = num 2l\den 2l;
t new 1 = t p l + update 1(1:6);
t new 2 = t p l + update 2(1:6);
cost 1 = 0;
cost 2 = 0;
for z = 1:len l
x l = XY(1,z);
y l = XY(2,z);
u x l = [x l2 x l * y l y l2 x l y l 1]';

25

u xdel l =[2* x l y l 0 1 0 0; 0 x l 2* y l 0 1 0]';

A n l = u x l * u x l ';

B n l = u xdel l * u xdel l';


den l = t new 1 ' * B n l * t new 1;
num l = t new 1 ' * A n l * t new 1;
den 2 l = t new 2 ' * B n l * t new 2;
num 2 l = t new 2 ' * A n l * t new 2;

cost 1 = cost 1 +
cost 2 = cost 2 +

num l/den l ;
num 2 l/den 2 l ;

end

num b l = t new 1 ' * I l * t new 1;


den b l = t new 1 ' * F l * t new 1;
num 2b l = t new 2 ' * I l * t new 2;
den 2b l = t new 2 ' * F l * t new 2;

cost 1 = cost 1 + (alpha l*(num b l/den b l))2;


cost 2 = cost 2 + (alpha l*(num 2b l/den 2b l))2;

if (cost 1 >= cost l && cost 2 >= cost l)


t p updated = false;
cost(k+1) = cost l;
t p(:,k+1) = t p l;
delta(:,k+1) = delta l;
lambda = lambda l * lagrange l;
elseif (cost 2 < cost l)
t p updated = true;
cost(k+1) = cost 2;
t p(:,k+1) = t new 2 / norm(t new 2);
delta(:,k+1) = update 2(1:6)';
lambda = lambda l / lagrange l;
else

26

t p updated = true;
cost(k+1) = cost 1;
t p(:,k+1) = t new 1 / norm(t new 1);
delta(:,k+1) = update 1(1:6)';
lambda = lambda l;
end

function [jacobian,jacobian merit,r n,I,lambda,delta,lagrange,F,...


t p,cost,alpha,XY,len,k,tolDelta,gamma] = LSA(jacobian,...
jacobian merit,r n,I,lambda,delta,lagrange,F,...
t p,cost,alpha,XY,len,k,tolDelta,gamma)
t p ls = t p(:,k);
jacobian ls = jacobian;
jacobian merit ls = jacobian merit;
r n ls = r n;
I ls = I;
lambda ls = lambda;
delta ls = delta(k);
tolDelta ls = tolDelta;
lagrange ls = lagrange;
F ls = F;
%
I ls = struct.I;
cost ls = cost(k);
XY ls = XY;
alpha ls = alpha;
gamma ls = gamma;
len ls = length(XY ls);

jacobian ls = [jacobian ls ; jacobian merit ls]'* r n ls;


den ls = t p ls ' * F ls * t p ls;

one ls = [((jacobian ls'*jacobian ls) + lambda ls* I ls)...


den ls4*(jacobian merit ls'*jacobian merit ls) ;...
I ls -(den ls)4* I ls];
two ls = -[jacobian ls ; zeros(6,1)];
update ls = pinv(one ls,1e-20)* two ls;
update ls = update ls(1:6);

frac = 0.5;
while (true)
t new ls = t p ls + frac*update ls;
delta ls = frac*update ls;
frac = frac / 2 ;

27

cost = 0;
for q = 1:len
x ls = XY ls(1,q);
y ls = XY ls(2,q);
u x ls = [x ls2 x ls * y ls y ls2 x ls y ls 1]';
u xdel ls =[2* x ls y ls 0 1 0 0; 0 x ls 2* y ls 0 1 0]';

A n ls = u x ls * u x ls ';

B n ls = u xdel ls * u xdel ls';


den 2 ls = t new ls' * B n ls * t new ls;
num 2 ls = t new ls' * A n ls * t new ls;

cost = cost +

num 2 ls/den 2 ls ;

end

num m ls = t new ls' * I ls * t new ls;


den ls = t new ls' * F ls * t new ls;

cost = cost + (alpha ls*(num m ls/den ls))2;


if (t new ls'* F ls * t new ls > 0 &&...
(cost < (1-frac*gamma ls)*cost ls)
norm(delta ls) < tolDelta ls)
break;
end
end
t p update = true;
t p(:,k+1) = t new ls / norm(t new ls);
delta(:,k+1) = delta ls;
cost(k+1) = cost;

28

| | ...

References
[1] Walter Gander, Gene H Golub, and Rolf Strebel. Least-squares fitting of
circles and ellipses. BIT Numerical Mathematics, 34(4):558578, 1994.
[2] Radim Halr and Jan Flusser. Numerically stable direct least squares fitting of ellipses. In Proc. 6th International Conference in Central Europe on
Computer Graphics and Visualization. WSCG, volume 98, pages 125132.
Citeseer, 1998.
[3] Andrew Fitzgibbon, Maurizio Pilu, and Robert B Fisher. Direct least square
fitting of ellipses. Pattern Analysis and Machine Intelligence, IEEE Transactions on, 21(5):476480, 1999.
[4] Zygmunt L Szpak, Wojciech Chojnacki, and Anton Van Den Hengel. Guaranteed ellipse fitting with the sampson distance. In Computer VisionECCV
2012, pages 87100. Springer, 2012.
[5] Zhengyou Zhang. Parameter estimation techniques: A tutorial with application to conic fitting. Image and vision Computing, 15(1):5976, 1997.
[6] Zygmunt L. Szpak et al.
Guaranteed ellipse fitting using sampson
distancehttps://sites.google.com/site/szpakz/source-code/guaranteedellipse-fitting-with-sampson-distance, December 2012.

29

S-ar putea să vă placă și