Skip to main content

Bezier Fitting

The goal is to fit a sequence of cubic Bezier curve over a sequence of ordered points. We use a classic Bezier fitting technique developed by Philip Schneider. A Bezier curve QQ of order nn is defined by its control points viv_i: Q(u)=โˆ‘i=0nviBin(u)Q(u) = \sum_{i=0}^n v_i B_i^n(u), where Bin(u)B_i^n(u) are the Bernstein polynomials of degree nn, Bin(u)=(ni)ui(1โˆ’u)nโˆ’iB_i^n(u) = {n \choose i} u^i (1-u)^{n-i}. In our case, we choose n=3n=3, i.e. the cubic Bezier curves with four control points.

We need to minimize the distance from a Bezier curve to a set SS of nn points. Formally, we define an objective function F=โˆ‘j=1nโˆฃโˆฃpiโˆ’Q(ui)โˆฃโˆฃ2F = \sum_{j=1}^n ||p_i - Q(u_i)||^2, where uiu_i is the parameter of the closest point on cubic Bezier curve QQ to the point pip_i.

Let t1t_1 and t2t_2 be the tangents at the start and end of the curve. Then write the second and the third control points in terms of the tangents, v1=ฮฑ1t1+v0v_1 = \alpha_1 t_1 + v_0 and v2=ฮฑ2t2+v3v_2 = \alpha_2 t_2 + v_3 where ฮฑ1\alpha_1 and ฮฑ2\alpha_2 are the distances to the nearest end points in the tangent direction. Therefore, the function FF can be minimized by finding the right parameters ฮฑ1\alpha_1 and ฮฑ2\alpha_2. In particular, we set the derivatives of FF with respect to ฮฑ1\alpha_1 and ฮฑ2\alpha_2, and set them to zero:

โˆ‚Fโˆ‚ฮฑ1=โˆ‘i=1n2(piโˆ’Q(ui))โ‹…t1B13(ui)=0\frac{\partial F}{\partial \alpha_1} = \sum_{i=1}^n 2\left(p_i - Q(u_i)\right) \cdot t_1 B_1^3(u_i) = 0

โˆ‚Fโˆ‚ฮฑ2=โˆ‘i=1n2(piโˆ’Q(ui))โ‹…t2B23(ui)=0\frac{\partial F}{\partial \alpha_2} = \sum_{i=1}^n 2\left(p_i - Q(u_i)\right) \cdot t_2 B_2^3(u_i) = 0

This linear system of equations can be solved via matrix inversion. Let Ai,j=tjBj3(u1)A_{i,j} = t_j B_{j}^3(u_1). Then,

(โˆ‘i=1nAi,12)ฮฑ1+(โˆ‘i=1nAi,1โ‹…Ai,2)ฮฑ2=โˆ‘i=1n(piโˆ’(v0B03(ui)+v0B13(u1)+v3B23(ui)+v3B33(ui)))โ‹…Ai,1\left(\sum_{i=1}^n A_{i,1}^2\right) \alpha_1 + \left(\sum_{i=1}^n A_{i,1} \cdot A_{i, 2}\right) \alpha_2 = \sum_{i=1}^n \left(p_i - \left( v_0 B_0^3(u_i) + v_0 B_1^3(u_1) + v_3 B_2^3(u_i) + v_3 B_3^3(u_i) \right)\right) \cdot A_{i,1}

(โˆ‘i=1nAi,22)ฮฑ2+(โˆ‘i=1nAi,1โ‹…Ai,2)ฮฑ1=โˆ‘i=1n(piโˆ’(v0B03(ui)+v0B13(u1)+v3B23(ui)+v3B33(ui)))โ‹…Ai,2\left(\sum_{i=1}^n A_{i,2}^2\right) \alpha_2 + \left(\sum_{i=1}^n A_{i,1} \cdot A_{i, 2}\right) \alpha_1 = \sum_{i=1}^n \left(p_i - \left( v_0 B_0^3(u_i) + v_0 B_1^3(u_1) + v_3 B_2^3(u_i) + v_3 B_3^3(u_i) \right)\right) \cdot A_{i,2}

Condensing the equations into a matrix,

[c1,1c1,2c2,1c2,2][ฮฑ1ฮฑ2]=[x1x2]\begin{bmatrix} c_{1,1} & c_{1,2} \\ c_{2,1} & c_{2,2} \end{bmatrix} \begin{bmatrix}\alpha_1 \\ \alpha_2 \end{bmatrix} = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}

Then solving these two equations allow us to obtain a good approximation.

There is still the problem of finding the parameter of the closest point for each point pip_i. We can use Newton-Raphson iteration to get a better uu: uโ†uโˆ’f(u)fโ€ฒ(u)u \leftarrow u - \frac{f(u)}{f'(u)} where f=(piโˆ’Q(u))โ‹…Qโ€ฒ(u)f = (p_i - Q(u)) \cdot Q'(u) and fโ€ฒ=Qโ€ฒ(u)โ‹…Qโ€ฒ(u)+(piโˆ’Q(u))โ‹…Qโ€ฒโ€ฒ(u)f' = Q'(u) \cdot Q'(u) + (p_i - Q(u)) \cdot Q''(u).