Reconstruction of an ellipse from its raster image
Abstract
Motivated by practical application, we investigate the ways how to obtain a description of an ellipse whose circumscribed rectangle is known exactly and whose coordinates of touching points are known up to a certain inaccuracy. We show the sufficient condition for determination of such an ellipse, derive the implicit and parametric equations and show a proposal of the solution by the modification of the standard regression method.
Full text
International Journal of Pure and Applied Mathematics Volume 101 No. 2 2015, 141-155 ISSN: 1311-8080 (printed version); ISSN: 1314-3395 (on-line version) url: http://www.ijpam.eu doi: http://dx.doi.org/10.12732/ijpam.v101i2.3 P A ijpam.eu RECONSTRUCTION OF AN ELLIPSE FROM ITS RASTER IMAGE Jaroslav Hrdina1§, Jan Pavl´ık2 1,2Brno University of Technology Antoninska 548/1, 601 90 Brno, CZECH REPUBLIC Abstract: Motivated by practical application, we investigate the ways how to obtain a description of an ellipse whose circumscribed rectangle is known exactly and whose coordinates of touching points are known up to a certain inaccuracy. We show the sufficient condition for determination of such an ellipse, derive the implicit and parametric equations and show a proposal of the solution by the modification of the standard regression method. AMS Subject Classification: 65D10, 14H50, 65Z05 Key Words: fitting ellipse, equation of an ellipse, regression method, parametric expression 1. Motivation – The Picture Analysis Suppose there is a circular object taken by a digital camera from a non-frontal view. On a picture, the circle is skewed to form an ellipse-like shape. Its boundary is a curve determined by a number of parameters of the position of the camera with respect to the object, the angle from which the object is taken, the optical and digital parameters of the camera etc. One often needs to analyze the picture automatically to be able to locate the object. Therefore we need to know the exact mathematical description of the curve. If we admit no more distortion than skewing and perspective, this curve is an ellipse. Received: June 20, 2014 c 2015 Academic Publications, Ltd. url: www.acadpubl.eu §Correspondence author
142 J. Hrdina, J. Pavl´ık Fitting of an ellipse through the set of points is one of the classical problems of applied mathematics. In general, there are two approaches. The algebraic one involves the system of five linear equations derived from the knowledge of the coordinates of five points - see also [8, 4, 2]. The geometric approach, uses the least-square method which can be solved numerically. Actual problems solutions mostly prefer applications of geometric approach since it is stable and provides satisfactory results. The regression method is described e.g. in [5]. Using graphical tools one may usually find quite accurately the bounding values in direction of xand yaxes. Hence the problem can be restated as finding an ellipse inscribed into the rectangle, which is also studied in [6]. Moreover, we may use possible knowledge of the position of some base point on the ellipse, namely the touching point. In the paper, we derive the determination of the ellipse by these data. In general, however, we may find only an interval in which the value of the second coordinate of the touching point occurs. Therefore it makes sense to ask for procedures how to obtain the formulas for the ellipse with such a way of description. If we take these properties in account, one can see that an advantageous choice is to take the midpoint of the interval. This procedure provides results of rather good accuracy which is demonstrated on the real-data example, where we compare the procedure with the regression method. Finally we study other possible choices of the base points. In Appendix, we recall some relationships between the equations for ellipse and their marginal points. 2. An Ellipse Inscribed in the Rectangle Let there be an ellipse E0where the values xmax,xmin,ymax,ymin of maximum and minimum in xand ydirection are determined, i.e. the ellipse is inscribed into the rectangle of known lengths. There are still more ellipses with this property. To determine the ellipse entirely, we need to know one of the following: •the second coordinate of some of the touching points, •the intersection of the ellipse with the rectangle’s diagonal. For other point of the ellipse there are general two possible ellipses satisfying the conditions. Suppose yris the y-coordinate of the point Xmax = [xmax, yr] on ellipse (the rightmost, i.e. the x-maximal point). We will show that these data determine the ellipse entirely.
RECONSTRUCTION OF AN ELLIPSE FROM... 143 Firstly, we find the center S= [z1, z2] of the ellipse. It can be easily obtained as a center of the circumscribed rectangle because the ellipse is centrally symmetric. Hence z1=xmax +xmin 2, z2=ymax +ymin 2.(1) Now, for the convenience, we move the center of ellipse to the origin so the marginal values are transformed as follows: x1=xmax −xmin 2, y2=ymax −ymin 2,(2) y1=yr−z2.(3) Clearly x1, y2>0. The new origin-centered ellipse will be denoted by Ec. The general equation of an ellipse (or a hyperbola) is Ax2+By2+ 2Cxy +Dx +Ey +F= 0,(4) where AB 6= 0. The coefficients Dand Edetermine the translation from the origin. Thus any origin-centered ellipse is fully determined by Ax2+By2+ 2Cxy +F= 0,(5) and the equation may be divided by A, so we get the form x2+By2+ 2Cxy +F= 0.(6) Lemma 1. Let the ellipse Ecbe described by (6) with the maximal points R= [x1, y1]and T= [x2, y2]in direction of xand ycoordinates, respectively. Then x1y1=x2y2(7) and the coefficients from (6) satisfy C=−x2 y2 ,(8) B=x2 1 y2 2 ,(9) F=x2 2−x2 1.(10)
144 J. Hrdina, J. Pavl´ık Proof. From (6) we get by implicit differentiation dy dx =−x+Cy By +Cx,dx dy =−By +Cx x+Cy and the points Pand Vwill be obtained by the solution of equations dx dy = 0, dy dx = 0, respectively. Hence x2+Cy2= 0 and By1+Cx1= 0. Assume the values y2and x1are nonzero (otherwise the situation is trivial). We get C=−x2 y2 , B =−Cx1 y1 =x1x2 y1y2 . Since P∈ Ec, it satisfies the equation (6), hence by substitution we get: x2 1+x1x2 y1y2 y2 1−2x2 y2 x1y1+F= 0, hence F=x1x2y1 y2−x2 1. Since V∈ Ec, we may put it in the equation (6) to get x2 2+x1x2y2 y1−2x1x2y2 y1 +x1x2y1 y2−x2 1= 0. This can be rewritten as (x2y1−x1y2)(x1y1−x2y2) = 0. In non-degenerate case, i.e. when the smaller of semiaxis is non-zero, we have strict inequalities |x2|>|x1|,|y1|>|y2|. Then |x2y1|>|x1y2|and |x2y1| 6= |x1y2|, hence x2y1−x1y26= 0. Therefore x1y1−x2y2must be equal to zero. In degenerate case, the ellipse collapses into a segment line, hence the maximal points in both directions coincide. Therefore x1y1=x2y2holds in both cases. Now by substitution x2=x1y1 y2 in the equations for B, C, F we get the required relationships. As a consequence, we get that Bis positive, hence generally AB > 0. Theorem 1. Duality property Let the ellipse Ecbe given by (6) with A, B > 0. Let γ=qA B(this will be called a duality ratio for Ec). Given a point X= [x0, y0], then the point X′= [1 γy0, γx0](which will be called the dual point for X) satisfies: X∈ Ec⇐⇒ X′∈ Ec
RECONSTRUCTION OF AN ELLIPSE FROM... 145 Figure 1: Duality property on the ellipse. Proof. Since X′′ = [1 γγx0, γ 1 γy0] = [x0, y0] = X, the duality is an involution and it suffices to prove just one direction. Let X∈ Ec. Then Ax2 0+By2 0+ 2Cx0y0+F= 0. Then A(1 γy0)2+B(γx0)2+ 2C1 γy0γx0+F=A(rB Ay0)2+B(rA Bx0)2 + 2Cy0x0+F =By2 0+Ax2 0+ 2Cy0x0+F = 0 hence X′∈ Ec. From now on, let ρbe an origin-centered basic (parallel with coordinate axes) rectangle with lengths 2αand 2βof horizontal and vertical, respectively, sides and Ebe its inscribed ellipse (clearly origin-centered) with the marginal points T= [ξ, β], B,Rand L(top, bottom, right and left, respectively). Hence x1=α, y2=β, x2=ξand, due to (6), the ellipse can be described by the equation β2x2+α2y2−2ξβxy +β2(ξ2−α2) = 0.(11)
146 J. Hrdina, J. Pavl´ık Then the property 1 can be applied on the marginal points of the ellipse as follows: due to 1 γ=qA B=s1 x2 1 y2 2 =qβ2 α2=β α. Hence γis the tangent of the diagonal with the positive slope. Therefore two points are mutually dual if they have common projection points on the rectangle’s diagonal (see Figure 1). One can easily see that the marginal points Tand Rare dual. Remark 1. Using the theory of conjugate diameters (see e.g. [7]), one can see that the diagonal chords of the rectangle are exactly the conjugate diameters and then the statement follows from the well-known fact that the midpoints of the parallel chords lie on the diameter, which is conjugate to the longest of these chords being the diameter. 2.0.1. Expression of the Marginal Values Since Ecan be described by (6), using the formulas from the Lemma 1 and introducing the variable Q=C2−B F(used analogously in the Appendix) one can easily get the marginal values in the form x1=sB Q, y1=|C| √BQ, x2=|C| √Q, y2=r1 Q.(12) 3. Vagueness of the Touching Point While the previous section was based on the knowledge of the exact values, now we will describe the more usual condition. It is usually not possible to read from the bitmap the exact value of the x-coordinate for the topmost (or y-coordinate for the rightmost) point. Let us focus on the topmost point. What one can get from the raster image is only a pixel range where it occurs (referred to as an occurrence range)– this is given by the resolution of the bitmap image. While the height of the range may be considered negligible, the problem of its width still needs to be solved. However, whatever the process of rasterizing of the ellipse was, the bounds of this range arose from the ellipse by replacement of a certain curve segment by a set of neighboring pixels. Hence we may expect that exact ellipse (after a slight change of the size) intersects the two-dimensional pixel range in the bounding values of x-coordinate. Though the original ellipse may not have satisfy this exactly, such a change will cause only minor differences. In fact, this change is negligible since it is certainly
RECONSTRUCTION OF AN ELLIPSE FROM... 147 Figure 2: Fitting the ellipse into the raster image. bellow the resolution of the image. On the Figure 2, the thin original ellipse and its raster image are depicted. In that case of scale, the rightmost and the leftmost ranges are intersected nearby the bounding values. Now let the situation be the following: Let the topmost point T= [ξ, β] of Ebe somewhere between the points T0= [x0, β] and T1= [x1, β] (the bounding points of the range). The segment line connecting T0and T1will be identified with the interval hx0, x1iof its x-values and the same convention will be used for bottom side and for the vertical sides and y-coordinate. Due to the previous discussion, we may consider the best solution being an ellipse which can be rescaled so it intersects the points T0and T1. Hence we are looking for an ellipse E′satisfying: •T0, T1∈ E′, • E′is origin-centered, •the basic circumscribed rectangle has the ratio γ=β αof the lengths of vertical and horizontal side. Such an ellipse can be obtained as follows. Using the duality one may find the points R0=T′ 0,R1=T′ 1with the duality ratio given by γ. To find the coefficients B, C, F for the equation (6) one solves the corresponding system of 3 linear equations for some triple of the points. The solution is B=α2 β2, C=−x0+x1 2β,F=x0x1−α2. Since here A= 1, the ellipse E′has the following
148 J. Hrdina, J. Pavl´ık equation: β2x2+α2y2−(x0+x1)βxy +β2(x0x1−α2) = 0.(13) One may easily check that it is satisfied by the points T0, T1, R0, R1. Observe that if x0=x1, we get exactly the equation (11), since T0=T1becomes the topmost point T. Now, from E′we can derive the description of the ellipse E. We need to change the scale in order to fit the ellipse into the rectangle ρ. But all we need to know is the x-value of the topmost point, i.e. the value of ξ. Due to the central symmetry, the change of the scale can be managed by translation of the points along the rays of the form y=δx. A point [x, y] is then mapped onto [x′, y′] where x′=y′x y. Let [xv, yv] be the topmost point of the ellipse E′. It will be translated to the point which will be the topmost point of E, i.e. the point T= [ξ, β]. Therefore ξ=βxv yv. From the paragraph 2.0.1 we get the coordinates xv=|C| √Q,yv=q1 Q. The nonzero coefficient Qis not needed because we will use only the ratio r=xv yv= |C| √Q q1 Q =|C|. Hence r=|−x0+x1 2β| 1=x0+x1 2βand we get finally ξ=βr =x0+x1 2. The result can be stated as Observation 1. The best ellipse (in sense of the discussion above) inscribed in the basic origin-centered rectangle has the topmost point in the middle of the occurrence range. 3.1. Regression Method In general, the situation can be even worse. Since each of the four marginal points yields an occurrence range, we have four ways how to describe the ellipse which may not yield the same results. There are more ways how to handle this situation - one can interpolate either the initial values or the results or to start over using a regression method. Due to the central symmetry and the duality, we can map all the 8 bounding points onto the same, say the upper, side. Then there are several ”reasonable” ways how to assign one particular point for all these 8 points, namely we can take their (arithmetical) average, which is exactly the same as the average of the mapped midpoints for each pair of bounding points. Other options are to make the intersection or the union of all the mapped occurrence ranges and to take their center.
RECONSTRUCTION OF AN ELLIPSE FROM... 149 If we are not satisfied with the interpolation of the initial values, we may use the regression method. The problem stands as: To fit an ellipse in the set of the given 8 points. It can be solved by the standard least square method. However, it involves equations which are not directly solvable so we use Gauss- Newton algorithm for numerical approximations. The result will be obtained in the parametric form, which includes the expression of the lengths of semiaxes and the tilt angle. After its application, the semiaxes of the resulting ellipse will be transformed in order to get the ellipse inscribed in the rectangle. It is possible to do it accurately only if the ellipse remains origin-centered, but the error becomes very small for high number of iterations. Hence it makes no problem to recenter the ellipse to the origin. We demonstrate how the method works for rectangle ρwith the values α=1 2√19 β=3 2 and the bounding points: T0:x0= 1.531375673 y0=β T1:x1= 1.343375673 y1=β R0:x2=α y2= 1.063965022 R1:x3=α y3= 0.9245745478 B0:x4=−1.343375673 y4=−β B1:x5=−1.546375673 y5=−β L0:x6=−α y6=−0.9245745478 L1:x7=−α y7=−1.073965022 Let us show the samples from the first 600 steps of Gauss-Newton method. For comparison, by thick dash line we draw an ellipse of the equation 36x2+ 76y2−40√3xy−96 = 0 which is an algebraically calculated ellipse with the topmost point T= [5 6√3, β], semiaxes √6≈2.449489743 and 1 and the tilt angle π 6≈0.5235987758. The right image shows the final iteration (thin solid line), recentered to the origin (dash-and-dot line) and the algebraically calculated ellipse (solid thick line). An interpretation of the results obtained by least square method is based on the modification of the resulting ellipses. As discussed above, the obtained ellipse intersects the rectangle so that the points with the maximal value of a coordinate have the value of the other coordinate between the values of the
156