Bsplines
Bsplines
Bsplines
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
k2
-continuous (i.e. the 0
th
through (k 2)
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 nd relevant
for practically understanding and implementing B-splines.
1 Bezier Curves
A Bezier curve of order k is dened by k control points, and is a degree k 1 polynomial. A Bezier curve is a smooth
interpolation of these k control points. A linear Bezier curve (order 2) is a linear interpolation of its two control points, p
0
and p
1
:
p
01
(s) = (1 s)p
0
+sp
1
,
dened for 0 s 1. A quadratic Bezier curve (order 3) is a linear interpolation between the linear interpolation of
control points p
0
and p
1
and the linear interpolation of control points p
1
and p
2
:
p
01
(s) = (1 s)p
0
+sp
1
, p
11
(s) = (1 s)p
1
+sp
2
p
02
(s) = (1 s)p
01
(s) +sp
11
(s)
= (1 s)[(1 s)p
0
+sp
1
] +s[(1 s)p
1
+sp
2
]
= (1 s)
2
p
0
+ 2s(1 s)p
1
+s
2
p
2
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
= (1 s)p
i,j1
+sp
i+1,j1
.
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 1, in any order.
Lets see the deCasteljeau algorithm for the popular cubic Bezier curve (order 4), with control points p
0
, p
1
, p
2
, and
p
3
.
The linear interpolation:
p
01
(s) = (1 s)p
0
+sp
1
, p
11
(s0 = (1 s)p
1
+sp
2
p
21
(s) = (1 s)p
2
+sp
3
The quadratic interpolation:
p
02
(s) = (1 s)p
01
(s) +sp
11
(s)
= (1 s)
2
p
0
+ 2s(1 s)p
1
+s
2
p
2
Similarly,
p
12
(s) = (1 s)
2
p
1
+ 2s(1 s)p
2
+s
2
p
3
.
The cubic interpolation:
p
03
(s) = (1 s)p
02
(s) +sp
12
(s)
= (1 s)[(1 s)
2
p
0
+ 2s(1 s)p
1
+s
2
p
2
] +s[(1 s)
2
p
1
+ 2s(1 s)p
2
+s
2
p
3
]
= (1 s)
3
p
0
+ 3s(1 s)
2
p
1
+ 3s
2
(1 s)p
2
+s
3
p
3
1
A Bezier curve can be expressed in terms of basis functions b
ij
(s) so that
p
0,k1
(s) =
k1
i=0
b
i,k1
(s)p
i
.
These basis functions are the Bernstein polynomials, dened recursively by
b
ij
(s) = (1 s)b
i,j1
(s) +sb
i1,j1
(s)
with base cases
b
00
(s) = 1, 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
13
(s)p
1
+b
23
(s)p
2
+b
33
(s)p
3
= [(1 s)b
02
(s) +sb
1,2
(s)]p
0
+ [(1 s)b
12
(s) +sb
02
(s)]p
1
+[(1 s)b
22
(s) +sb
12
(s)]p
2
+ [(1 s)b
32
(s) +sb
22
(s)]p
3
= (1 s)b
02
(s)p
0
+ [(1 s)b
12
(s) +sb
02
(s)]p
1
+[(1 s)b
22
(s) +sb
12
(s)]p
2
+sb
22
(s)p
3
= (1 s)[(1 s)b
01
(s) +sb
1,1
(s)]p
0
+{(1 s)[(1 s)b
11
(s) +sb
01
(s)] +s[(1 s)b
01
(s) +sb
1,1
(s)]}p
1
+{(1 s)[(1 s)b
21
(s) +sb
11
(s)] +s[(1 s)b
11
(s) +sb
01
(s)]}p
2
+s[(1 s)b
21
(s) +sb
11
(s)]p
3
= (1 s)
2
b
01
(s)p
0
+ [(1 s)
2
b
11
(s) + 2s(1 s)b
01
(s)]p
1
+[2s(1 s)b
11
(s) +s
2
b
01
(s)]p
2
+s
2
b
11
(s)p
3
= (1 s)
2
[(1 s)b
00
(s) +sb
1,0
(s)]p
0
+{(1 s)
2
[(1 s)b
10
(s) +sb
00
(s)] + 2s(1 s)[(1 s)b
00
(s) +sb
1,0
(s)]}p
1
+{2s(1 s)[(1 s)b
10
(s) +sb
00
(s)] +s
2
[(1 s)b
00
(s) +sb
1,0
(s)]}p
2
+s
2
[(1 s)b
10
(s) +sb
00
(s)]p
3
= (1 s)
3
p
0
+ [s(1 s)
2
+ 2s(1 s)
2
]p
1
+ [2s
2
(1 s) +s
2
(1 s)]p
2
+s
3
p
3
= (1 s)
3
p
0
+ 3s(1 s)
2
p
1
+ 3s
2
(1 s)p
2
+s
3
p
3
The closed form equation for the basis functions is:
b
ij
(s) =
j!
i!(j i)!
s
i
(1 s)
ji
.
This basis has some nice properties:
Partition of unity:
j
i=0
b
ij
(s) = 1, b
ij
(s) 0
for all 0 s 1.
Afne Invariance: any linear transformation of p
i
results in a linear transformation of the Bezier curve.
The Bezier curve is tangent to the rst and last segments of the control polygon.
2 B-Splines are Piecewise Bezier Curves
A B-spline of order k is a piecewise order k Bezier curve, and is C
k2
-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 1 control points dene L curve pieces for an open spline (only L control points are needed to dene 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 1
B
i,j1
(s) +
i +j s
j
B
i+1,j1
(s)
2
with base cases
B
i1
(s) =
_
1 if i u < i + 1
0 otherwise
for i = 0, ..., L 1. The equation for a B-spline is then
p
0,k1
(s) =
L1
i=0
B
i,k1
(s)p
i
.
For the special case of the cubic B-spline (k = 4), the basis functions are
B
i3
(s) =
_
_
1
6
(s i)
3
if i s < i + 1
1
6
[3(s i 1)
3
+ 3(s i 1)
2
+ 3(s i 1) + 1] if i + 1 s < i + 2
1
6
[3(s i 2)
3
6(s i 2)
2
+ 4] if i + 2 s < i + 3
1
6
[1 (s i 3)]
3
if i + 3 s < i + 4
0 otherwise
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:
B
ik
(s) = B
0k
(s i).
The curve is C
k2
continuous.
The basis functions are a partition of unity.
Afne invariance.
3 A Convenient Representation
Because of the local support property, we can rewrite the equation for a cubic B-spline as
p(s) =
1
6
_
(1 (s i))
3
p
i3
+ [3(s i)
3
6(s i)
2
+ 4]p
i2
+ [3(s i)
3
+ 3(s i)
2
+ 3(s i) + 1]p
i1
+ (s i)
3
p
i
,
where i s < i + 1. A similar computation can be made for any k. This can be written in matrix notation as
p(s) = [1 s s
2
s
3
]B
i
_
_
p
i3
p
i2
p
i1
p
i
_
_
,
again where i s < i + 1. In this equation,
B
i
=
_
1
6
i
3 1
6
(3i
3
+ 3i
2
3i + 1)
1
2
i
3
i
2
+
2
3
1
i
(i + 1)
3
1
2
i
2
1
2
(3i 1)(i + 1)
1
2
(3i
2
+ 4i)
1
2
(i + 1)
2
1
2
i
1
2
(3i + 1)
1
2
(3i + 2)
1
2
(i + 1)
1
6
1
2
1
2
1
6
_
_
.
We can also include the placement matrix G
i
:
p(s) = [1 s s
2
s
3
]B
i
G
i
p,
3
where p is the vector of control points,
p =
_
_
p
0
.
.
.
p
n
_
_.
For an open spline
G
i
[m, n] =
_
1 if n = i +m3
0 otherwise
For a closed spline, the placement matrix is similar:
G
i
[m, n] =
_
1 if n 1 = i +m3 3 1 modL
0 otherwise
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 rst row vector. This simplies 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 nd the set of control points P that best interpolates a set of data points {(s
i
, x
i
)}
N
i=1
. We use best in
the least-squares sense, i.e. we want to nd P that minimizes:
h[P] =
N
i=1
(x
i
p(s
i
; P))
2
.
To solve this problem, we can write the function p(s
i
; P) in vector form:
P(s; P) = A
s
P.
It must then be that A
s
is a N L matrix such that
P(s
i
; P) = A
s
(i, :)P
=
L1
j=0
B
j,k1
(s
i
)p
j
Thus, entry A
s
(i, j) = B
j1,k1
(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
P
trace[(XA
s
P)
(XA
s
P)].
To nd 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
)
1
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 ts 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, uniformB-splines. The functions evaluate spline.m
and evaluate spline curve.m evaluate the B-spline (1D and 2D, respectively) dened 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 afne invariance of the B-spline basis functions, and applies a transla-
tion, 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, dening multiple B-splines, be input and evaluated at once. This is because my
implementation is designed for tracking contours using particle ltering.
[]
References
[1] S. Buss. 3-D Compute Graphics. Cambridge University Press, New York, 2003.
[2] E. Demidov. An interactive introduction to splines website: http://www.ibiblio.org/e-notes/splines/intro.htm, 2004.
[3] T. Hastie, R. Tibshirani, and J. Friedman. The Elements of Statistical Learning. Springer Series in Statistics. Springer Verlag, Basel,
2001.
5