Due to the good properties of the presented method for circle recognition, I had the idea to use this method to recognize bicycle wheels, which are ideal circles. However, if the bike is positioned so that the plane of its frame makes an acute angle with the viewing direction, then the wheels look like ellipses rather than circles. We thus also need a method of recognizing ellipses. Unfortunatelly we have not succeded with generalizing our method of circle recognition (see Chapter 12) for ellipses. We have tried to use the well-known method of conjugate gradients, but our experiments have shown that this method is not robust: Sometimes it fails when the points to which the ellipse is to be fitted do not lie near an ellipse.
Because an ellipse is defined by only a small number of parameters, namely five, it is possible to use the classical procedure of least squares as described in the next section.
Mathematical Foundation of Ellipse Recognition
An ellipse with axes parallel to the coordinate axes of a Cartesian coordinate system and with the center lying in the origin has the well-known equation
x2 / a2 + y2 / b2 = 1.
However, we need to consider the general case of a shifted and inclined ellipse. We use the general equation of a conic section:
Ax2 + Bxy + Cy2 + Dx + Ey + F = 0.
The expression in the parentheses is approximately proportional to the distance of the point (xi, yi) from the ellipse. It contains six unknown coefficients A, B, C, D, E, and F. However, it is well known that an ellipse is uniquely defined by five parameters. Therefore we divide all terms of Equation 13.1 by A and denote the new coefficients as follows:
B / A = 2k1
C / A = k 2
D / A = 2k3
E / A = 2k4
F / A = k 5
This system of equations is solved by the well-known Gauss method implemented in the method GetEllipseNew using the method Gauss_K. The sums S(xm, yn) are calculated by the method MakeSums2 .
The Project WFellipseBike
The user clicks Open image, then selects the folder and the image. The image appears in the left picture box. Then the user clicks Detect edges. There is a numeric up and down tool for the selection of the threshold for edge detection. However, the preselected value of 20 is good for all images and should not be changed. The program runs automatically. It uses the methods described in Chapter 12 for edge detection and polygonal approximation of the edges. Then it uses the method MakeArcsTwo for subdividing the polygons into arcs. This method is slightly different from the MakeArcs3 method described in Chapter 12.
The method FindEllipsesMode is then called to find the ellipses of the wheels of a bike. The method uses arcs sorted according to the number of points. The sorting is performed by the method SortingArcs writing the indexes of the sorted arcs into the array SortArcs . The arc with the greatest number of points stays in SortArcs[0]. Thus the arcs can be taken in the order of decreasing number of points.
Another Method of Recognizing the Direction
The method just described works when the plane of the frame is at an acute angle to the viewing direction as, for example, in Figure 13-1. In addition, parts of the frame are often obscured by the cyclist’s legs. Therefore we have developed another method DarkSpots which returns the direction of the bike movement: 0 for right and 2 for left. The method makes a box inside of each wheel, calculates histograms of the lightness and sums SumLengths of lengths of almost horizontal polygon edges in these boxes and decides about the drawing direction of the bike. The decision is made by comparing the sum of histogram values below a threshold, i.e. the number of dark pixels in the box, plus 6*SumLenths in both boxes. The greater value indicates the rear wheel and with it also the direction of drawing: if the rear wheel is on the right hand side then the bike moves to left.
When the ellipses of the wheels and the direction of movement are recognized, the ellipses of the wheels and the model of the frame are shown in the right picture box. The message box in the middle of the display shows the message “Bicycle going to left is recognized.” If the user clicks Save result, a text file will be saved on the disk with context corresponding to the following example:
“A bicycle going to left has elliptical wheels.
First: a = 113; b = 173; c = 864; d = 796.
Second: a = 114; b = 173; c = 469; d = 790.”
The notations are as follows: a is the horizontal half-axis of the ellipse, b is its vertical half-axis, c is the x coordinate of the center of the wheel, and d is its y coordinate.