Fidig the circle that best fits a set of poits L. MAISONOBE October 5 th 007 Cotets 1 Itroductio Solvig the problem.1 Priciples............................... Iitializatio............................. 3.3 Issues................................ 4.4 Iterative improvemet........................ 4.4.1 Problem statemet..................... 4.4. Direct solvig........................ 5.4.3 Leveberg-Marquardt................... 5.4.4 Cojugate gradiet..................... 5 3 Numerical example 7 A Algorithms 10 luc@spaceroots.org 1
1 Itroductio We cosider the followig D problem: give a set of poits P i (x i,y i ) (i = 1...) fid the ceter C(x c,y c ) ad the radius r of the circle that pass closest to all the poits. Figure 1: closest circle problem y P 5 P1 r C P 4 P P 3 x For the latest versio of this documet, please check the dowloads page of the spaceroots site at http://www.spaceroots.org/dowloads.html. The paper ca be browsed o-lie or retrieved as a PDF, compressed PostScript or LaTeX source file. A implemetatio i the Java laguage of the algorithms preseted here is also available i source form as a stad-aloe file at http:// www.spaceroots.org/documets/circle/circlefitter.java. Solvig the problem.1 Priciples The uderlyig priciples of the proposed fittig method are to first compute a iitial guess by averagig all the circles that ca be built usig all triplets of o-
aliged poits, ad the to iteratively reduce the distace betwee the circle ad the complete set of poits usig a miimizatio method.. Iitializatio For ay give triplet of o-aliged poits, there is a sigle circle passig through all three poits: the triagle circumcircle. We will use this property to build a iitial circle. Let P i (x i,y i ), P j (x j,y j ) ad P k (x k,y k ) be three poits. The triagle circumceter is at the itersectio of the perpedicular bisectors of the triagle sides. Equatio (1) holds as the circumceter belogs to the perpedicular bisector of side (P i,p j ) ad equatio () holds it also belogs to the perpedicular bisector of side (P j,p k ): (1) () (3) x Ci, j,k = (x i + x j ) + α i, j (y j y i ) y Ci, j,k = (y i + y j ) α i, j (x j x i ) x Ci, j,k = (x j + x k ) + α j,k (y k y j ) y Ci, j,k = (y j + y k ) α j,k (x k x j ) Solvig this set of liear equatios is straightforward: α i, j = (x k x i )(x k x j ) + (y k y i )(y k y j ) α j,k = (x k x i )(x j x i ) + (y k y i )(y j y i ) where = (x k x j )(y j y i ) (x j x i )(y k y j ) (4) Hece the circumceter coordiates are: x Ci, j,k = (y k y j )(xi + y i ) + (y i y k )(x j + y j ) + (y j y i )(xk + y k ) y Ci, j,k = (x k x j )(xi + y i ) + (x i x k )(x j + y j ) + (x j x i )(xk + y k ) These coordiates ca be computed as log as the determiat is o-ull, which meas the three poits are ot aliged. Sice the methos iteded to be geeral, we do ot make ay assumptios o the poits. Some of them may have small (or eve large) errors, some may be aliged with other oes, some may appear several times i the set. We caot reliably select oe triplet amog the other oes. We build the iitial guess of the fittig circle by averagig the coordiates of the circumceters of all triplets of o aliged poits. This etire iitializatio process is implemete algorithm 1 (page 10). 3
.3 Issues The iitializatio process described above may be iadequate i some cases. If the umber of poits is very large, the umber of triplets will be overwhelmigly large as it grows i 3. I this case, oly a subset of the complete triplets set must be used. Selectig the subset implies kowig the distributio of the poits, i order to avoid usig oly poits i the same area for iitializatio. If the poits sample cotais really far outliers i additio to regular poits, the iitial ceter may be offset by a large amout. This is due to the fact the mea is ot a robust statistic. This ca be circumveted by either usig a media rather tha a mea or by droppig outliers (either before or after circumceter determiatio). If the poits distributio is kow to cover exactly oe circle with a cosistet step betwee the poits ad o outliers, a much simpler iitializatio process is simply to use the mea of the x ad y coordiates for all poits..4 Iterative improvemet.4.1 Problem statemet Oce we have a iitial guess for the circle ceter, we wat to improve it for some defiitio of best fit agaist the poits set. Usig a least squares estimator based o the euclidea distace betwee the poits ad the circle is a commo choice. We try to miimize the cost fuctio J: J = ( r) where = (x i x) + (y i y) i=1 is the euclidea distace betwee the poit P i (x i,y i ) ad the circle ceter C(x,y) ad r is the circle radius. For a give circle ceter, we compute the optimum circle radius ˆr by solvig: J (5) r = 0 ˆr = i=1 r=ˆr This meas that both ad ˆr ca be cosidered as fuctios of the circle ceter coordiates x ad y which from ow o will be the free parameters of our model. Usig the choice r = ˆr, the cost fuctio ca be rewritte: (6) J = i=1 ( ˆr) = i=1 d i ( i=1 ) from this expressio we ca compute the gradiet of the cost fuctio with respect to the free parameters: J x = (x x i )( ˆr) (7) J J y = (y y i )( ˆr) 4
.4. Direct solvig Equatio (7) ca easily be solved aalytically. However experiece shows that eve for small errors with the iitial guess, this leads to a solutio C(x,y) that is very far from the validity domai of the partial derivatives values. This sesitivity to the iitial guess is a commo issue with GAUSS-NEWTON based least squares solvers. Iteratig as i a fixed poit method does ot help either, iteratios diverge oce we are too far. So we have to refrai from usig this easy but wrog solutio ad use a more robust approach..4.3 Leveberg-Marquardt The LEVENBERG-MARQUARDT methos a combiatio of the GAUSS-NEWTON ad steepest descet methods. It beefits from the stregth of both methods ad is both robust eve for startig poits far from the solutio ad efficiet ear the solutio. It is a sure bet as highly recommeded. This methos however quite complex to implemet, so it should be used oly if a validatemplemetatio of the methos easily available. The MINPACK implemetatio 1 i fortra is widely used. Our ow Matissa Java library also provides a Java implemetatio based o the MINPACK oe as of versio 6.0. May other implemetatios are available i various laguages. For cost fuctio J = i=1 ( ˆr), the LEVENBERG-MARQUARDT method eeds the jacobia matrix of the deviatios ˆr. The two elemets of row i of this matrix is give by equatio (8): ( 1 k=0 d ) k x ( ˆr) = ( d i 1 k=0 d ) k y (8) = x x i y y i 1 1 k=0 k=0 x x k d k y y k d k This solver is far more efficiet tha the simple oe we will preset i the followig sectio (by several orders of magitude). The drawbacks are the implemetatio complexity..4.4 Cojugate gradiet If o LEVENBERG-MARQUARDT based solver are available, we ca still fid the values of the free parameters that miimize the cost fuctio J usig the simpler 1 http://www.etlib.org/mipack/lmder.f http://www.spaceroots.org/software/matissa/idex.html 5
cojugate gradiet method with POLAK ad RIBIÈRE factor. This will be less efficiet tha usig LEVENBERG-MARQUARDT especially whe the poits are far from a real circle, but is very simple to do. The cojugate gradiet methos also a iterative oe, usig two embedded loops. The outer loop computes a ew search directio by combiig the cost fuctio gradiet at the curret step ad the previous search directio. The ier loop roughly miimizes the cost fuctio i the search directio. This methos very easy to implemet ad allows to have a self-cotaied algorithm. The rough miimizatio of the cost fuctio alog the search directio ca be doe by performig a few steps of a NEWTON solver o the derivative of the cost fuctio J(x + λu,y + λv) with respect to the step λ. The followig equatios are the exact derivatives used for this rough ier miimizatio (we use J u,v (λ) = J(x + λu,y + λv), u,v (λ) = (x + λu,y + λv) ad ˆr u,v (λ) = ˆr(x + λu,y + λv) as a way to simplify the equatio): [ dj u,v (λ) (x + λu xi )u + (y + λv y i )v ][ u,v (λ) ˆr u,v (λ) ] = dλ u,v (λ) d J u,v (λ) dλ = (u + v u,v (λ) ˆr u,v (λ) ) u,v (λ) J u,v (λ) = u,v (λ) 1 ( ( +ˆr u,v (λ) u,v (λ) ) ) (x + λu x i )u + (y + λv y i )v u,v (λ) ( (x + λu xi )u + (y + λv y i )v ) 3 u,v (λ) hece (9) ad (10) dj u,v (λ) dλ d J u,v (λ) dλ = λ=0 = (u + v ) λ=0 + ˆr [ (x xi )u + (y y i )v ][ ˆr ] ( ˆr (x x i )u + (y y i )v ( (x xi )u + (y y i )v ) d 3 i ) 6
The precedig equatios are use a traditioal cojugate gradiet method, assumig x ad y have bee iitialized, as show i algorithm (page 11). The parameters of the algorithms are the maximal umber of iteratios i max, ad the two covergece thresholds ε ier ad ε outer. Their values deped o the problem at had (umber of poits, expected error at each poits, required accuracy). Fidig the values for a give problem is a matter of trial ad errors. As always with the cojugate gradiet method, there is o real eed to have a accurate miimum alog the search directio, hece ε ier may be quite large (I used values betwee 0.0 ad 0.1). It is eve possible to remove completely the covergece check ad replace it with a arbitrary small umber of NEWTON steps. 3 Numerical example This sectio shows a example applicatio of the algorithms describe this ote. It is based o a simple case with oly five poits ad a order of magitude of about 1% of the circle radius for the errors i the poits locatios. We start with the followig set of five poits: poit x y P 1 30 68 P 50-6 P 3 110-0 P 4 35 15 P 5 45 97 Sice there are five poits i the set, there are te differet triplets, oe of them have aliged poits: circumceter x y circumceter x y C 1,,3 93.650 45.500 C 1,4,5 103.768 48.3 C 1,,4 103.704 48.17 C,3,4 9.400 40.14 C 1,,5 103.753 48.30 C,3,5 94.178 47.765 C 1,3,4 95.81 47.473 C,4,5 103.70 48.9 C 1,3,5 99.8 50.571 C 3,4,5 96.580 49.100 we observe that with this data set, four circumceters (C 1,,4, C 1,,5, C 1,4,5 ad C,4,5 ) are very close to each other. This is oly a coicidece. This explais why few poits show up i figure : the four rightmost poits are almost superimposed. Averagig these circumceters gives the iitial value for the circle ceter: C iit (98.680,47.345) The iitial value of the cost fuctio is 13.407. Usig the LEVENBERG-MARQUARDT method with very striget covergece thresholds (10 10 for all tolerace settigs: relative cost, relative parameters ad 7
Figure : iitializatio P 5 y P1 circumceters C iit C P 4 x P P 3 orthogoality), covergece is reached after oly 4 cost fuctio evaluatios ad 4 jacobias evaluatios. Usig the cojugate gradiet method with a loose covergece settig for the ier loop ad a tight oe for the outer loop (ε ier = 0.1 ad ε outer = 10 1 ), covergece is reached after 7 iteratios. Both methods give the same results 3. The miimal value foud for the cost fuctio is 3.17. The parameters for the best fittig circle ad the correspodig residuals are: d 1 r = 0.963 { C(96.076,48.135) d r = 1.19 d 3 r = 0.417 r = 69.960 d 4 r = 0.475 d 5 r = 0.75 This example is oly oe example, o coclusios should be draw from the similar results ateratio umbers. Some key features that deped o the problems ad are ot addresse this paper are the followig oes: umber of poits: this example uses 5 poits, other may have hudreds of poits, error level: errors rage to about 1% of the circle radius, I have ecoutered real life applicatios were errors were aroud 0.001%, other were errors were about 30%, 3 a previous versio of this paper foud slight differeces betwee the results ad a large differece i the umber of iteratios, this was due to a implemetatio bug i the cojugate gradiet method 8
error distributio is homogeeous: but i some applicatio errors may be highly correlated (deted or squizzed circles), circle coverage: the example poits all belog to the same left half of the circle, i some cases poits will be regularly distributed over the circle, a some cases there will be some strage distributio like 3.5 turs. 9
A Algorithms σ x 0 σ y 0 q 0 loop for i = 1 to i = loop for j = i + 1 to j = 1 loop for k = j + 1 to k = compute usig equatio (3) if > ε the // we kow the poits are ot aliged compute C i, j,k usig equatio (4) σ x σ x + x Ci, j,k σ y σ y + y Ci, j,k q q + 1 ef ed loop ed loop ed loop if q = 0 the error all poits are aliged ef retur circle ceter C(σ x /q,σ y /q) Algorithm 1: iitializatio 10
compute ˆr usig equatio (5) compute J usig equatio (6) compute J usig equatio (7) if J < ε 1 or J < ε the retur // we cosider we already have the miimum ef J prev J J prev J u prev 0 loop from i = 1 to i = i max // search directio u J if i > 1 the β J T ( J J prev )/ J prev // Polak-Ribiere coefficiet u u + β u prev ef J prev J u prev u // rough miimizatio alog the search directio (a few Newto steps) loop J ier J compute dj/dλ usig equatio (9) compute d J/dλ usig equatio (10) λ (dj/dλ)/(d J/dλ ) C C + λ u compute ˆr usig equatio (5) compute J usig equatio (6) compute J usig equatio (7) while i i max ad J J ier /J ε ier if J J prev /J < ε outer retur // covergece reached ef ed loop error uable to coverge i i max iteratios Algorithm : cojugate gradiet 11