A Practical Review of Uniform B-Splines Kristin Branson A B-spline is a convenient form for representing complicated, smooth curves. A uniform B-spline of order k is a piecewise order k Bezier curve, and is C k -continuous (i.e. the 0 th through (k ) th derivatives are continuous). The form of a B-spline is in general chosen because it is easy to manipulate, not because it is the solution to an optimization problem, like smoothing splines. B-splines are particularly easy to manipulate because they have local control: parameters of the B-spline only affect a small part of the entire spline. This review includes the details of B-splines that I find relevant for practically understanding and implementing B-splines. Bezier Curves A Bezier curve of order k is defined by k control points, and is a degree k polynomial. A Bezier curve is a smooth interpolation of these k control points. A linear Bezier curve (order ) is a linear interpolation of its two control points, p 0 and p : p 0 (s) = ( s)p 0 + sp, defined for 0 s. A quadratic Bezier curve (order 3) is a linear interpolation between the linear interpolation of control points p 0 and p and the linear interpolation of control points p and p : p 0 (s) = ( s)p 0 + sp, p (s) = ( s)p + sp p 0 (s) = ( s)p 0 (s) + sp (s) = ( s)[( s)p 0 + sp ] + s[( s)p + sp ] = ( s) p 0 + s( s)p + s p This method of interpolating between interpolations is called the decasteljau algorithm, and can be generalized to any order k by iterating the equation p ij = ( s)p i,j + sp i+,j. Iterations must be carried out in order of increasing j, from j = 0 to k. The base case is just the control point, p i0 = p i. For a given j, the iterations must be carried out for i = 0,..., k j, in any order. Let s see the decasteljeau algorithm for the popular cubic Bezier curve (order 4), with control points p 0, p, p, and p 3. The linear interpolation: p 0 (s) = ( s)p 0 + sp, p (s0 = ( s)p + sp p (s) = ( s)p + sp 3 The quadratic interpolation: p 0 (s) = ( s)p 0 (s) + sp (s) = ( s) p 0 + s( s)p + s p Similarly, The cubic interpolation: p (s) = ( s) p + s( s)p + s p 3. p 03 (s) = ( s)p 0 (s) + sp (s) = ( s)[( s) p 0 + s( s)p + s p ] + s[( s) p + s( s)p + s p 3 ] = ( s) 3 p 0 + 3s( s) p + 3s ( s)p + s 3 p 3
A Bezier curve can be expressed in terms of basis functions b ij (s) so that k p 0,k (s) = b i,k (s)p i. These basis functions are the Bernstein polynomials, defined recursively by b ij (s) = ( s)b i,j (s) + sb i,j (s) with base cases b 00 (s) =, b ij (s) = 0 if i < 0 or i > j. We can verify this for the cubic Bezier curve: p 03 (s) = b 03 (s)p 0 + b 3 (s)p + b 3 (s)p + b 33 (s)p 3 = [( s)b 0 (s) + sb, (s)]p 0 + [( s)b (s) + sb 0 (s)]p +[( s)b (s) + sb (s)]p + [( s)b 3 (s) + sb (s)]p 3 = ( s)b 0 (s)p 0 + [( s)b (s) + sb 0 (s)]p +[( s)b (s) + sb (s)]p + sb (s)p 3 = ( s)[( s)b 0 (s) + sb, (s)]p 0 + {( s)[( s)b (s) + sb 0 (s)] + s[( s)b 0 (s) + sb, (s)]}p +{( s)[( s)b (s) + sb (s)] + s[( s)b (s) + sb 0 (s)]}p + s[( s)b (s) + sb (s)]p 3 = ( s) b 0 (s)p 0 + [( s) b (s) + s( s)b 0 (s)]p +[s( s)b (s) + s b 0 (s)]p + s b (s)p 3 = ( s) [( s)b 00 (s) + sb,0 (s)]p 0 + {( s) [( s)b 0 (s) + sb 00 (s)] + s( s)[( s)b 00 (s) + sb,0 (s)]}p +{s( s)[( s)b 0 (s) + sb 00 (s)] + s [( s)b 00 (s) + sb,0 (s)]}p + s [( s)b 0 (s) + sb 00 (s)]p 3 = ( s) 3 p 0 + [s( s) + s( s) ]p + [s ( s) + s ( s)]p + s 3 p 3 = ( s) 3 p 0 + 3s( s) p + 3s ( s)p + s 3 p 3 The closed form equation for the basis functions is: This basis has some nice properties: b ij (s) = j! i!(j i)! si ( s) j i. Partition of unity: for all 0 s. j b ij (s) =, b ij (s) 0 Affine Invariance: any linear transformation of p i results in a linear transformation of the Bezier curve. The Bezier curve is tangent to the first and last segments of the control polygon. B-Splines are Piecewise Bezier Curves A B-spline of order k is a piecewise order k Bezier curve, and is C k -continuous. Because of the continuity requirements at each breakpoint between Bezier curve pieces, only one control point of the second Bezier curve piece is free. Thus, L + k control points define L curve pieces for an open spline (only L control points are needed to define L curve pieces of a closed spline). We generalize the Bezier basis functions to the B-spline basis functions: B ij (s) = s i j B i,j (s) + i + j s B i+,j (s) j
with base cases B i (s) = for i = 0,..., L. The equation for a B-spline is then B i3 (s) = { if i u < i + L p 0,k (s) = B i,k (s)p i. For the special case of the cubic B-spline (k = 4), the basis functions are 6 (s i)3 if i s < i + 6 [ 3(s i )3 + 3(s i ) + 3(s i ) + ] if i + s < i + 6 [3(s i )3 6(s i ) + 4] if i + s < i + 3 6 [ (s i 3)]3 if i + 3 s < i + 4 The B-spline basis functions also have some nice properties: Local support: Each curve piece is only a function of the four closest control points. The basis functions are translates of each other: The curve is C k continuous. The basis functions are a partition of unity. Affine invariance. B ik (s) = B 0k (s i). 3 A Convenient Representation Because of the local support property, we can rewrite the equation for a cubic B-spline as p(s) = 6 [ ( (s i)) 3 p i 3 + [3(s i) 3 6(s i) + 4]p i + [ 3(s i) 3 + 3(s i) + 3(s i) + ]p i + (s i) 3 p i ], where i s < i +. A similar computation can be made for any k. This can be written in matrix notation as p i 3 p(s) = [ s s s 3 ]B i p i p i, p i again where i s < i +. In this equation, 6 i3 6 (3i3 + 3i 3i + ) i3 i + 3 i (i + )3 B i = i (3i )(i + ) (3i + 4i) (i + ) i (3i + ) (3i + ) (i + ) 6 We can also include the placement matrix G i : p(s) = [ s s s 3 ]B i G i p, 6. 3
where p is the vector of control points, For an open spline G i [m, n] = p = p 0. p n. { if n = i + m 3 For a closed spline, the placement matrix is similar: { if n = i + m 3 3 modl G i [m, n] = This form is convenient for taking derivatives or integrals w.r.t. s, since the only term in this equation that depends on s is the first row vector. This simplifies calculations like computing the curve tangents and normals, or the curve area, centroid, and variance. 4 Fitting a B-Spline to Data Points Suppose we want to find the set of control points P that best interpolates a set of data points {(s i, x i )} N i=. We use best in the least-squares sense, i.e. we want to find P that minimizes: h[p] = N (x i p(s i ; P)). To solve this problem, we can write the function p(s i ; P) in vector form: It must then be that A s is a N L matrix such that i= P(s; P) = A s P. P(s i ; P) = A s (i, :)P = L B j,k (s i )p j j=0 Thus, entry A s (i, j) = B j,k (s i ). Notice that each row of A s will be nonzero for at most four consecutive entries (exactly four for a closed B-spline). Given this vector notation for P(s i ; P), we can write the optimization as P = min trace[(x A sp) (X A s P)]. P To find the minimizing P, we can take the derivative w.r.t. P and set it to 0, then solve for P. This results in P = (A s A s ) A s X. 5 Matlab Implementations I have implemented a number of B-spline functions in Matlab. To get started with B-splines, I recommend playing with the function input spline.m. This function allows the user to input a closed B-spline on top of an image. The most interesting function called by input spline.m is fit closed b spline.m. This function fits a closed B-spline to the input data points, as described in Section 4. This function can be easily extended to open splines, if one wishes. I have also implemented some basic functions for dealing with cubic, uniform B-splines. The functions evaluate spline.m and evaluate spline curve.m evaluate the B-spline (D and D, respectively) defined by the input control points 4
at the input locations. These both use the convenient representation discussed in Section 3. The B i and G i matrices discussed in section 3 are created using the function setup cubic splines.m, which sets up the matrices for the desired number of curve pieces. The functions evaluate spline prime.m and evaluate normal.m compute the tangents and normals of the B-spline at the input locations, again using the representation of Section 3. The function transform coefs.m takes advantage of the affine invariance of the B-spline basis functions, and applies a translation, scaling, and rotation to an input B-spline. Note that the functions described in this paragraph have an interface that allows multiple sets of control points, defining multiple B-splines, be input and evaluated at once. This is because my implementation is designed for tracking contours using particle filtering. [] References [] S. Buss. 3-D Compute Graphics. Cambridge University Press, New York, 003. [] E. Demidov. An interactive introduction to splines website: http://www.ibiblio.org/e-notes/splines/intro.htm, 004. [3] T. Hastie, R. Tibshirani, and J. Friedman. The Elements of Statistical Learning. Springer Series in Statistics. Springer Verlag, Basel, 00. 5