Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines...

35
Splines Vida Movahedi January 2007

Transcript of Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines...

Page 1: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Splines

Vida MovahediJanuary 2007

Page 2: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Outline

• Piecewise polynomials• Splines• Two approaches• B-splines• Multivariate functions• Examples

Page 3: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

What problem splines solve?

• In the simplest situation, one is given points (t i, yi) and is looking for a piecewise polynomial function f that satisfies f(ti)=yi , all i, more or less.– Exact fit Interpolation– Approximate fit least squares approximation or

smoothing splines

• Or we have a complex function g that we want to approximate with a piecewise polynomial function f that satisfies f(ti)=g(ti) , all i, more or less.

Page 4: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Linear Interpolation Cubic Interpolation Cubic Smoothing

Page 5: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Why polynomials?

• Easy evaluation, differentiation, and integration• If done properly, the work needed for data fitting

grows only linearly with the number of data points.

Page 6: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Definition of Polynomial of order n

• Note that a polynomial of order n has degree<n• The set of all polynomials of order n forms a

linear space <n: linear space of polynomials of order n

n

1

1-jj

1-nn21 xaxa x a a p(x)

j

Page 7: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Why piecewise polynomials?

• Jackson’s Theorem: If g has r continuous derivatives on [a..b] and n> r+1, then

• The only way to make the error small is to make (b-a)/ (n-1) small partitioning [a..b] using piecewise polynomial approximation.

• It is usually much more efficient to make (b-a) small than to increase n. This is the justification for piecewise polynomial approximation.

nj

n

1

1-jj

1-nn21 xaxa x a a p(x)

hyxbayxygxghgw

rn

abgw

n

abconstgdistrnbaCg r

r

rnr

||],..[,|:)()(|max:);(

))1(2

;(.1

),()1(]..[ )()(

Page 8: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Why not higher order polynomials?

• We expect the error between the function g and the polynomial approximation pn on n sites to decrease when n increases. If the sites are uniformly spaced, it can be shown that this is not true and the interpolation error increases with n for some examples.

• It is shown that choosing the sites as the zeros of the Chebyshev polynomial of degree n can lead to much decreasing interpolation error with increasing n. For some examples, the error, while decreasing, decreases far too slowly.

Page 9: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

What is a spline?

• An interval [a..b] is subdivided into sufficiently small intervals [j.. j+1], with a=1<…<l+1=b,

• On each such interval, a polynomial pj of relatively low degree can provide a good approximation to g.

• This can even be done in such a way that the polynomial pieces blend smoothly, i.e. so that the resulting composite function s(x) that equals pj(x) for x[j j+1] , all j , has several continuous derivatives.

• Any such smooth piecewise polynomial function is called a spline.

Page 10: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

0 0.5 1 1.5 2 2.5 3 3.50

0.2

0.4

0.6

0.8

1

1.2

1.4

Continuous 1st derivative

0 0.5 1 1.5 2 2.5 3 3.50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Continuous 2nd derivative

Page 11: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Two major approaches

• The ppform: Use of piecewise polynomials

• The B-form: Use of Basis functions

Page 12: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

ppform

• More efficient when evaluating the spline extensively

• The ppform of a polynomial spline of order k provides a description in terms of its breaks 1,…,l+1 and the local polynomial coefficients cji of its l pieces.

• For linear approximation

ljcxxpk

iji

ikjj :1,.)()(

1

baxccxxp jjjj 21).()(

Page 13: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Example>> x=0:0.6:pi; y=sin(x);>> figure; plot (x,y, 'o')>> sp=spapi(2,x,y);>> hold on; fnplt(sp);>> pp=sp2pp(sp); >>fnbrk(pp)The input describes a ppformbreaks(1:l+1) Columns 1 through 5 0 0.6000 1.2000 1.8000 2.4000 Column 6 3.0000coefficients(d*l,k) 0.9411 0 0.6123 0.5646 0.0697 0.9320 -0.4973 0.9738 -0.8906 0.6755pieces number l 5order k 2dimension d of target 1

0 0.5 1 1.5 2 2.5 3 3.50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

The ppform provides the coefficients for each of the polynomial pieces between the breaks. In the above example the coefficients for each of the lines is provided.

g(x)=sin(x)

Interpolating spline of order 2

Breaks (interpolation)

Page 14: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

The B-form

• Useful during construction of a spline• The B-form describes a spline as a weighted sum

• of B-splines of the required order k, with their number n>k-1+l

• Each Bj,k is defined on an interval [tj..tj+k] and is zero elsewhere.

• tj are called knots and are provided based on the smoothness required (breaks, but sometimes repeated!)

• B-splines are functions that:

n

jjkj atBtf

1, ).()(

n

jnkkj ttxxB

11, ]..[,1)(

Page 15: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

0 0.5 1 1.5 2 2.5 3 3.50

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Example

B-splines of order 2

(uniform spacing between knots)

Page 16: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Example (cont.)>> fnbrk(sp)The input describes a B-formknots(1:n+k) Columns 1 through 7 0 0 0.6000 1.2000 1.8000 2.4000 3.0000 Column 8 3.0000coefficients(d,n) 0 0.5646 0.9320 0.9738 0.6755 0.1411number n of coefficients 6order k 2dimension d of target 1

Note that in ppform, we had 6 breaks, and 10 coefficients.

Page 17: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Control Points

• aj are called the control points

• If B are B-splines aj are de Boor control points

• If B are Bezier splines aj are Bezier control points

n

jjkj atBtf

1, ).()(

Bezier Control Points

de Boor Control Points for B-splines

Page 18: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Why B-splines?• Composed of (n-k+2) curves of k-order joined Ck-2

continuously at knot values (t0,…,tn+k) smoothness and continuity

• Each point is affected by k control points• Each control point affects k segments local

controllability

• Inside convex hull Built-in boundedness does not shoot to infinity (unlike Bezier)

• Affine invariance• Any degree of detail desired Generative nature

Page 19: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Uniform B-splines

• If the knots are equi-spaced, for a given order k, the splines are simply shifted copies of one another.

Linear B-splines (k=2) Cubic B-splines (k=4)

Page 20: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

knot multiplicity

• Knots are a sequence of breaks, sometimes repeated!

• The rule is Knot multiplicity + condition multiplicity = order

• For example, for a B-spline of order k=3Simple knot smoothness_cond=2 continuity of function and first derivativeDouble knot smoothness_cond= 1 just continuityTriple knot No condition function can even be discontinuous

Page 21: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Multivariate Functions

• If f is a function of x, and g is a function of y, then their tensor-product p(x,y):=f(x)g(y) is a function of x and y.

• For example for a bivariate spline of order h in x and k in y, we have:

• Knot sequences – s= (s1,…, sm+h)– t= (t1,…,tn+k)

• Coefficients (aij:i=1..m, j=1..n)

m

i

n

jijkjjhii attyBssxByxf

1 1

),..,|(),...,|(),(

Page 22: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Chord-Length Method

• The knots must be non-decreasing!• So, what should we do for closed curves?!

• [1,4,7,10,6,3] not acceptable• t: new variable , increasing as we follow the contour• x and y will be functions of t

m

jjj

iiii

QQ

QQKtt

21

11

||||

||||

Page 23: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Representing Image Contours

20 40 60 80 100 120 140 160

50

100

150

200

Contour from Paco’s grouping algorithm

Page 24: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Interpolation

unit step: 50

Page 25: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

Page 26: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Interpolation

unit step: 10

Page 27: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

Page 28: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Smoothing

unit step: 50

Page 29: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

csaps: Cubic Smoothing Spline spaps: Tolerance Smoothing (tol=2)

spap2: Least Square Smoothing spaps: Tolerance Smoothing & Original Lines

csaps: Cubic Smoothing Spline spaps: Tolerance Smoothing (tol=2)

spap2: Least Square Smoothing spaps: Tolerance Smoothing & Original Lines

csaps: Cubic Smoothing Spline spaps: Tolerance Smoothing (tol=2)

spap2: Least Square Smoothing spaps: Tolerance Smoothing & Original Lines

csaps: Cubic Smoothing Spline spaps: Tolerance Smoothing (tol=2)

spap2: Least Square Smoothing spaps: Tolerance Smoothing & Original Lines

Page 30: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Smoothing

unit step: 10

Page 31: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

csaps: Cubic Smoothing Spline spaps: Tolerance Smoothing (tol=2)

spap2: Least Square Smoothing spaps: Tolerance Smoothing & Original Lines

csaps: Cubic Smoothing Spline spaps: Tolerance Smoothing (tol=2)

spap2: Least Square Smoothing spaps: Tolerance Smoothing & Original Lines

csaps: Cubic Smoothing Spline spaps: Tolerance Smoothing (tol=2)

spap2: Least Square Smoothing spaps: Tolerance Smoothing & Original Lines

csaps: Cubic Smoothing Spline spaps: Tolerance Smoothing (tol=2)

spap2: Least Square Smoothing spaps: Tolerance Smoothing & Original Lines

Page 32: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Note the corners

At least one sample from each line?

Unit step: 20

Page 33: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

csapi: Cubic Spline Interpolation spapi: Order k=2 Interpolation

spapi: Order k=3 Interpolation spapi: Order k=4 Interpolation

Page 34: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

Questions?

• Which method is better? Interpolation or smoothing?

• What order? Linear, cubic, …?• How can we calculate the error? Should we

compare with the original lines? Or with image?

Page 35: Splines Vida Movahedi January 2007. Outline Piecewise polynomials Splines Two approaches B-splines Multivariate functions Examples.

References1. Matlab Spline Toolbox

2. Carl de Boor, A Practical Guide to Splines, Springer, 2001.

3. ‘An Interactive Introduction to Splines’, http://www.ibiblio.org/e-notes/Splines/Intro.htm

4. Cohen, F.S.; Wang, J.Y. , “Part I: Modeling Image Curves Using Invariant 3-D Object Curve Models- A Path to 3-D Recognition and Shape Estimation from Image Contours”, IEEE Transactions on Pattern Analysis and Machine Intelligence, vol. 16, no. 1, Jan. 1994.

5. Miroslaw Kuc, COSC 4080.03 Project report: “Representing Image Contours with Splines”, Jan. 2005

6. Branka Otasevic, COSC 4080.03 Project report, Winter 1999.