Professional Documents
Culture Documents
Least-Squares Fitting of Polygons
Least-Squares Fitting of Polygons
Least-Squares Fitting of Polygons
net/publication/303329343
CITATIONS READS
2 9,999
1 author:
Jon Sinnreich
Load Test Consulting, Ltd.
20 PUBLICATIONS 52 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
Derivation of p-y Curves from Lateral Pile Load Test Instrument Data View project
All content following this page was uploaded by Jon Sinnreich on 09 November 2017.
Abstract—Fitting a polygon to a set of given points in the plane is a problem which may arise in certain engi
neering, computer graphics or scientific applications. This paper presents an algorithm which computes a
continuous function closely approximating various polygons, for which the sum of the squares of the distance
to the given set of points is minimized.
1
INTRODUCTION Table 1 below lists some specific combinations of a,
(DEFINITION OF PROBLEM) b and d, and their corresponding geometric results.
Note that the ratios of a and b are fixed in order to
Data regression or curvefitting to continuous create closed curves which approximate the desired
closedform twodimensional shapes has typically geometric shapes. The values of d listed were arrived at
been limited to very smooth curves such as circles and by trialanderror, and may be adjusted slightly to
ellipses [3–5]. A solution for curvefitting an arbitrary “sharpen” the vertices or straighten the line segments
rectangle (that is, not centered on the origin and not of the resulting curve as desired. Polygons with more
aligned with the coordinate system) was required for vertices than an octagon may be defined, but begin to
analyzing certain engineering data. In order to imple approximate a circle to such an extent that attempting
ment a geometric leastsquares solution with to uniquely identify them in data sets with any amount
GaussNewton optimization, a continuous function of scatter may be impractical.
which adequately modeled a polygon was needed. The
parametric function chosen is the hypotrochoid,
which is the trace of a point attached to a small circle PROBLEM SOLUTION
of radius b rolling around the inside of a larger circle of IN PARAMETRIC FORM
radius a, where the trace point is a distance d from the To generalize the solution to a shape of arbitrary
center of the interior circle. Its most familiar incarna size, orientation and location in the plane, the hypo
tion is the “Spirograph” toy which allows the user to trochoid function scale multiplier g, coordinate trans
create interesting geometric patterns using geared cir form Q and centerpoint z are introduced:
cles of various diameters in a planetary gear configura
tion. ⎧x ( ϕ)⎫ ⎧z1⎫ ⎧ h1(ϕ) ⎫
⎨ ⎬ = ⎨ ⎬ + g ⋅ Q ( α) ⎨ ⎬,
The analyses below closely follow the methods ⎩ y ( ϕ)⎭ ⎩z 2⎭ ⎩h2 ( ϕ)⎭
described by Gander [4], and for clarity, similar nota ⎡cos ( α) − sin ( α)⎤
Q=⎢ ⎥.
⎣ sin ( α) cos ( α) ⎦
tion is used wherever possible. Note that procedures
for producing the pseudoinverse of the Jacobian
Typically for symmetric polygons, g is a single
matrix, J+, are not discussed since they are adequately value. Thus, any symmetric polygon is defined by four
addressed in [1] and elsewhere.
parameter values u = ( z1, z 2, g, α): the x and y coordi
The parametric equations of the hypotrochoid nate of its centerpoint z, its hypotrochoid size g and its
function h(ϕ) are defined as: angular orientation relative to the coordinate axes α.
x = h1(ϕ) = ( a − b) cos ϕ + d ⋅ cos a − b ϕ ,
b ( ) Note that g is not a simple distance scalar, but is a
function of a, b and d. If the parameter r is defined as
the scalar distance from the centerpoint of the polygon
y = h2(ϕ) = ( a − b) sin ϕ − d ⋅ sin a − b ϕ .
b ( ) to the midpoint of any of its faces, then the scale of the
polygon is computed as:
1 The article is published in the original. r = g (a − b + d ).
The original motivator for this calculation, the
identification of a rectangle, may be defined by mak
ing g a twovalued parameter g = { g1, g 2} . Thus, each
T
Received November 20, 2014
ISSN 10546618, Pattern Recognition and Image Analysis, 2016, Vol. 26, No. 2, pp. 343–349. © Pleiades Publishing, Ltd., 2016.
344 SINNREICH
∑f
a b d Shape
i
2
= min
3 1 –0.6 Triangle i =1
we simultaneously minimize for v =
{ϕ1, ..., ϕm, z1, z 2, g, α}; that is, find the minimum of the
quadratic function
P ( ϕ1, ..., ϕ m, z1, z 2, g, α)
m
4 1 –0.4 Square = ∑[( x i − x ( ϕi )) + ( yi − y ( ϕi )) ],
2 2
i =1
which is equivalent to solving the nonlinear least
squares problem:
⎧x ⎫ ⎧z ⎫ ⎧h ( ϕ ) ⎫
qi = ⎨ i ⎬ − ⎨ 1⎬ + g ⋅ Q ( α) ⎨ 1 i ⎬ ≈ 0, i = 1, ..., m.
⎩yi ⎭ ⎩z 2⎭ ⎩h2 ( ϕi )⎭
5 1 –0.3 Pentagon
Thus we have 2m nonlinear equations for m + 4
unknowns (or m + 5 if g1 ≠ g2). To compute the Jaco
bian, we need the partial derivatives of qi:
∂ qi ⎧ g ⋅ h ⎫
= δ ij Q ( α ) ⎨ 1 1 ⎬ ,
∂φ i ⎩ g 2 ⋅ h2 ⎭
∂ q i ⎧− 1⎫ ∂ q i ⎧ 0 ⎫ ∂qi ⎧ h ⋅ H (ϕ ) ⎫
= ⎨ ⎬, = ⎨ ⎬, = −Q ( α) ⎨ 1 1 i ⎬ ,
⎩h2 ⋅ H 2 ( ϕi )⎭
6 1 –0.25 Hexagon
∂ z1 ⎩ 0 ⎭ ∂ z 2 ⎩− 1⎭ ∂α
∂ qi ⎧h ( ϕ ) ⎫
= −Q ( α) ⎨ 1 i ⎬ ( g1 = g 2 ) ,
∂g ⎩h2 ( ϕi )⎭
∂ qi ⎧h ( ϕ )⎫
= −Q ( α) ⎨ 1 i ⎬
∂ g1 ⎩ 0 ⎭
∂ qi ⎧ 0 ⎫
8 1 –0.2 Octagon = −Q ( α) ⎨ ⎬ ( g1 ≠ g 2 ) ,
∂g 2 ⎩h2 ( ϕi )⎭
where
⎡− sin α − cos α⎤
Q ( α ) = ⎢ ,
⎣ cos α − sin α ⎥⎦
⎧h1 ( ϕi ) ⎫
⎨ ⎬
of the aligned axes of the rectangle has its own scaling ⎩h2 ( ϕi )⎭
factor, “stretching” a square into a rectangular shape ⎧⎪−(( a − b) sin ( φi ) + d(b −1 ( a − b))sin(b −1 ( a − b) ϕi ))⎫⎪
defined by a total of five parameters =⎨ −1 −1 ⎬
⎪⎩ (( a − b) cos ( φi ) − d(b (a − b))cos(b (a − b) ϕi )) ⎪⎭
u = ( z1, z 2, g1, g 2, α).
and the Kronecker delta notation is employed:
The fitting procedure follows the parametric curve
⎧0, i ≠ j .
fitting solution described by Gander [4]. Given the δ ij = ⎨
measured data points ⎩1, i = j
The Jacobian is constructed as:
⎡ x1 y1 ⎤ ⎡ H A⎤
X =⎢ ⎥ J = ⎢ 1 ⎥,
⎢ ⎥ ⎣H B ⎦ 2
⎣⎢ x m y m ⎦⎥ where
we need to determine the parameters u of the curve H 1 = diag ( g1 ⋅ h1 ( ϕ i ) ⋅ cos ( α ) − g 2 ⋅ h2 ( ϕ i ) ⋅ sin ( α ))
which minimize the geometric distance residual and
f i 2 = min[( xi − x ( ϕi )) + ( yi − y ( ϕi )) ].
2 2 H 2 = diag ( g1 ⋅ h1 ( ϕ i ) ⋅ sin ( α ) + g 2 ⋅ h2 ( ϕ i ) ⋅ cos ( α ))
ϕi are m × m diagonal matrices.
200
150
100
50
−100 Triahgle
Square
−150
−200
θ(degrees)
The standard twostep GaussNewton iterative tan −1 ( yi − z 2 xi − z1 ). Figure 1 illustrates the relation
solution regime is followed, where ship between ϕ and θ for a (3, 1, –0.6) hypotrochoid
1.{t} = J + {q} (triangle) and a (4, 1, –0.4) hypotrochoid (square):
2.{v} := {v} + {t}. However, in lieu of deriving a complicated mapping
As a starting point for the parameters u for initiat function for each polygon (see below), as an initial
ing the iterative solution, a circle with parameters estimate we may assume that ϕi ≈ θi .
{z1, z 2, r} may be fit to the data as described by Gander
[4]. This will give a reasonable starting approximation SAMPLE RESULTS (VALIDATION)
of z. The initial hypotrochoid size parameter g may be
backcalculated from the circle radius r as The following tables present some sample results of
−1 the analysis presented above. Data points are gener
g = r ( a − b + d ) , while the orientation parameter α ated by picking some input values for u. To test the
is either simply assumed to equal zero, or estimated by robustness of the solution, noise in the form of
assuming some data point furthest from the initial esti xi := xi + c ( Rand − 0.5) is added to the data points,
mate of center represents a vertex of the polygon. For where Rand produces random numbers between 0 and
a rectangle, an ellipse of parameters {z1, z 2, r1, r2, α} 1 inclusive and c is a constant. In the figures, the
should be used for the initial estimate, which provides dashed line indicates the starting geometric figure
the additional benefit of a reasonable initial α. from which data points were derived, while the solid
Note that the hypotrochoid function angles in gen line is the hypotrochoid function fitting result to the
eral are not equal to the angles of the data points if noisy data points. Starting estimates for u were gener
expressed in polar form: ϕi ≠ θi = ated by fitting a circle as described in Gander [4].
ANALYTICAL PROBLEM SOLUTION from which aligned angles θ 'i and ϕ 'i are also com
FOR RECTANGLES AND SQUARES puted. Next, the residual vector of distances between
each data point and the hypotrochoid rectangle is
One potential disadvantage of the parametric solu computed:
tion described above is the size of the Jacobian matrix,
which must be inverted, as the number of data points { fi (u)}i =1,..., m = { (h1(ϕ'i ) − z1)2 + (h2(ϕ'i ) − z 2)2
m becomes arbitrarily large. For example, the original .
problem motivating this analysis typically included 50 − (xi' − z1) + (yi' − z 2) }.
2 2
to 100 data points. If the hypotrochoid function input
angles can be approximated separately, rather than Next, construct the (m × 5) Jacobian matrix
adjusted directly with the GaussNewton iteration, ⎡ ∂f1 ( u) ∂f1 ( u) ∂f1 ( u) ∂f1 ( u) ∂f1 ( u) ⎤
then only the five parameters actually defining the ⎢ ∂z ∂z 2 ∂ g1 ∂g 2 ∂α ⎥
geometry of the rectangle need to be solved for. The ⎢ 1
⎥
details of estimating ϕ from g1, g1 and θ for a rectangle ⎢ ⋅ ⋅ ⋅ ⎥
are described in the next section below. To implement J =⎢ ⋅ ⋅ ⋅ ⎥
the solution, the input angles are adjusted by the align ⎢ ⎥
ment angle α: ⎢ ⋅ ⋅ ⋅ ⎥
⎢∂f m ( u) ∂f m ( u) ∂f m ( u) ∂f m ( u) ∂f m ( u)⎥
⎢ ⎥
{x i'} = [Q ] {x i } ⎣ ∂z1 ∂z 2 ∂ g1 ∂g 2 ∂α ⎦
⎝ g1 ⎠
RECTANGULAR HYPOTROCHOID ANGLE Combine the equations from the steps above:
MAPPING FUNCTION ⎛ 2 ⎛g ⎞ ⎞ 2
2
is defined as:
= g 2 [9.16 − 2.40(8 cos 4 ( 4ϕi ) − 8 cos 2 ( 4ϕi ) + 1)]
2
xi = r i cos ( θi ) ≈ g1h1 ( ϕi ) = si cos ( θi ) ,
yi = r isin ( θi ) ≈ g 2h2 ( ϕi ) = s i sin ( θi ) . = g 22[6.76 + 19.20 cos 2(ϕi ) − 19.20 cos 4 (ϕi )] .
Rearrange:
Therefore,
g h (ϕ ) g h (ϕ ) − 6.76 g 22 − (19.20 − 17.64(g12tan 2 ( θi ) + g 22 ))cos 2 ( ϕi )
si = 1 1 i = 2 2 i and
cos ( θi ) sin ( θi ) +(19.20 − 13.44(g12tan 2 ( θi ) + g 22 ))cos 4 ( ϕi )
g 2h2 ( ϕi ) = g1h1 ( ϕi ) tan ( θi ) . + 2.56(g12 tan 2 ( θi ) + g 22 )cos 6 ( ϕi ) = 0 .
By trigonometric identities, Normalizing the leading term:
g1h1 ( ϕi ) = g1 [3 cos ( ϕi ) − 0.4 cos (3ϕi )] (13.44( g12 tan 2 ( θi )) − 5.76 g 22 )
cos 6 ϕ − cos 4 ( ϕi )
= g1[3cos ( ϕi ) − 0.4(4 cos 3 ( ϕi ) − 3cos ( ϕi ))] 2.56(g1 tan ( θi ) + g 2 )
2 2 2
20
10
0
60 70 80 90 100 110 120 130
−10
−20
−30
−40
−50
−60
− 6.76 g 2
2 RECTANGLE SOLUTION COMPARISON
β3i = For comparison purposes, both of the solutions
2.56( g1 tan 2 ( θ i ) + g 22 )
2
described above are used to fit an arbitrarilygenerated
and rectangle shape. Note that if no noise is introduced
into the data, both the parametric and analytical solu
ϕi = cos −1( w). tion yield essentially the exact values of the input
parameters. For a noisy data set, each solution yields
Thus, for an estimate of g1, g1 and data point angle slightly different results, tabulated below.
θi, the corresponding hypotrochoid angle ϕi can be In Fig. 2, the parametric solution result is plotted as
calculated. a solid line, while the analytical solution result is plot